Hello,
I am using the below search to all method declarations within a package root:
IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { javaProject });
SearchEngine searchEngine = new SearchEngine();
String pattern = "jdt.search.pkg.*.*";
SearchPattern searchParttern = SearchPattern.createPattern(pattern, IJavaSearchConstants.METHOD, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_PATTERN_MATCH);
SearchRequestor requestor = new SearchRequestor() {
@Override
public void acceptSearchMatch(SearchMatch match) throws CoreException {
System.out.format("In %s found: %s %n", match.getResource().getName(), match.getElement());
}
};
searchEngine.search(searchParttern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope, requestor, monitor);
Now, this works as expected for the sources in my project, but seems to return random results from the classpath of the project (it is an m2e project so there are maven dependencies managed by m2e).
Here is the problem when running this:
Searching for: jdt.search.pkg.*.*...
In search-project found: hasNext() (not open) [in <anonymous> [in SourceEvents$1.class [in gherkin.stream [in /Users/karypid/.m2/repository/io/cucumber/gherkin/5.0.0/gherkin-5.0.0.jar]]]]
...
In search-project found: protect() (not open) [in <anonymous> [in TestSetup$1.class [in junit.extensions [in /Users/karypid/.m2/repository/junit/junit/4.12/junit-4.12.jar]]]]
...
In SomeClass.java found: someMethod(String) (not open) {key=Ljdt/search/pkg/SomeClass;.someMethod(Ljava/lang/String;)V} [in SomeClass [in SomeClass.java [in jdt.search.pkg [in src/test/java [in search-project]]]]]
In SomeClass.java found: someMethod(int) (not open) {key=Ljdt/search/pkg/SomeClass;.someMethod(I)V} [in SomeClass [in SomeClass.java [in jdt.search.pkg [in src/test/java [in search-project]]]]]
The last two results are the only ones I expect (in package jdt.search.pkg I have in my source folder a Java file for testing).
All the previous ones are from random packages of anonymous classes in the classpath.
I actually looked at JUnit's TestSetup class and replicated the "protect()" method declaration in the project's source file (jdt/search/TestSetup.java) to see if it will get reported. It looks like this:
package jdt.search;
import junit.framework.Protectable;
import junit.framework.TestResult;
public class TestSetup {
public void run(final TestResult result) {
Protectable p = new Protectable() {
public void protect() throws Exception {
}
};
}
}
However, this does NOT show up in the matches.
So is there some problem in JDT or am I doing something wrong?
P.S. I am using the slightly outdated 2018-09 release.
P.P.S I noticed that it does NOT always happen. My test dependencies are below and removing cucumber to leave only junit seems to solve the issue (which is why I think this may be a JDT issue) and returns only the last two results (even through junit is still in the classpath).
<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>2.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>2.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>