Reputation: 669
I am using JDT ASTParser to parse all Java file in given folder. I wrote the follow code:
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;
}
});
}
The thing is that, there are 2 kind of Java classes:
Bound2Processor.java is a normal java class: TypeDeclaration
package com.richardle;
import ...;
public class Bound2Processor extends AbstractAnnotationProcessor<Bound, CtMethod<?>> {
...
}
Bound.java is annotation declaration class: AnnotationTypeDeclaration
package com.richardle;
public @interface Bound {
double min();
}
But when running the code, I got the output:
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
The problem is that the name of annotation class was not printed. Maybe ASTParser not call the function public boolean visit(AnnotationTypeDeclaration node)
. Could you please tell me why ASTParser ignore this function? And how can determine a class is a normal class or annotation declaration ?
Upvotes: 1
Views: 858
Reputation: 8708
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
Upvotes: 1