Community
Participate
Working Groups
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.
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...)
Created attachment 112769 [details] This zip file contains 2 projects: ModA and MyJavaProj
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.
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()]); } }
If I move DianaTest.java out of gen/src folder, and put it to another package. Everything works fine.
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?
Created attachment 112885 [details] Method implementing the search Method with code similar to your test case.
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.
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.
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
I followed the code snippet, and it worked for me as well. Thanks a lot.