Java获取注解信息

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

/**
 * @auther CharlieLiang
 * @date 2022/5/13-12:38
 */
@TableStu("db_Student")
public class Student2 {
    @FieldStu(columnName = "db_id",type = "int",length = 10)
    private int id;
    @FieldStu(columnName = "db_age",type = "int",length = 10)
    private int age;
    @FieldStu(columnName = "db_name",type = "varchar",length = 3)
    private String name;

    public Student2() {
    }

    public Student2(int id, int age, String name) {
        this.id = id;
        this.age = age;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

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

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "Student2{" +
                "id=" + id +
                ", age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}

//类名的注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface TableStu {
    String value();
}

//属性的注解
@Target({ElementType.TYPE,ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@interface FieldStu{
    String columnName();
    String type();
    int length();
}

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

/**
 * @auther CharlieLiang
 * @date 2022/5/13-12:47
 */
public class TestStudent2 {
    public static void main(String[] args) throws Exception {
        Class c1=Class.forName("reflection.Student2");

        //通过反射获得注解
        Annotation[] annotations = c1.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation);
        }

        //获得注解的值
        TableStu tableStu = (TableStu) c1.getAnnotation(TableStu.class);
        String val=tableStu.value();
        System.out.println(val);

        //获得类指定的注解
        Field f=c1.getDeclaredField("name");
        FieldStu anno=f.getAnnotation(FieldStu.class);
        System.out.println(anno.columnName());
        System.out.println(anno.type());
        System.out.println(anno.length());

        System.out.println("==============id==========");
        Field f2=c1.getDeclaredField("id");
        FieldStu anno2=f.getAnnotation(FieldStu.class);
        System.out.println(anno2.columnName());
        System.out.println(anno2.type());
        System.out.println(anno2.length());

        System.out.println("==============age==========");
        Field f3=c1.getDeclaredField("age");
        FieldStu anno3=f.getAnnotation(FieldStu.class);
        System.out.println(anno3.columnName());
        System.out.println(anno3.type());
        System.out.println(anno3.length());
    }
}

运行结果:

Java获取注解信息_第1张图片

 

你可能感兴趣的:(java,开发语言)