Role
Classification
- Standard annotation, such as frequently used @override, @Deprecated, @SuppressWarnings, the so-called standard annotation refers to the Java comes with a few annotation
-
Meta-annotation, such as @retention, @Target, @Inherited, @Documented, the so-called Meta annotation refers to the annnotation used to define annotation
- @Retention
- Used to specify the retention time for annotations
- Optional value: Retentionpolicy.source (source time), Retentionpolicy.class (compile time), Retentionpolicy.runtime (runtime)
- @ Target
- Used to specify which program elements the annotations can decorate
- Selectable values: Elementtype.type, Elementtype.method, Elementtype.constructor, Elementtype.field, Elementtype.parameter, etc.
- @ Inherited
- Used to specify whether it can be inherited by default to False
- @ Documented
- Used to specify whether to save to the Javadoc document
-
custom annotation, sometimes there is a need for their own custom annotation, the process of defining the need to use the meta-annotation
Standard annotation Use
Java comes with a part of the annotations, usually in the casual use, the most used is @override, used to indicate rewriting a function, and then there are @deprecated, indicating that the function has been discarded, no longer recommended to use. @SuppressWarnings used to indicate that an error was ignored.
Run-time annotation parsing
When we add runtime annotations to a class, field, or function, we can dynamically get the information of the annotations at run time.
Class<?> clazz=SomeClass.getClass(); Annotation[] annotations = clazz.getAnnotations();for (Annotation annotation : annotations) { System.out.println(annotation.annotationType());}
Compile-time annotation parsing
This type of annotation is used to generate code dynamically at compile time.
Writing annotations
@Target({ElementType.FIELD,ElementType.METHOD})@Retention(RetentionPolicy.CLASS)public @interface TestAnnotation { String name(); intdefault1;}
- Use @interface to define an annotation, which is a custom annotation name
- All methods have no method body, parameter, modifier
- Method return value can only be a basic data type
- Use default to add defaults
Using annotations
publicclass Person { @TestAnnotation(name="name") private String name; @TestAnnotation(name="walk") publicvoidwalk(){ System.out.println("walk"); }}
- Use @ Note name on corresponding target, attribute value is used by property = ""
Parsing annotations
@SupportedAnnotationTypes("Cn.edu.zafu.TestAnnotation") Public class testprocessor extends abstractprocessor { @Override Public Boolean Process(set<? extends typeelement> annotations, roundenvironment roundenv) { for(Typeelement te:annotations) { for(Element element:roundEnv.getElementsAnnotatedWith (TE)) {Testannotation testannotation = element. Getannotation (Testannotation.class);//testannotation.name () //testannotation.version ()} }return true; }}
- Use the @supportedannotationtypes annotation to represent the full classpath of the annotations supported by this class, supported by wildcard characters
- Inherit the Abstractprocessor class, and all processing is performed in the process
- The return value of the Process function indicates whether this set of annotations is accepted by this Processor, and if subsequent Processor are accepted, the annotations will not be processed
Reference articles
- Java Annotations Annotation of public technical points
Summary of Java annotations