extract interface that a class implementing using AST parser

半城伤御伤魂 提交于 2019-12-08 01:56:49

问题


I am compiling a project source using AST parser. In what way i can extract class hierarchy infromation, that is whether it is implementing any interface or extends from another class?


回答1:


You can visit the TypeDeclaration node and get the type binding from it.

ITypeBinding typeBind = typDec.resolveBinding();

You can then get the super class and implemented interfaces as follows:

public boolean visit(TypeDeclaration typeDeclaration) {

        ITypeBinding typeBind = typeDeclaration.resolveBinding();
        ITypeBinding superTypeBind = typeBind.getSuperclass();
        ITypeBinding[] interfaceBinds = typeBind.getInterfaces();      

        return true;
}



回答2:


If you have an IType instance (type), you can get get the class hierarchy in an ITypeHierarchy as follows

ITypeHierarchy typeHierarchie = type.newTypeHierarchy(new NullProgressMonitor());

The ITypeHierarchy has methods to query the implemented interfaces

typeHierarchie.getSuperInterfaces(type);

as well as which classes were extended

typeHierarchie.getSuperclass(type); 
typeHierarchie.getAllSuperclasses(type);


来源:https://stackoverflow.com/questions/15800942/extract-interface-that-a-class-implementing-using-ast-parser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!