任何人都可以向我提供一个简单的java注释处理器的代码片段,只需在调用其进程方法时打印“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
}
答案 0 :(得分:2)
我发现了答案
那么你对未来的需求: