【发布时间】:2014-03-25 21:42:34
【问题描述】:
我正在使用 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 annotations eclipse-jdt