Live2D

反射获取注解值

1.获取变量注解上的属性值

注解类

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface primaryKey {
    String name() default "";
}

测试方法

    @Test
    public void test(){
        Student student = new Student();
        for (Field field : student.getClass().getFields()) {
            field.setAccessible(true);
           if (field.isAnnotationPresent(primaryKey.class)){
               String property=field.getAnnotation(primaryKey.class)==null?"":field.getAnnotation(primaryKey.class).name();
               System.out.println(property);
           }
        }
   }

实体类

@Data
public class Student {
    @primaryKey(name = "java")
    public String name;
    public String address;
    public int status;
}
posted @ 2022-02-18 10:37  没有梦想的java菜鸟  阅读(198)  评论(0编辑  收藏  举报