在Java / eclipse中使用搜索引擎实现调用层次结构(获取调用某种方法的所有方法) [英] Using Search Engine to implement call hierarchy (getting all the methods that invoke a certain method) in Java/eclipse
问题描述
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();
}
}
我可以找到引用的所有方法(或调用)Hello.world()使用搜索/ Java对话框。
我想使用搜索引擎
以编程方式获得相同的结果。这是我想出的代码,但是它什么也没有返回。
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);
}
- 我的搜索代码可能有错误?
- 是否有其他方式以编程方式在eclipse插件中获取调用层次结构?
ADDED
搜索代码工作正常,问题是我错误地使用了 JavaProjectHelper.createJavaProject 。我应该打开现有的java项目,而不是创建一个同名的项目。因此,.metadata已损坏,没有任何搜索。
使用我的新的getJavaProject方法,一切都可以正常工作。
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;
}
从本网站。我可以使用CallHierarchy API。
getCallersOf()方法
package com.prosseek.asttest;
// http://www.programcreek.com/2011/07/find-all-callers-of-a-method/
import java.util.HashSet;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IMember;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.internal.corext.callhierarchy.CallHierarchy;
import org.eclipse.jdt.internal.corext.callhierarchy.MethodWrapper;
public class CallHierarchyGenerator {
public HashSet< IMethod> getCallersOf(IMethod m){
CallHierarchy callHierarchy = CallHierarchy.getDefault();
IMember [] members = {m};
MethodWrapper [] methodWrappers = callHierarchy.getCallerRoots(members);
HashSet< IMethod> callers = new HashSet< IMethod>(); (MethodWrapper mw:methodWrappers)
{
MethodWrapper [] mw2 = mw.getCalls(new NullProgressMonitor());
HashSet< IMethod> temp = getIMethods(mw2);
callers.addAll(temp);
}
return callers;
}
HashSet< IMethod> getIMethods(MethodWrapper [] methodWrappers){
HashSet< IMethod> c = new HashSet< IMethod>(); (MethodWrapper m:methodWrappers)
{
IMethod im = getIMethodFromMethodWrapper(m);
if(im!= null){
c.add(im);
}
}
return c;
}
IMethod getIMethodFromMethodWrapper(MethodWrapper m){
try {
IMember im = m.getMember();
if(im.getElementType()== IJavaElement.METHOD){
return(IMethod)m.getMember();
}
} catch(Exception e){
e.printStackTrace();
}
返回null;
}
}
findMethod实用程序
IMethod findMethod(IType类型,String methodName)throws JavaModelException
{
// IType type = project.findType(typeName);
IMethod [] methods = type.getMethods();
IMethod theMethod = null; (int i = 0; i< methods.length; i ++)
{
IMethod imethod = methods [i];
if(imethod.getElementName()。equals(methodName)){
theMethod = imethod;
}
}
if(theMethod == null)
{
System.out.println(Error,method+ methodName +not找到了);
返回null;
}
return theMethod;
}
使用情况
<$ call $ {code} CallHierarchyGenerator callGen = new CallHierarchyGenerator();
IMethod m = findMethod(type,world);
设置< IMethod> methods = new HashSet< IMethod>();
methods = callGen.getCallersOf(m);
for(Iterator< IMethod> i = methods.iterator(); i.hasNext();)
{
System.out.println(i.next()。toString()) ;
}
结果
void referWorld(){key = Lcom / prosseek / test / ReferenceHello; .referWorld()V} [在ReferenceHello [在ReferenceHello.java [在com.prosseek.test [在src [在javaTest]]]]]
void referWorld(){key = Lcom / prosseek / test / Hello; .referWorld()V} [在Hello [在Hello.java [在com.prosseek.test [在src [在javaTest]]]]]
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;
}
From the hint of this site. I could use the CallHierarchy API.
getCallersOf() method
package com.prosseek.asttest;
// http://www.programcreek.com/2011/07/find-all-callers-of-a-method/
import java.util.HashSet;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IMember;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.internal.corext.callhierarchy.CallHierarchy;
import org.eclipse.jdt.internal.corext.callhierarchy.MethodWrapper;
public class CallHierarchyGenerator {
public HashSet<IMethod> getCallersOf(IMethod m) {
CallHierarchy callHierarchy = CallHierarchy.getDefault();
IMember[] members = { m };
MethodWrapper[] methodWrappers = callHierarchy.getCallerRoots(members);
HashSet<IMethod> callers = new HashSet<IMethod>();
for (MethodWrapper mw : methodWrappers) {
MethodWrapper[] mw2 = mw.getCalls(new NullProgressMonitor());
HashSet<IMethod> temp = getIMethods(mw2);
callers.addAll(temp);
}
return callers;
}
HashSet<IMethod> getIMethods(MethodWrapper[] methodWrappers) {
HashSet<IMethod> c = new HashSet<IMethod>();
for (MethodWrapper m : methodWrappers) {
IMethod im = getIMethodFromMethodWrapper(m);
if (im != null) {
c.add(im);
}
}
return c;
}
IMethod getIMethodFromMethodWrapper(MethodWrapper m) {
try {
IMember im = m.getMember();
if (im.getElementType() == IJavaElement.METHOD) {
return (IMethod) m.getMember();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
findMethod utility
IMethod findMethod(IType type, String methodName) throws JavaModelException
{
//IType type = project.findType(typeName);
IMethod[] methods = type.getMethods();
IMethod theMethod = null;
for (int i = 0; i < methods.length; i++)
{
IMethod imethod = methods[i];
if (imethod.getElementName().equals(methodName)) {
theMethod = imethod;
}
}
if (theMethod == null)
{
System.out.println("Error, method" + methodName + " not found");
return null;
}
return theMethod;
}
The usage
CallHierarchyGenerator callGen = new CallHierarchyGenerator();
IMethod m = findMethod(type, "world");
Set<IMethod> methods = new HashSet<IMethod>();
methods = callGen.getCallersOf(m);
for (Iterator<IMethod> i = methods.iterator(); i.hasNext();)
{
System.out.println(i.next().toString());
}
The result
void referWorld() {key=Lcom/prosseek/test/ReferenceHello;.referWorld()V} [in ReferenceHello [in ReferenceHello.java [in com.prosseek.test [in src [in javaTest]]]]]
void referWorld() {key=Lcom/prosseek/test/Hello;.referWorld()V} [in Hello [in Hello.java [in com.prosseek.test [in src [in javaTest]]]]]
这篇关于在Java / eclipse中使用搜索引擎实现调用层次结构(获取调用某种方法的所有方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!