我玩着唱片,发现了一些对我来说不合逻辑的东西:
记录:
record R(@NotEmpty Integer i) {}代码:
RecordComponent[] recordComponents = R.class.getRecordComponents();
System.out.println(recordComponents[0].isAnnotationPresent(NotEmpty.class));
//prints false但是,如果我这样做了:
System.out.println(R.class.getDeclaredFields()[0].isAnnotationPresent(NotEmpty.class));
//prints true这是意料之中的吗?因为RecordComponent implements AnnotatedElement,所以我认为RecordComponent应该有关于注释的信息。我的期望是错误的吗?
发布于 2021-10-13 18:14:49
这取决于批注的指定允许targets。record组件具有关联的构造函数参数、字段、访问器方法和类型。如果这些位置中至少有一个是允许的,则编译器将接受注释,但该注释将仅与记录的允许位置相关联。
所以下面的代码
public class RecordAnnotations {
public static void main(String[] args) {
Class<?> cl = Test.class;
for(var r: cl.getRecordComponents()) {
System.out.println("record component "
+ Arrays.toString(r.getAnnotations()) + " " + r);
System.out.println("\tof type " + r.getAnnotatedType());
System.out.println("\taccessed by " +
Arrays.toString(r.getAccessor().getAnnotations())+" "+r.getAccessor());
System.out.println("\twith return type "
+ r.getAccessor().getAnnotatedReturnType());
}
System.out.println();
for(var r: cl.getDeclaredFields()) {
System.out.println("field " + Arrays.toString(r.getAnnotations())+" "+r);
System.out.println("\tof type " + r.getAnnotatedType());
}
System.out.println();
for(var c: cl.getDeclaredConstructors()) {
System.out.println("constructor " + c);
for(var r: c.getParameters()) {
System.out.println("\tparameter "
+ Arrays.toString(r.getAnnotations()) + " " + r);
System.out.println("\t\tof type " + r.getAnnotatedType());
}
}
//System.out.println();
//for(var r: cl.getRecordComponents()) System.out.println(r);
}
}
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.RECORD_COMPONENT)
@interface WithRecord {}
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD)
@interface WithField {}
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.PARAMETER)
@interface WithParameter {}
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE_USE)
@interface WithTypeUse {}
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD)
@interface WithMethod {}
record Test(@WithRecord @WithField @WithParameter @WithTypeUse @WithMethod
int component) {
}打印
record component [@WithRecord()] int component
of type @WithTypeUse() int
accessed by [@WithMethod()] public int Test.component()
with return type @WithTypeUse() int
field [@WithField()] private final int Test.component
of type @WithTypeUse() int
constructor Test(int)
parameter [@WithParameter()] int component
of type @WithTypeUse() int显示如何将每个注释复制到其允许的位置。只有将RECORD_COMPONENT作为允许目标的注释才能通过RecordComponent的getAnnotation或isAnnotationPresent进行检索。
当然,@Retention(RetentionPolicy.RUNTIME)也是必需的。
https://stackoverflow.com/questions/69559744
复制相似问题