How do annotations work internally--转
原文地址:http://stackoverflow.com/questions/18189980/how-do-annotations-work-internally The first main distinction between kinds of annotation is whether they're used at compile time and then discarded (like? When compiling code with annotations,the compiler sees the annotation just like it sees other modifiers on source elements,like access modifiers ( There are a number of tutorials available on writing annotation processors;?. Look through the methods on?Processor?interface?for how the compiler invokes an annotation processor; the main operation takes place in the? ?附录: 原文地址http://travisdazell.blogspot.hk/2012/10/writing-annotation-based-processor-in.html The code for this tutorial is on GitHub:?Annotations have been around since Java 5. They provide a way to mark (i.e. annotate) Java code in a clean and concise manner. With Java 6 and 7,we can do even more with annotations. The most exciting of which,in my opinion,is the ability to write our own annotations and even process our own annotations to detect code issues or even generate source code at compile time.In this example,we'll create an annotation named @Metrics. We'll be able to mark Java classes with @Metrics and at compile-time,generate a report of all methods defined in the class. To create your own annotation and its corresponding processor,follow these steps.
???? public @interface Metrics {???? }?????? 2. We can now annotate any class in our project with our new annotation.???? import net.travisdazell.annotations.Metrics;???? @Metrics???? public class CustomerDaoImpl implements CustomerDao {??????? public Customer getCustomer(int customerId) {?????????? // return Customer record??????? }???? }???? 3. The next step is to write a processor that,at compile time,will report all of the methods defined in CustomerDaoImpl.java. Here's our processor in its entirety.?package net.travisdazell.annotations.processors;import java.lang.reflect.Method;import java.util.Set;import javax.annotation.processing.AbstractProcessor;import javax.annotation.processing.RoundEnvironment;import javax.annotation.processing.SupportedAnnotationTypes;import javax.annotation.processing.SupportedSourceVersion;import javax.lang.model.SourceVersion;import javax.lang.model.element.Element;import javax.lang.model.element.TypeElement;import javax.tools.Diagnostic;import net.travisdazell.annotations.Metrics;@SupportedAnnotationTypes("net.travisdazell.annotations.Metrics")@SupportedSourceVersion(SourceVersion.RELEASE_6)public class MetricsProcessor extends AbstractProcessor {??????? @Override??? public boolean process(Set extends TypeElement> arg0,??? ??? ??? RoundEnvironment roundEnv) {??? ??? StringBuilder message = new StringBuilder();??? ??? for (Element elem : roundEnv.getElementsAnnotatedWith(Metrics.class)) {??? ??? ??? Metrics implementation = elem.getAnnotation(Metrics.class);??????? ??? message.append("Methods found in " + elem.getSimpleName().toString() + ":n");??????? ??? for (Method method : implementation.getClass().getMethods()) {??????? ??? ??? message.append(method.getName() + "n");??? ??????? }??????? ??? processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE,message.toString());??? ??? }??? ??? return false; // allow others to process this annotation type??? }}??? 4. Next,we need to package our annotation processor and annotation in a JAR file. We must also include a META-INFservices directory in our JAR file. Within the services directory,include a file named javax.annotation.processing.Processor. In that file,type the fully qualified name of the annotation processor.? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |