Java 如何有效地查找IType的所有子类型
我目前正在开发基于JDT的自定义重构工具。在某一点上,我希望找到一个类型的所有子类型,就像eclipse中的“类型层次结构”视图一样。我使用搜索引擎编写了一个递归函数来遍历层次结构。这是可行的,但对于深层层次结构来说速度非常慢。我是否可以使用更高效的APIJava 如何有效地查找IType的所有子类型,java,eclipse,eclipse-jdt,Java,Eclipse,Eclipse Jdt,我目前正在开发基于JDT的自定义重构工具。在某一点上,我希望找到一个类型的所有子类型,就像eclipse中的“类型层次结构”视图一样。我使用搜索引擎编写了一个递归函数来遍历层次结构。这是可行的,但对于深层层次结构来说速度非常慢。我是否可以使用更高效的API private Set<IType> searchForSubTypesOf(IType type, IProgressMonitor monitor) throws CoreException { final Set&l
private Set<IType> searchForSubTypesOf(IType type, IProgressMonitor monitor) throws CoreException {
final Set<IType> result = new HashSet<IType>();
SearchPattern pattern = SearchPattern.createPattern(type, IJavaSearchConstants.REFERENCES, SearchPattern.R_EXACT_MATCH);
SearchParticipant[] participants = new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() };
IJavaSearchScope scope = SearchEngine.createHierarchyScope(inputType);
SearchRequestor requestor = new SearchRequestor() {
@Override
public void acceptSearchMatch(SearchMatch match) throws CoreException {
if (match.getAccuracy() == SearchMatch.A_ACCURATE && match.getElement() instanceof IType) {
IType subType = (IType)match.getElement();
result.add(subType);
// Recursive search for the type found
Set<IType> subTypes = searchForSubTypesOf(subType, new NullProgressMonitor());
result.addAll(subTypes);
}
}
};
new SearchEngine().search(pattern, participants, scope, requestor, monitor);
return result;
}
private Set searchForSubTypesOf(IType类型,IProgressMonitor监视器)引发CoreException{
最终设置结果=新的HashSet();
SearchPattern=SearchPattern.createPattern(类型,IJavaSearchConstants.REFERENCES,SearchPattern.R\u精确匹配);
SearchParticipant[]参与者=新的SearchParticipant[]{SearchEngine.getDefaultSearchParticipant()};
IJavaSearchScope=SearchEngine.createHierarchyScope(输入类型);
SearchRequestor requestor=新的SearchRequestor(){
@凌驾
public void acceptSearchMatch(SearchMatch匹配)引发CoreException异常{
if(match.getaccurity()==SearchMatch.A\u accurial&&match.getElement()实例的IType){
IType subType=(IType)match.getElement();
结果:添加(亚型);
//对找到的类型进行递归搜索
Set subType=searchForSubTypesOf(subType,new NullProgressMonitor());
结果:addAll(亚型);
}
}
};
新搜索引擎()。搜索(模式、参与者、范围、请求者、监视器);
返回结果;
}
经过大量的代码阅读,我终于找到了我想要的API
我的上述功能归结为:
public static IType[] getAllSubtypesOf(IType type, IProgressMonitor monitor) throws CoreException {
return type.newTypeHierarchy(monitor).getAllSubtypes(type);
}
你不能查看一下类型层次结构的源代码,看看他们是怎么做的吗?玩得开心;-)谢谢找到它:-)请看下面。。。