【问题标题】:Why RecordComponent doesn't have annotation info that is defined in the Records class in Java 17?为什么 RecordComponent 没有 Java 17 的 Records 类中定义的注释信息?
【发布时间】:2021-12-02 04:25:02
【问题描述】:

我玩唱片,发现一些对我来说看起来不合逻辑的东西:

记录:

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 应该有关于注释的信息。我的期望错了吗?

【问题讨论】:

  • 你必须显示@NotEmpty的定义。我想,它的@Target 注释(如果有的话)没有列出RECORD_COMPONENT

标签: java java-record java-17


【解决方案1】:

这取决于注解指定的允许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 为允许目标的注解,才能通过RecordComponentgetAnnotationisAnnotationPresent 检索。

当然,@Retention(RetentionPolicy.RUNTIME) 也是必需的。

【讨论】:

  • 谢谢。情况就是这样。我的目标是 ElementType.FIELD
猜你喜欢
  • 2019-04-02
  • 2021-11-15
  • 1970-01-01
  • 1970-01-01
  • 2013-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-17
相关资源
最近更新 更多