2012-02-12 20 views
0

在eclipse插件中,假設我持有Java類
org.eclipse的IJavaElement(也是IType)。 jdt.core.ITypeIJavaElement/IType(類的問題) - 如何獲取超類的IType

如何獲取其超類的IType(或其他對象表示形式,如IJavaElement)? 我不想使用newSupertypeHierarchy(),因爲這是太多的開銷。 我只想直接,一個級別,超...
我嘗試了所有的下列方法
(以下「子類型」的代碼是一個類,其超我想要得到的IJavaElement表示):

System.out.println("subType root: "+((IType) subType).getTypeRoot().getElementName()); 
System.out.println("subType primary: "+subType.getPrimaryElement().getElementName()); 
System.out.println("subType ansc: "+((IType) subType).getAncestor(0)); 
System.out.println("subType dclr:"+((IType) subType).getDeclaringType()); 
System.out.println("subType parnt: "+((IType) subType).getParent().getElementName()); 

他們沒有生產的超類

+0

不'getSuperClass'你想要什麼? – Mat 2012-02-12 07:17:31

回答

0

試試這個:

System.out.println("subType getSuperClass: "+((subType.class).getSuperClass().getName()); 
+0

我得到編譯錯誤,因爲subType是IType類型編譯器抱怨表達式'subType.class'。 IType是一個接口。所以我可以做IType.class或subType.getClass(),但沒有讓我在正確的路徑。 – inor 2012-02-12 14:17:38

1

IType.getSuperclassNa我(),但那隻會給你超類的SimpleName。問題是,超類可能不在同一個項目中,甚至可能在一些庫jar中。這就是爲什麼需要更多資源來計算。

AFAIK這是獲得超的ITYPE的最佳方式:

public static IType getSupertypeOf(final IType type, IProgressMonitor monitor) throws CoreException { 
    return type.newSupertypeHierarchy(monitor).getSuperclass(type); 
}