提取使用AST解析器实现的类的接口

时间:2013-04-04 01:13:18

标签: java eclipse-jdt abstract-syntax-tree

我正在使用AST解析器编译项目源代码。以什么方式我可以提取类层次结构信息,即它是实现任何接口还是从另一个类扩展?

2 个答案:

答案 0 :(得分:1)

您可以访问TypeDeclaration节点并从中获取类型绑定。

ITypeBinding typeBind = typDec.resolveBinding();

然后您可以按如下方式获取超类和实现的接口:

public boolean visit(TypeDeclaration typeDeclaration) {

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

        return true;
}

答案 1 :(得分:0)

如果您有IType实例(类型),您可以在ITypeHierarchy中获取类层次结构,如下所示

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

ITypeHierarchy具有查询已实现接口的方法

typeHierarchie.getSuperInterfaces(type);

以及扩展了哪些类

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