我目前正致力于基于JDT的自定义重构工具。有一次,我想找到一个类型的所有子类型,就像eclipse中的“类型层次结构”视图一样。我使用SearchEngine编写了一个通过hierarchie进行的recursiv函数。这可行,但对于深层次结构来说真的很慢。我可以使用更高效的API吗?
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;
}
答案 0 :(得分:3)
经过大量的代码阅读,我有一个很棒的时刻,我终于找到了我想要的API!
我的上述功能归结为:
public static IType[] getAllSubtypesOf(IType type, IProgressMonitor monitor) throws CoreException {
return type.newTypeHierarchy(monitor).getAllSubtypes(type);
}