21.注解

2019-08-24  本文已影响0人  Java架构师之路

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


@Target(ElementType.CONSTRUCTOR)
@Retention(RetentionPolicy.RUNTIME)
public @interface ConstructorAnnotaion {
    String value() default "默认构造器";
}



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


@Target({ElementType.FIELD,ElementType.METHOD,ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface FieldMethodParamterAnnotaion {
    String describe();
    Class type() default void.class;
}

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


public class AnnotationClassTest {
    @FieldMethodParamterAnnotaion(describe = "编号", type = int.class)
    int id;

    @ConstructorAnnotaion("默认构造方法")
    public AnnotationClassTest() {

    }

    public AnnotationClassTest(@FieldMethodParamterAnnotaion(describe = "编号", type = int.class) int id) {
        this.id = id;
    }

    @FieldMethodParamterAnnotaion(describe = "获取编号", type = int.class)
    public int getId() {
        return id;
    }

    @FieldMethodParamterAnnotaion(describe = "设置编号")
    public void setId(int id) {
        this.id = id;
    }

}



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


public class AnnotationTest {
    @Test
    public void getConstructor(){
        Constructor[] constructors = AnnotationClassTest.class.getDeclaredConstructors();
        for (int i = 0; i <constructors.length ; i++) {
            Constructor constructor = constructors[i];
            if(constructor.isAnnotationPresent(ConstructorAnnotaion.class)){
                ConstructorAnnotaion c = (ConstructorAnnotaion)constructor.getAnnotation(ConstructorAnnotaion.class);
                System.out.println(c.value());
            }
        }
    }
    @Test
    public void getField(){
        Field[] fields = AnnotationClassTest.class.getDeclaredFields();
        for (int i = 0; i <fields.length ; i++) {
            Field field = fields[i];
            if(field.isAnnotationPresent(FieldMethodParamterAnnotaion.class)){
                FieldMethodParamterAnnotaion c = (FieldMethodParamterAnnotaion)field.getAnnotation(FieldMethodParamterAnnotaion.class);
                System.out.println(c.describe());
                System.out.println(c.type());
            }
        }
    }
    @Test
    public void getMethod(){
        Method[] methods = AnnotationClassTest.class.getDeclaredMethods();
        for (int i = 0; i <methods.length ; i++) {
            Method method = methods[i];
            if(method.isAnnotationPresent(FieldMethodParamterAnnotaion.class)){
                FieldMethodParamterAnnotaion c = (FieldMethodParamterAnnotaion)method.getAnnotation(FieldMethodParamterAnnotaion.class);
                System.out.println(c.describe());
                System.out.println(c.type());
            }
        }
    }
}

上一篇 下一篇

猜你喜欢

热点阅读