日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

How do annotations work internally--转

發布時間:2025/4/5 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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?@Override) or placed in the compiled class file and available at runtime (like Spring's?@Component). This is determined by the?@Retention?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;?here's a useful one. Look through the methods on?the?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.

?附錄:

Writing an Annotation Based Processor in Java

原文地址http://travisdazell.blogspot.hk/2012/10/writing-annotation-based-processor-in.html

The code for this tutorial is on GitHub:?https://github.com/travisdazell/Annotation-Based-Processor

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.

  • 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<? 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-INF\services 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.?

    ?

    Here's what my JAR file looks like when exploded in Eclipse.?

    ?

    5. Next, to test our annotation processor, create a class and annotate it with @Metrics.?

    package net.travisdazell.projects.testproject.dao.impl;

    import net.travisdazell.annotations.Metrics;

    @Metrics
    public class CustomerDaoImpl {
    }?


    6.When we compile this class, we'll get a message displaying the list of methods in the class. As expected, we see what's been inherited from java.lang.Object.

    轉載于:https://www.cnblogs.com/davidwang456/p/5564108.html

    總結

    以上是生活随笔為你收集整理的How do annotations work internally--转的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。