extract interface that a class implementing using

2019-05-24 22:44发布

问题:

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);