谁能给我一个简单的java注释处理器的代码片段,当它的process方法被调用时,它只打印“foo”。我已经为此工作了 2 天,但我还没能走到那一步
这是我的代码:
public class ComplexityProcessor extends AbstractProcessor {
public ComplexityProcessor() {
super();
}
@Override
public boolean process(Set annotations,
RoundEnvironment roundEnv) {
String message = "annotation found in ";
processingEnv.getMessager().printMessage(javax.tools.Diagnostic.Kind.NOTE, message);
try {
JavaFileObject jfo = processingEnv.getFiler().createSourceFile("Foo");
BufferedWriter bw = new BufferedWriter(jfo.openWriter());
bw.append("Bar!!!");
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (Element elem : roundEnv.getElementsAnnotatedWith(Complexity.class)) {
Complexity complexity = elem.getAnnotation(Complexity.class);
message = "annotation found in " + elem.getSimpleName()
+ " with complexity " + complexity.value();
processingEnv.getMessager().printMessage(javax.tools.Diagnostic.Kind.NOTE, message);
try {
JavaFileObject jfo = processingEnv.getFiler().createSourceFile("Foo");
BufferedWriter bw = new BufferedWriter(jfo.openWriter());
bw.append("Bar!!!");
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return true; // no further processing of this annotation type
}