gpt4 book ai didi

java - 如何有效地查找 IType 的所有子类型

转载 作者:行者123 更新时间:2023-12-04 05:05:05 24 4
gpt4 key购买 nike

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

最佳答案

在阅读了大量代码之后,我终于找到了我正在寻找的 API!

我上面的功能归结为:

public static IType[] getAllSubtypesOf(IType type, IProgressMonitor monitor) throws CoreException {
return type.newTypeHierarchy(monitor).getAllSubtypes(type);
}

关于java - 如何有效地查找 IType 的所有子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15617098/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com