Java tutorial using Java annotations annotation methods

  • 2020-04-01 02:55:01
  • OfStack

1. An overview of the

Annotations can be defined on methods, on classes, and an annotation is equivalent to a class, which is equivalent to an instance of an object, and an annotation is equivalent to a flag.

Common comments:
Override: represents a method that redoes the superclass,
This can also determine whether to override the parent method, in the method before this statement, if the error, then you are not overwriting the parent method, if there is no error, then overwriting the parent method.
SuppressWarnings ("deprecation") : cancels warnings from the compiler (e.g. the method you are using is out of date)
Deprecated: also on top of a method to indicate that the method is out of date, or used on a class


import java.util.ArrayList;
import java.util.List;
public class annotationDemo {

@SuppressWarnings ( "unchecked" ) 
public static void main ( String[] args )  {
List list=new ArrayList (a); 
}
}

2. Custom annotations

1. The format
Permission @interface annotation name {}
Steps:
Define the annotation class --> Class that defines the application annotation class --> A class that reflects a class that applies the annotation class (this class can be defined separately or tested in the application annotation class)


import java.lang.annotation.Retention;
importjava.lang.annotation.RetentionPolicy;
//Define this annotation to remain in the bytecode
@Retention ( RetentionPolicy.RUNTIME ) 
public @interface MyAnnotation {
}
@MyAnnotation
//Apply the defined annotation class
public class ApplyMyAnnotation {
public static void main ( String[] args )  {
if  ( ApplyMyAnnotation.class.isAnnotationPresent ( MyAnnotation.class ))  {//Determines whether the specified annotation class exists on this class
MyAnnotation annotation=  ( MyAnnotation )  ApplyMyAnnotation.class
.getAnnotation ( MyAnnotation.class ); 
System.out.println ( annotation ); 
}
   }
}

2. Declaration cycle

Format: e.g. @retention (retentionpolicy.class)
Define cycles on the annotation class of custom one. The @retention (parameter type) parameter type is RetentionPolicy
The runtime virtual machine does not retain annotations on the retentionpolicy-class: CLASS file
Retentionpolicy.runtime: the RUNTIME virtual preserves the annotations on the class file
Retentionpolicy.source: on the SOURCE file, discard the annotations
SuppressWarnings and Override are retentionpolicy.source,
Deprecated is at retentionpolicy.runtime. To be the same as defined in the RUNTIME call, it must be retentionpolicy.runtime,
The default is retentionpolicy-class:

3. Specify a target
Format: for example: @target on METHOD (elementtype.method)
What members can be annotated by the annotations defined. If this annotation is not declared, it can be placed on an element of any program.
It can be packages, interfaces, parameters, methods, local variables, fields... And so on.


//Define this annotation to remain in the bytecode
@Retention ( RetentionPolicy.RUNTIME ) 
@Target ( {ElementType.METHOD,ElementType.TYPE} ) //You can define interfaces on methods and classes that represent types
public @interface MyAnnotation {
}
@MyAnnotation
//Apply the defined annotation class
public class ApplyMyAnnotation {
@MyAnnotation//It's defined on the method
public static void main ( String[] args )  {
if  ( ApplyMyAnnotation.class.isAnnotationPresent ( MyAnnotation.class ))  {//Determines whether the specified annotation class exists on this class
MyAnnotation annotation =  ( MyAnnotation )  ApplyMyAnnotation.class
.getAnnotation ( MyAnnotation.class ); 
System.out.println ( annotation ); 
}
}
}

3. Add attributes to annotations
Type 1.
The properties of an annotation can be: 8 basic data types, String, enumeration, annotation, Class, array type,
2. Pay attention to the point
If there is only one property in the annotation solution or only one property that needs to be assigned, then when it is called, it can be written directly without specifying the property name,
When the attribute of an annotation is of type array and is assigned only one value, the {} can be omitted.
Example 3.
3.1. Attribute type (String)


import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.*;
//Define this annotation to remain in the bytecode
@Retention ( RetentionPolicy.RUNTIME ) 
public @interface MyAnnotation {
String value (a)  ;
String Color (a) default "red";//The default is "red"
}
@MyAnnotation ( "java" ) 
public class ApplyMyAnnotation {
public static void main ( String[] args )  {

if  ( ApplyMyAnnotation.class.isAnnotationPresent ( MyAnnotation.class ))  {//Determines whether the specified annotation class exists on this class
MyAnnotation annotation =  ( MyAnnotation )  ApplyMyAnnotation.class
.getAnnotation ( MyAnnotation.class ); 
System.out.println ( "value="+annotation.value ()); 
System.out.println ( "Color="+annotation.Color ()); 
}
}
  }

Results:
Value = Java
Color = red
From the calling program, you can also see that if only one property can be assigned, you can omit the property name. Otherwise @annotation class (attribute name = value)
3.2. Comprehensive type



public enum Week{
SUN,MON;
}

public @interface annotationText {
String value (a); 
}
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.*;
//Define this annotation to remain in the bytecode
@Retention ( RetentionPolicy.RUNTIME ) 
public @interface MyAnnotation {
String value (a)  ;
String Color (a) default "red";//The default is "red"
Week week (a)  default Week.MON;//Enumerated type
int [] array (a)  default {1,2,3};//An array type
annotationText annotation (a)  default @annotationText ( "MY" ); //Annotation type
Class classDemo (a)  default Integer.class;//The Class type
}
@MyAnnotation ( value="java",Color="green",week=Week.SUN,array=5,annotation=@annotationText ( "YOU" ), classDemo=String.class ) //An array of array = {4 and 6}
public class ApplyMyAnnotation {
public static void main ( String[] args )  {

if  ( ApplyMyAnnotation.class.isAnnotationPresent ( MyAnnotation.class ))  {//Determines whether the specified annotation class exists on this class
MyAnnotation annotation=  ( MyAnnotation )  ApplyMyAnnotation.class
.getAnnotation ( MyAnnotation.class ); 
System.out.println ( "value="+annotation.value ()); 
System.out.println ( "Color="+annotation.Color ()); 
System.out.println ( "week="+annotation.week ()); 
System.out.println ( "array The length of the ="+annotation.array (). length ); 
System.out.println ( "Annotation type value ="+annotation.annotation (). value ()); 
System.out.println ( "The Class type value ="+annotation.classDemo ()); 
}
}
}

  Results:
 


value=java
Color=green
week=SUN
array The length of the =1
 Annotation type value =YOU
Class Type values =classjava.lang.String

4. Notes on Method


importjava.lang.annotation.Retention;
importjava.lang.annotation.RetentionPolicy;

@Retention ( RetentionPolicy.RUNTIME ) 
public @interface annotationText{
Stringvalue (a); 
}
publicclassApplyMyAnnotation{
publicstaticvoidmain ( String[]args ) throwsException{
Methodmethodshow=ApplyMyAnnotation.class.getMethod ( "show" ); 
annotationTextanno=methodshow.getAnnotation ( annotationText.class ); 
System.out.println ( anno.value ()); 
}
@annotationText ( "java" ) 
publicvoidshow (a) {
System.out.println ( "hello" ); 
}
}

Results: the Java


Related articles: