Bug 322979 - [search] use of IJavaSearchConstants.IMPLEMENTORS yields surprising results
Summary: [search] use of IJavaSearchConstants.IMPLEMENTORS yields surprising results
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.5.1   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: 3.7 M2   Edit
Assignee: Satyam Kandula CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-08-18 03:10 EDT by joerg CLA
Modified: 2010-09-13 08:00 EDT (History)
3 users (show)

See Also:


Attachments
Experimental patch (9.96 KB, patch)
2010-08-30 07:06 EDT, Satyam Kandula CLA
no flags Details | Diff
Other experimental patch (13.26 KB, patch)
2010-09-02 10:48 EDT, Frederic Fusier CLA
no flags Details | Diff
a test (1.04 KB, text/plain)
2010-09-03 01:37 EDT, Satyam Kandula CLA
no flags Details
Proposed patch (18.67 KB, patch)
2010-09-03 07:29 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 joerg CLA 2010-08-18 03:10:37 EDT
Build Identifier: M20090917-0800

Searching for IMPLEMENTORS of, e.g. "java.lang.Object" also returns classes whose supertypes/superinterfaces use "java.lang.Object" as a type parameters. 

Considering the following lines in the Javadoc of SearchPattern.createPattern(String, ...)
"search for implementers of java.lang.Runnable: createSearchPattern("java.lang.Runnable", IJavaSearchConstants.TYPE, IJavaSearchConstants.IMPLEMENTORS, true);"

I am wondering whether (a) the observed behaviour is intended, or (b) the API documentation is outdated (there is no 'createSearchPattern' method anymore). Either way, one of them needs fixing.

Reproducible: Always

Steps to Reproduce:
1. The test class in the search scope
public class Test implements Comparable<Object> {
	@Override
	public int compareTo(Object o) {
		return 0;
	}
}
2. create the following search pattern
final SearchPattern p = SearchPattern.createPattern(
	"java.lang.Object", 
	IJavaSearchConstants.TYPE,
	IJavaSearchConstants.IMPLEMENTORS,
	SearchPattern.R_PATTERN_MATCH);
Comment 1 Satyam Kandula CLA 2010-08-30 02:29:21 EDT
(In reply to comment #0)
This is a bug and am looking into it.
Comment 2 Satyam Kandula CLA 2010-08-30 07:06:52 EDT
Created attachment 177717 [details]
Experimental patch

Frederic, For the given test case, the search doesn't take into consideration the fact that the parameter arguments are not required for super type references. The context of the reference can be known only while the file is being parsed or while the reference is getting reported. I see that the context is being checked only while the reference is getting reported rather than the file is being parsed. Is there any reason why this is not being done? Please look at the attached patch to get more sense of what I am talking about.
Comment 3 Satyam Kandula CLA 2010-08-30 07:08:39 EDT
Frederic, please look at comment 2.
Comment 4 Frederic Fusier CLA 2010-09-02 09:02:32 EDT
(In reply to comment #2)
> Created an attachment (id=177717) [details]
> Experimental patch
> 
> Frederic, For the given test case, the search doesn't take into consideration
> the fact that the parameter arguments are not required for super type
> references. The context of the reference can be known only while the file is
> being parsed or while the reference is getting reported. I see that the context
> is being checked only while the reference is getting reported rather than the
> file is being parsed. Is there any reason why this is not being done? Please
> look at the attached patch to get more sense of what I am talking about.

Remove inaccurate matches as soon as possible is the better for Search Engine. So, if it's possible to filter matches during the parse, then it's better to do it at that time. So, in this sense, your intention is correct.

However, I do not like the idea to use a context of the MatchLocator to do this filtering. Doing that, you added crossed-references between the MatchLocatorParser and the MatchLocator and also between the PatterLocator and the MatchLocator. That really worries me as that often shows a design issue and also leads to potential performances/leaks issues.

I think a simpler solution is possible. I'll be back soon with a proposal after I've tested it a little bit...
Comment 5 Frederic Fusier CLA 2010-09-02 10:48:25 EDT
Created attachment 178046 [details]
Other experimental patch

Here's my proposal. The change is specific to SuperTypeReferenceLocator which removes matches when they correspond to a type argument of the given type reference.

Note that there's no need to recurse among the type arguments as the match(TypeReference node, MatchingNodeSet nodeSet) method is called for each single type reference...
Comment 6 Satyam Kandula CLA 2010-09-03 01:37:44 EDT
Created attachment 178120 [details]
a test

(In reply to comment #5)
Removing the matches is a nice idea, but createOrPattern(...) could have problems. Though this probably is not used through UI or not typically used, we should keep in mind of it so that it is handled properly whenever it is widely used.  
Attached is a testcase which shows the problem.
Comment 7 Frederic Fusier CLA 2010-09-03 07:29:35 EDT
Created attachment 178133 [details]
Proposed patch

(In reply to comment #6)
> Created an attachment (id=178120) [details]
> a test
> 
> (In reply to comment #5)
> Removing the matches is a nice idea, but createOrPattern(...) could have
> problems. Though this probably is not used through UI or not typically used, 
> we should keep in mind of it so that it is handled properly whenever it is 
> widely used.  
> Attached is a testcase which shows the problem.

Well spotted! The AndPattern is not a problem as the expected results is the intersection of the matches, hence removing the ones for the implementors one is still ok... but I agree that the OrPattern is definitely annoying :-(

So, here's a patch which is the combination of our two proposals which fixes your additional test case, even after having made it a little bit more complex...
Comment 8 Satyam Kandula CLA 2010-09-06 01:30:17 EDT
(In reply to comment #7)
Frederic, Yes, this patch is clean and looks good. However, I am wondering if it really makes sense to classify this as a flavor and if it does really belong to the other group.
Comment 9 Frederic Fusier CLA 2010-09-06 05:18:40 EDT
(In reply to comment #8)
> (In reply to comment #7)
> Frederic, Yes, this patch is clean and looks good. However, I am wondering if
> it really makes sense to classify this as a flavor and if it does really belong
> to the other group.

IMO, this is really a flavor... That means this is something specific for the matches found through this method: they are obviously super types. That's why we also found a similar filter for fine grain specialization at the same places...
Comment 10 Satyam Kandula CLA 2010-09-07 04:21:50 EDT
Released the patch onto HEAD. Frederic, Thank you.
Comment 11 Ayushman Jain CLA 2010-09-13 08:00:42 EDT
verified for 3.7M2 using build I20100909-1700.