Annotations will only be parsed by the ASTParser, if the Java compliance is set to be greater than 1.5
.
From the ASTParser Javadoc:
In order to parse 1.5 code, some compiler options need to be set to 1.5
So you need to add the following lines to your code:
Map options = JavaCore.getOptions();
JavaCore.setComplianceOptions(JavaCore.VERSION_1_5, options);
parser.setCompilerOptions(options);
EDIT
Here is the complete parse
method I used for testing:
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;
}
});
}
And here are the very simple classes I provided as input to parse(String)
:
public @interface Bound {
double min();
}
public class Bound2Processor {
}
This is the Output:
Annotaion: Bound
Type: Bound2Processor