Java developers implement custom annotation

Java developers implement custom annotation

 

Has been developed to comment very curious, most recently at last I have time to practice their own one, record it in case the latter will use it ha ha ha 

First, we look at the standard sample custom annotations, annotation class using the keyword @interface modification and annotation information in the annotation above the class declaration contains the following four types of information

@Documented - whether notes will be included in the JavaDoc

@Retention - when to use the annotation

@Target - notes for what

@Inherited - whether to allow subclass inherits the comment

 

 . 1) @ Retention - the definition of the annotation lifecycle
  ● RetentionPolicy.SOURCE: discarded at compile time. These annotations at the end of the compiler will no longer have any meaning, they do not write byte code. @Override, @SuppressWarnings belong to such comments.
  ● RetentionPolicy.CLASS: class loading when discarded. Useful bytecode files. NOTE Using this way by default
  ● RetentionPolicy.RUNTIME: are never discarded run also keep the notes, so you can use reflection to read the annotation information. Annotations our custom commonly used in this way.

  2.) Target - indicates that the annotations are used where. The default value is any element, indicating that the annotation is used somewhere. Available parameters include ElementType
  ● ElementType.CONSTRUCTOR: constructor is used to describe
  ● ElementType.FIELD: member variables, objects, properties (including enum example)
  ● ElementType.LOCAL_VARIABLE: local variables used to describe
  ● ElementType.METHOD: a method for describing
  ● ElementType.PACKAGE: package for describing
  ● ElementType.PARAMETER: parameters used to describe
  ● ElementType.TYPE: for describing class, interface (including type of annotation), or an enum declaration

 3) @ Documented -. A simple Annotations mark notes, annotations indicating whether the information added to the java documentation.

 . 4) @ Inherited - define the relationship between the notes and subclass
     @Inherited meta-annotation is a marker annotation, @ Inherited describes a certain type of indexed is inherited. If using a modified @Inherited annotation type is used for a class, then this annotation is used in a subclass of this class.

 

Statement >> custom annotation class

@Target (value = {ElementType.TYPE, ElementType.METHOD , ElementType.FIELD}) scope to specify the current annotation class class method attributes are

@Retention (value = RetentionPolicy.RUNTIME) to retain the running annotation, annotation information can be read by reflection

com.gaunyi.batteryonline.annotation Package; 

Import java.lang.annotation.ElementType; 
Import java.lang.annotation.Retention; 
Import java.lang.annotation.RetentionPolicy; 
Import java.lang.annotation.Target; 

/ ** 
 * the Created 2019/8/20 S0111 ON by. 
 * custom annotation class declaration 
 * / 
@Target (value = {ElementType.TYPE, ElementType.METHOD, ElementType.FIELD}) 
@Retention (value = RetentionPolicy.RUNTIME) 
public {@interface MyAnnotationDefinition 

  / * define the parameters inside annotation information * / String name (); String value (); String path (); }

 

>> Use custom annotation

In each class, method, information on the property annotation

package com.gaunyi.batteryonline.annotation;

/**
 * Created by S0111 on 2019/8/20.
 * 自定义注解类使用
 */
@MyAnnotationDefinition(name="类名称",value="类值",path="类路径")
public class MyAnnotationUse {

    @MyAnnotationDefinition(name="属性名",value="属性值",path="属性路径")
    private String name;

    @MyAnnotationDefinition(name="年龄",value="18",path="/user2")
    private String age;

    @MyAnnotationDefinition(name="方法名",value="方法值",path="方法访问路径")
    public String testAnno(){
        return "successs!!!";
    }

    @MyAnnotationDefinition(name="方法名1",value="方法值1",path="方法访问路径1")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
}

 

>> 读取注解信息(测试注解类)

这里通过反射读取注解信息,注解内容与对应的类、方法、属性对应。

package com.gaunyi.batteryonline.annotation;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * Created by S0111 on 2019/8/20.
 * 自定义注解类测试
 */
public class MyAnnotationTest {

    public static void  main(String[] args) throws Exception{
        Class clazz = Class.forName("com.gaunyi.batteryonline.annotation.MyAnnotationUse");

        //获取类注解信息
        MyAnnotationDefinition classAnno =(MyAnnotationDefinition) clazz.getAnnotation(MyAnnotationDefinition.class);
        System.out.println( classAnno.name()+"---"+classAnno.value()+"---"+classAnno.path());

        //获取所以方法注解信息 ps:这里需要使用 isAnnotationPresent 判断方法上是否使用了注解
        Method[] allMethods = clazz.getDeclaredMethods();
        for(int i=0;i<allMethods.length;i++){
            if(allMethods[i].isAnnotationPresent(MyAnnotationDefinition.class)) {
                MyAnnotationDefinition methodAnno = allMethods[i].getAnnotation(MyAnnotationDefinition.class);
                System.out.println("遍历:当前方法名为:"+allMethods[i].getName()+" 的注解信息:---"+methodAnno.name() + "---" + methodAnno.value() + "---" + methodAnno.path());
            }
        }

        //获取指定方法注解信息
       Method methodTest = clazz.getDeclaredMethod("testAnno");
        MyAnnotationDefinition methodAnnotest =  methodTest.getAnnotation(MyAnnotationDefinition.class);
        System.out.println( methodAnnotest.name()+"---"+methodAnnotest.value()+"---"+methodAnnotest.path());


        //获取属性注解信息
        Field nameField =  clazz.getDeclaredField("name");
        MyAnnotationDefinition attrAnno = nameField.getAnnotation(MyAnnotationDefinition.class);
        System.out.println( attrAnno.name()+"---"+attrAnno.value()+"---"+attrAnno.path());
    }
}

  

>>测试结果如下:

 

至此我们就实现了自定义注解啦....  关于自定义注解的实际应用,待我使用时再来更新...

Guess you like

Origin www.cnblogs.com/DFX339/p/11386722.html