简体   繁体   English

如何使用ASTParser从Java文件中检索超类名称?

[英]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()方法吗?

getSuperClass() 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.

相关问题 我可以使用Java ASTParser解析.war或.jar文件吗? - Using Java ASTParser can I parse a .war or .jar file? 从给定的.java文件中删除使用ASTParser的if语句 - deleting a if statements using ASTParser from a given .java file 如何使用Eclipse JDT ASTParser获取方法的类名? - How to get a class name of a method by using Eclipse JDT ASTParser? 在Java中使用ASTParser的变量的数据类型 - Datatype of a variable using ASTParser in java astparser如何在setSource函数中将.java文件设置为ICompilationUnit? - astparser how to set a .java file as an ICompilationUnit in setSource function? 在Java中使用ASTParser实现Metric Suite - Implementing a Metric Suite using ASTParser in Java 如何使用Eclipse JDT ASTParser从类或接口名称(String)获取ITypeBinding? - How to get an ITypeBinding from a class or interface name (String) with Eclipse JDT ASTParser? 如何使用talend检查从ftp站点检索的文件是否存在于java的本地文件夹中? - How to check if a file retrieved from the ftp site exists in the local folder in java using talend? 我如何从jsp表单中检索数据到java控制器类(从数据库表中检索输入字段的名称 - how can i retrieve the data from a jsp form to the java controller class(the name of input fields are retrieved from the database table 如何将数据库中的信息(使用Java代码检索)打印到html中? - How can I print information from a database (retrieved using java code) into html?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM