[英]How Can a SuperClass name be retrieved from a java file using ASTParser?
I need to retrieve SuperClass name of a java file (which extends a class, i need that class name). 我需要检索Java文件的SuperClass名称(它扩展了一个类,我需要该类名称)。 In order to do this, I began to use ASTParser but I'm newbie so any example or illustration can be helpful. 为了做到这一点,我开始使用ASTParser,但是我是新手,因此任何示例或插图都将有所帮助。 I use ASTView to comprehend which ways that AST keeps a java file structure but I'm stuck while trying visit(), endvisit() functions. 我使用ASTView来了解AST保持Java文件结构的方式,但是在尝试visit(),endvisit()函数时遇到了麻烦。 I could not find any detailed example. 我找不到任何详细的示例。
Thanks in advance... 提前致谢...
You didn't say much of how far you had got already. 您没有说太多。
In the visitor, override visit(TypeDeclaration node)
and then, on the node, call getSuperclassType()
. 在访客中,重写visit(TypeDeclaration node)
,然后在该节点上调用getSuperclassType()
。
If you return true
the parsing will continue inside the type declaration, otherwise it stops. 如果返回true
则解析将在类型声明内继续进行,否则将停止。
Could you use the getSuperClass() method of the Class class in your code instead? 您可以在代码中使用Class类的getSuperClass()方法吗?
This code will work.. 该代码将起作用。
IType type = ...;
ITypeHierarchy a = type.newSupertypeHierarchy(new NullProgressMonitor());
IType[] superTypes = a.getAllSupertypes(type);
if(superTypes.length == 1 && superTypes[0].getKey().equals("Ljava/lang/Object;")){
this.isSuperClassObject = true;
}
else{
this.isSuperClassObject = false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.