Bug 247551 - [search] SearchEngine.searchDeclarationsOfReferencedTypes does not return the correct reference when type is in a source folder
Summary: [search] SearchEngine.searchDeclarationsOfReferencedTypes does not return the...
Status: CLOSED WORKSFORME
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.4   Edit
Hardware: PC Windows XP
: P3 critical (vote)
Target Milestone: 3.5 M3   Edit
Assignee: Frederic Fusier CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-09-16 15:58 EDT by Diana Lau CLA
Modified: 2008-09-18 11:11 EDT (History)
1 user (show)

See Also:


Attachments
This zip file contains 2 projects: ModA and MyJavaProj (62.90 KB, application/x-zip-compressed)
2008-09-17 10:15 EDT, Diana Lau CLA
no flags Details
Method implementing the search (1.96 KB, text/plain)
2008-09-18 07:48 EDT, Frederic Fusier CLA
no flags Details
Method implementing the search (improved output) (2.53 KB, patch)
2008-09-18 08:27 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 Diana Lau CLA 2008-09-16 15:58:22 EDT
I have 2 projects: ProjectA and ProjectB. There's a type called MyType1 resides in ProjectA which references MyType2 resides in ProjectB. When using SearchEngine.searchDeclarationsOfReferencedTypes(IJavaElement, SearchRequestor, IProgressMonitor), it returns the correct information that MyType2 is referenced by MyType1.

However, if I create a source folder called "src", and put MyType1 inside this source folder, the SearchEngine method returns that it does not reference to MyType2, which is not true.
Comment 1 Frederic Fusier CLA 2008-09-17 04:39:48 EDT
There's no reason that putting MyType1 in a source folder makes the Search Engine missing matches it found before... So, to help me to understand what's wrong in your setup, please provide:
1) source for MyType1 (I want to know how it references MyType2)
2) the snippet you use to call searchDeclarationsOfReferencedTypes (I want to know what are the parameters value, especially the scope you use to perform the search)
3) classpathes of ProjectA and ProjectB (in fact a zip with these two projects attached to this bug would be ideal...)
Comment 2 Diana Lau CLA 2008-09-17 10:15:51 EDT
Created attachment 112769 [details]
This zip file contains 2 projects: ModA and MyJavaProj
Comment 3 Diana Lau CLA 2008-09-17 10:20:08 EDT
ModA has a source folder called "gen/src".  Inside that, DianaTest.java references the type "ABC" in MyJavaProj/src/com/ibm/test/ABC.java.

This is the snippet I'm retrieving the referenced type of a given java type. 
- javaTypes.get(i).getType() is an instance of IType.  I obtained a list of types within a java project. and the list is called javaTypes.

javaSearchEngine.searchDeclarationsOfReferencedTypes(javaTypes.get(i).getType(), collector, monitor);
	       		System.out.println("match = " + javaTypes.get(i).getFullyQualifiedName());
	       		
	       		SearchMatch[] matches =  collector.getMatches();
	       		for (int j=0; j<matches.length; j++) {
	       			
	       			System.out.println(matches[j].getResource());
	       			IProject p = matches[j].getResource().getProject(); 
	       			if (!p.equals(project)) {
	       				if (requiredProjects.indexOf(p) == -1)
	       					requiredProjects.add(p);
	       			}
	       				
	       			IPath path = matches[j].getResource().getFullPath();
	       			if (requiredPath.indexOf(path) == -1)
	       				requiredPath.add(path);
	    
	       		}


Thanks for your help.
Comment 4 Diana Lau CLA 2008-09-17 10:21:34 EDT
One missing thing: the "collector" is an class extends from SearchRequestor, it has nothing special.

public class SearchMatchCollector extends SearchRequestor 
{

	protected List<SearchMatch> matches;
	
	public SearchMatchCollector(IProject proj) {
		super();
		matches = new ArrayList<SearchMatch>();
	}
	
	public void acceptSearchMatch(SearchMatch match) throws CoreException {
			matches.add(match);
	}
	
	public SearchMatch[] getMatches() {
		return (SearchMatch[]) matches.toArray(new SearchMatch[matches.size()]);
	}
}
Comment 5 Diana Lau CLA 2008-09-17 10:23:01 EDT
If I move DianaTest.java out of gen/src folder, and put it to another package. Everything works fine.
Comment 6 Frederic Fusier CLA 2008-09-18 04:29:09 EDT
How did you get the list of types (i.e. javaTypes)?
Are you sure that the type DianaTest is still in this list after having moved the CU DianaTest.java in the source folder?
Comment 7 Frederic Fusier CLA 2008-09-18 07:48:02 EDT
Created attachment 112885 [details]
Method implementing the search

Method with code similar to your test case.
Comment 8 Frederic Fusier CLA 2008-09-18 08:27:01 EDT
Created attachment 112887 [details]
Method implementing the search (improved output)

Same search but improved output. It does not display package fragment root in which no referenced type was found and it now displays the name of the found referenced types.
Comment 9 Frederic Fusier CLA 2008-09-18 08:29:23 EDT
I unzipped the two projects in a workspace on which I run a runtime workbench with an specific action calling this method when a Java project is selected.

I ran this action on 'ModA' project (i.e. calling the method with the IJavaProject as 'ModA') with different locations for DianaTest.java compilation unit and all the results are fine.

Comment 10 Frederic Fusier CLA 2008-09-18 08:32:31 EDT
Initial test case (no change from the zipped project):
-----------------------------------------------------

Here's the output produced by the attached method snippet:

Package fragment root: <default> (/ModA)
    - package fragment: <default>
    - package fragment: diana
    - package fragment: diana.test
        + compilation unit: Component1Impl.java
            -> 2 referenced types were found:
                1) SuppressWarnings
                    => project ModA is required
                    => path /ModA is required
                2) Object
    - package fragment: my

Package fragment root: test (/ModA/my/test)
    - package fragment: <default>
    - package fragment: abc
        + compilation unit: Component1Impl.java
            -> 5 referenced types were found:
                1) SuppressWarnings
                2) Object
                3) DBUtilityTask
                    => path /ModA/commondb.jar is required
                4) String
                5) ABC
                    => project MyJavaProj is required
                    => path /MyJavaProj/src/com/ibm/test/ABC.java is required
        + compilation unit: MyTestClass.java
            -> 2 referenced types were found:
                1) String
                2) DBUtilityTask

Package fragment root: src (/ModA/gen/src)
    - package fragment: <default>
    - package fragment: moda
        + compilation unit: DianaTest.java
            -> 3 referenced types were found:
                1) String
                2) ABC
                3) DBUtilityTask


"ABC" type is well found in DianaTest.java, hence close this bug as WORKSFORME
Comment 11 Diana Lau CLA 2008-09-18 11:11:01 EDT
I followed the code snippet, and it worked for me as well. Thanks a lot.