2

我目前正在研究基于 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;
}
4

1 回答 1

3

经过大量代码阅读后,我终于找到了我正在寻找的 API 的这些伟大时刻之一!

我上面的功能归结为:

public static IType[] getAllSubtypesOf(IType type, IProgressMonitor monitor) throws CoreException {
    return type.newTypeHierarchy(monitor).getAllSubtypes(type);
}
于 2013-03-26T13:55:29.097 回答