How to implement something similar to the @Override java annotation?

前端 未结 2 1734
眼角桃花
眼角桃花 2021-02-04 19:36

With this jdk code in ../java/lang/Override.java,

package java.lang;
import java.lang.annotation.*;
@Target(ElementType.METHOD)
@Retention(Retentio         


        
相关标签:
2条回答
  • 2021-02-04 19:48

    which is nothing more than an interface.

    So,

    How does interface java.lang.Override syntax help java compiler to detect above error at compile time?

    That's right. Override is nothing more than an interface. The actual work is done by the java compiler. How the compiler does this is not specified.

    Here are some links that explain how to work with an AnnotationProcessor to implement something similar to @Override :

    1. Processor Java doc
    2. Java annotation processing tool
    3. Code generation using AnnotationProcessor
    4. Annotation Processor, generating a compiler error
    5. Source code analysis using Java 6 API
    6. Playing with Java annotation processing
    0 讨论(0)
  • 2021-02-04 20:00

    The implementation that triggers the compile error doesn't lie in the annotation, it lies in the Java compiler.

    If you want to write your own similar annotation processor, you would use the annotation processor API: http://docs.oracle.com/javase/7/docs/api/javax/annotation/processing/Processor.html

    0 讨论(0)
提交回复
热议问题