为什么 RecordComponent 没有在 Records class in Java 17 中定义的注释信息?
Why RecordComponent doesn't have annotation info that is defined in the Records class in Java 17?
我在玩唱片,发现一些对我来说不符合逻辑的东西:
记录:
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
应该有关于注释的信息。我的期望有误吗?
这取决于注释的指定允许targets。记录组件具有关联的构造函数参数、字段、访问器方法和类型。如果至少允许这些位置之一,编译器将接受注释,但注释只会与记录的允许位置相关联。
所以下面的代码
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)
也是必须的
我在玩唱片,发现一些对我来说不符合逻辑的东西:
记录:
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
应该有关于注释的信息。我的期望有误吗?
这取决于注释的指定允许targets。记录组件具有关联的构造函数参数、字段、访问器方法和类型。如果至少允许这些位置之一,编译器将接受注释,但注释只会与记录的允许位置相关联。
所以下面的代码
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)
也是必须的