gpt4 book ai didi

java - 如何使用JDT确定类是AnnotationDeclaration还是TypeDeclaration

转载 作者:太空宇宙 更新时间:2023-11-04 06:57:32 25 4
gpt4 key购买 nike

我正在使用 JDT ASTParser 来解析给定文件夹中的所有 Java 文件。我编写了以下代码:

private void parse(String fileContent) {
// TODO Auto-generated method stub
//advise the parser to parse the code following to the Java Language Specification, Third Edition.
ASTParser parser = ASTParser.newParser(AST.JLS3);
// tell the parser, that it has to expect an ICompilationUnit (a pointer to a Java file) as input.
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(fileContent.toCharArray());
parser.setResolveBindings(true);
final CompilationUnit cu = (CompilationUnit) parser.createAST(null);

cu.accept(new ASTVisitor() {
public boolean visit(AnnotationTypeDeclaration node) {

System.out.println("Annotaion: " + node.getName());

return true;
}

public boolean visit(TypeDeclaration node) {

System.out.println("Type: " + node.getName());

return true;
}
});

}

问题是,有两种 Java 类:

  • Bound2Processor.java 是一个普通的 java 类:TypeDeclaration

    package com.richardle;

    import ...;


    public class Bound2Processor extends AbstractAnnotationProcessor<Bound, CtMethod<?>> {
    ...
    }
  • Bound.java是注解声明类:AnnotationTypeDeclaration

        package com.richardle;    
    public @interface Bound {
    double min();
    }

    但是运行代码时,我得到了输出:

    File: D:\SOFTWARE\Android\SpoonTest\src\com\richardle\Bound.java // no thing print here
    File: D:\SOFTWARE\Android\SpoonTest\src\com\richardle\Bound2Processor.java
    Type: Bound2Processor

问题是没有打印注释类的名称。也许 ASTParser 没有调用函数 public boolean Visit(AnnotationTypeDeclaration node)。你能告诉我为什么 ASTParser 忽略这个函数吗?那么如何判断一个类是普通类还是注解声明呢?

最佳答案

如果 Java 合规性设置为大于 1.5,则注释只会由 ASTParser 进行解析。

来自 ASTParser Javadoc:

In order to parse 1.5 code, some compiler options need to be set to 1.5

因此您需要将以下行添加到代码中:

Map options = JavaCore.getOptions();
JavaCore.setComplianceOptions(JavaCore.VERSION_1_5, options);
parser.setCompilerOptions(options);

编辑

这是我用于测试的完整parse方法:

public static void parse(String fileContent) {
//advise the parser to parse the code following to the Java Language Specification, Third Edition.
ASTParser parser = ASTParser.newParser(AST.JLS3);
Map options = JavaCore.getOptions();
JavaCore.setComplianceOptions(JavaCore.VERSION_1_5, options);
parser.setCompilerOptions(options);

// tell the parser, that it has to expect an ICompilationUnit (a pointer to a Java file) as input.
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(fileContent.toCharArray());
parser.setResolveBindings(true);
final CompilationUnit cu = (CompilationUnit) parser.createAST(null);

cu.accept(new ASTVisitor() {
public boolean visit(AnnotationTypeDeclaration node) {
System.out.println("Annotaion: " + node.getName());
return true;
}

public boolean visit(TypeDeclaration node) {
System.out.println("Type: " + node.getName());
return true;
}
});
}

这是我提供的非常简单的类作为parse(String)的输入:

public @interface Bound {
double min();
}

public class Bound2Processor {

}

这是输出:

Annotaion: Bound
Type: Bound2Processor

关于java - 如何使用JDT确定类是AnnotationDeclaration还是TypeDeclaration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22522870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com