Bug 142044 - [search] "And" Pattern fails with NullPointerException
Summary: [search] "And" Pattern fails with NullPointerException
Status: CLOSED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.2   Edit
Hardware: PC Windows 2000
: P5 normal (vote)
Target Milestone: 3.4 M1   Edit
Assignee: Frederic Fusier CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-05-16 13:51 EDT by Gerry Kessler CLA
Modified: 2008-04-11 13:55 EDT (History)
0 users

See Also:


Attachments
Proposed patch (320.21 KB, patch)
2007-07-04 09:58 EDT, Frederic Fusier CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Gerry Kessler CLA 2006-05-16 13:51:55 EDT
The code below is failing.   The elems list hold valid interface IType objects and the constructed SearchPatterns are not null.   
	
        JavaSearchScopeFactory factory= JavaSearchScopeFactory.getInstance();
	IJavaSearchScope scope= factory.createJavaSearchScope(new IJavaElement[]{myJavaProject}, false);
	
	SearchPattern patternA = SearchPattern.createPattern((IJavaElement)elems.get(0), IJavaSearchConstants.IMPLEMENTORS, 0);
	SearchPattern patternB = SearchPattern.createPattern((IJavaElement)elems.get(1), IJavaSearchConstants.IMPLEMENTORS, 0);
	SearchPattern andedPattern = SearchPattern.createAndPattern(patternA, patternB);
	
	SearchRequestor requestor = new Searcher();
	SearchEngine engine = new SearchEngine();
	try {
		engine.search(andedPattern, new SearchParticipant[]  {SearchEngine.getDefaultSearchParticipant()}, scope, requestor, null);
	} catch (CoreException e) {
		...
	}
	...
	
This results in a NullPointerException in MatchLocator:

	java.lang.NullPointerException
	at org.eclipse.jdt.internal.core.search.matching.MatchLocator.<init>(MatchLocator.java:351)
	at org.eclipse.jdt.internal.core.search.JavaSearchParticipant.locateMatches(JavaSearchParticipant.java:85)
	at org.eclipse.jdt.internal.core.search.BasicSearchEngine.findMatches(BasicSearchEngine.java:212)
	at org.eclipse.jdt.internal.core.search.BasicSearchEngine.search(BasicSearchEngine.java:503)
	at org.eclipse.jdt.core.search.SearchEngine.search(SearchEngine.java:538)
	...
	
	
The problem seems to stem from the static method PatternLocator.patternLocator(SearchPattern pattern) returning null.
Comment 1 Olivier Thomann CLA 2006-05-16 14:36:51 EDT
Indeed, PatternLocator.patternLocator(SearchPattern) can return null. We should either handle the null case or return a non-null pattern.
In the same way, the call to PatternLocator.patternLocator(SearchPattern pattern) in org.eclipse.jdt.internal.core.search.matching.OrLocator can also put null patterns in the patternLocators array.
There is no null check when retrieving the pattern locators.
Comment 2 Frederic Fusier CLA 2007-07-04 09:58:28 EDT
Created attachment 73026 [details]
Proposed patch

Note that for AND patterns, Search Engine returns matches which will both be returned by each pattern of the AND. Unfortunately that means it does not return any match for the initial bug scenario given in comment 0 as results of requests "Implementors of I1" and "Implementors of I2" have no real common match.

The goal of this patch is just to:
1) avoid the NPE
2) implement minimal behavior for the AND pattern allowing deeper implementation
   in case of a strong need appears in the future...
Comment 3 Frederic Fusier CLA 2007-07-04 09:59:44 EDT
Released for 3.4M1 in HEAD stream.
Comment 4 Frederic Fusier CLA 2007-08-03 09:39:01 EDT
Verified (from code of added tests) for 3.4M1 using build I20070802-0800.
Comment 5 Gerry Kessler CLA 2008-04-11 13:55:22 EDT
closing