2013-03-25 51 views
2

我目前正在開發基於JDT的自定義重構工具。在某一點上,我希望找到所有類型的子類型,就像eclipse中的「Type Hierarchy」視圖一樣。我使用SearchEngine編寫了一個通過hierarchie的遞歸函數。這有效,但對於深層次結構來說速度緩慢。我可以使用更高效的API嗎?如何高效地查找IType的所有子類型

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

你們能不能考慮的類型層次的根源,看看他們是如何做的? – Kai 2013-03-25 15:23:55

+1

[TypeHierarchyViewPart](http://grepcode.com/file/repository.grepcode.com/java/eclipse.org/3.6.1/org.eclipse.jdt/ui/3.6.1/org/eclipse/jdt/internal /ui/typehierarchy/TypeHierarchyViewPart.java#TypeHierarchyViewPart.updateHierarchyViewer%28boolean%29)玩得開心;-) – Kai 2013-03-25 16:03:30

+0

謝謝!發現它:-)見下面... – 2013-03-26 13:55:50

回答

3

大量的代碼閱讀後,我有一個偉大的時刻,我終於找到了我正在尋找的API!

我上面boilds功能到這一點:

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