deprecating a record component and (not) discovering that via reflection

Dmitry Bessonov dmitry.bessonov at oracle.com
Wed Dec 25 15:32:12 UTC 2019


It seems that currently record components could be @Deprecated but the fact of deprecation is not discoverable with RecordComponent::getAnnotation(s).

j.l.Deprecated doesn’t have ElementType.RECORD_COMPONENT [1] (an ‘essentialAPI' of the Records preview feature) among its @Targets [2].

The following code

import java.lang.annotation.*;
public class DeprecatingRC {
    @Target(ElementType.RECORD_COMPONENT) @Retention(RetentionPolicy.RUNTIME)
    @interface _Deprecated {}

    public static void main(String[] args) {
        record Rec(@Deprecated @_Deprecated int i) {}
        java.lang.reflect.RecordComponent rc = Rec.class.getRecordComponents()[0];
        System.err.println(java.util.Arrays.toString(rc.getAnnotations()));
    }
}

when compiled and run with JDK14b28 prints out

[@DeprecatingRC$_Deprecated()]



dmitry

[1] http://hg.openjdk.java.net/jdk/jdk/file/bb0a7975b31d/src/java.base/share/classes/java/lang/annotation/ElementType.java#l137
[2] http://hg.openjdk.java.net/jdk/jdk/file/bb0a7975b31d/src/java.base/share/classes/java/lang/Deprecated.java#l82



More information about the amber-dev mailing list