加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

How do annotations work internally--转

发布时间:2020-12-14 06:21:28 所属栏目:Java 来源:网络整理
导读:原文地址: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? @Override ) or placed in the

原文地址: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?@Override) or placed in the compiled class file and available at runtime (like Spring's?@Component). This is determined by the??policy of the annotation. If you're writing your own annotation,you'd need to decide whether the annotation is helpful at runtime (for autoconfiguration,perhaps) or only at compile time (for checking or code generation).

When compiling code with annotations,the compiler sees the annotation just like it sees other modifiers on source elements,like access modifiers (public/private) or?final. When it encounters an annotation,it runs an annotation processor,which is like a plug-in class that says it's interested a specific annotation. The annotation processor generally uses the Reflection API to inspect the elements being compiled and may simply run checks on them,modify them,or generate new code to be compiled.?@Override?is an example of the first; it uses the Reflection API to make sure it can find a match for the method signature in one of the superclasses and uses the?Messagerto cause a compile error if it can't.

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?process?method,which gets called every time the compiler sees an element that has a matching annotation.

?附录:

原文地址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.

  1. Define the annotation. This is done by defining the annotation with @interface.

???? 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 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.?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读