You get to know one article: custom annotation

With more and more extensive use of annotations, notes gradually appreciate the convenience brought by the same time, it will put forward new requirements, how to customize annotations according to their needs.

Use custom annotations. We define a @interface annotation, then define its properties and properties to the annotation can be marked, the methods, classes, and annotations, we again use annotations, using the method of acquiring the target object getDeclaredAnnotataion annotation instance of an object.

How to define annotation. Defined annotations need to use @interface. Such as:

public @interface Test {} annotations to add custom annotations or metadata default configuration.

Meta-annotation interpretation. Where @Target used, @ Retention when to use, @ Inherited whether to allow inheritance.

@Target represent notes for what. Type specific parameters are as follows: 
ElementType.CONSTRUCTOR, a constructor
ElementType.FIELD, a member variable, attribute
ElementType.LOCAL, local variables are used to describe
ElementType.METHOD, describe a method for
ElementType.PACKAGE, for describing package
ElementType. the pARAMETER, is used to describe the parameters
ElementType.TYPE, used to describe a class, interface
@Retention indicate when to use the annotation. Specific parameters are as follows: 
RetentionPolicy.SOURCE, it represents discarded at compile time, after compilation, meaningless. Such as: @Override.
RetentionPolicy.CLASS, represents lost to when the class is loaded, useful when loading the bytecode using a default in this way.
RetentionPolicy.RUNTIME, represents not always discarded at run time, using the reflection of the information read annotation. Custom annotation mostly in this way.
@Inherited indicating whether it is inherited, if used, it means that can be inherited.

Examples of operations carried out below

A first customize a comment. Such as:

@Target(value = ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Teacher {
public String name() default "";
public String school() default "";
public String students() default "";
}

Second, the use reflection to tell what to do this comment

{class AnnotationUtil public 
public static void getTeacherInfo (Class clazz) {
String teacherInfo = "teacher information:";
// Get all variables class field
Field, [] = clazz.getDeclaredFields Fields ();
// field traversing the array
for (Field field : Fields) {
IF (field.isAnnotationPresent (Teacher.class)) {
// find the notes have @Teacher
Teacher Teacher = field.getAnnotation (Teacher.class);
teacherInfo = teacherInfo teacher.name + () + "," + teacher.school () + "," + teacher.students ();
System.out.println (teacherInfo);
}
}
}
}

Third, the use of the notes elsewhere

{class SmallStudent public 

@Teacher (name = "Huang", school = "in a Hangzhou", students = "Middle Class Five")
public String Teacher;
}

Fourth, the test

@Test
public void testAnnotation(){
AnnotationUtil.getTeacherInfo(SmallStudent.class);
}

Fifth, the test results

Teacher Information: Huang, Hangzhou, a medium, and high class five

Finally, custom annotations you understand it, welcome discussion and attention.

Guess you like

Origin www.cnblogs.com/shilei-ysl/p/11031716.html