-2
任何人都可以給我提供一個簡單的java註釋處理器的代碼片段,當它的處理方法被調用時它會打印「foo」。我在這已經工作了2天,我一直沒能走到這一步Java AnnotationProcessor源代碼
這裏是我的代碼:
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
}
你的代碼在哪裏? – Jeffrey 2012-04-09 14:33:07
[堆棧溢出不是代碼編寫,教程或諮詢服務](http://meta.stackexchange.com/a/128553/172496) – 2012-04-09 14:39:34
在這裏你可以找到[教程](http://www.javalobby。組織/ JAVA /論壇/ t17876.html)。 – dash1e 2012-04-09 14:40:21