Get an entity class field in java and then get one of its annotations, and then how to get the properties set in the annotations on this field?

for example, demo2 comment:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
public @interface Demo2 {
    int max() default 100;

    int min() default 1;

    boolean isNotNull() default true;
}

now only the annotated attribute isNotNull is set in a field of the entity class

@Demo2(isNotNull = false)
    private String sex;

so how do I get this property set in the annotation on this field? That is, as long as the property with the new value is set, the other properties that do not set the new value do not get it?

Jun.25,2021

Field f = YourClass.class.getDeclaredField("sex");
Demo2 d = f.getDeclaredAnnotation(Demo2.class);
System.out.println(d.isNotNull());

   

Menu