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

时间:2014-03-20 03:26:37

标签: java annotations eclipse-jdt

我正在使用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会忽略这个功能吗?如何确定一个类是普通的类或注释声明?

1 个答案:

答案 0 :(得分:1)

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

来自ASTParser Javadoc:

  

为了解析1.5代码,需要将一些编译器选项设置为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