I have a Java Project named "javaProject" which has two classes Hello and ReferenceHello.
Hello.java
package com.prosseek.test;
public class Hello {
public void world()
{
System.out.println("Hello world");
}
public void referWorld()
{
world();
}
}
ReferenceHello.java
package com.prosseek.test;
public class ReferenceHello {
public void referWorld()
{
Hello h = new Hello();
h.world();
}
}
I can find all the methods that references (or invokes) Hello.world() using Search/Java dialog box.
I'd like to get the same result programmatically using Search Engine
. This is the code that I came up with, however it returns nothing.
public void testIt() throws CoreException {
String projectName = "javaProject";
IJavaProject javaProject = JavaProjectHelper.createJavaProject(projectName);
String targetMethodName = "world";
SearchPattern pattern = SearchPattern.createPattern(
targetMethodName,
IJavaSearchConstants.METHOD,
IJavaSearchConstants.REFERENCES,
SearchPattern.R_CASE_SENSITIVE // <--- ????
);
boolean includeReferencedProjects = false;
IJavaElement[] javaProjects = new IJavaElement[] {javaProject};
IJavaSearchScope scope = SearchEngine.createJavaSearchScope(javaProjects, includeReferencedProjects); // <--- ????
SearchRequestor requestor = new SearchRequestor() {
@Override
public void acceptSearchMatch(SearchMatch match)
throws CoreException {
System.out.println(match.getElement());
}
};
SearchEngine searchEngine = new SearchEngine();
searchEngine.search(
pattern,
new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant()},
scope,
requestor,
null);
}
- What might be wrong in my search code?
- Is there other way to get the call hierarchy in a eclipse plugin programmatically?
ADDED
The search code works fine, the issue was with my wrong use of JavaProjectHelper.createJavaProject. I should have opened the existing java project, not creating one with the same name. As a result, .metadata was broken and nothing was searched.
With my new getJavaProject method, everything works fine now.
private IJavaProject getJavaProject(String projectName) throws CoreException
{
IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
IProject project= root.getProject(projectName);
if (!project.exists()) {
project.create(null);
} else {
project.refreshLocal(IResource.DEPTH_INFINITE, null);
}
if (!project.isOpen()) {
project.open(null);
}
IJavaProject jproject= JavaCore.create(project);
return jproject;
}