我目前正在研究基于 JDT 的自定义重构工具。在某一时刻,我想找到一种类型的所有子类型,就像 eclipse 中的“类型层次结构”视图一样。我使用搜索引擎编写了一个遍历层次结构的递归函数。这可行,但对于深层层次结构来说确实很慢。我可以使用更高效的 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;
}