[External] : Re: JEP proposal: Generic/Interfaced Enums as annotation attributes

Brian Goetz brian.goetz at oracle.com
Tue Feb 15 22:24:59 UTC 2022


One of the hidden constraints (you can see it if you look carefully at 
the spec) of annotations is that the annotation are effectively 
_compile-time constants_.  This is a reflection of the fact that 
annotations are metaDATA; they're not intended to be anything more than 
typed carriers for metadata.

This is why annotation properties are restricted to primitives, strings, 
class literals (symbolic constants), enum constants, annotations, and 
arrays of same.  Similarly, the types of interface properties must be 
narrowly scoped to one of these; JLS 9.6.1 says that the type must be a 
primitive type, String, Class, an enum type, an annotation interface 
type, or an array of one of the above.

Over the years we've received hundreds of enhancement requests around 
annotations, many of which may feel like "small tweaks" -- though which, 
in the aggregate, would consume the full bandwidth of the language 
team.  And ultimately, these are symptoms of a larger disease: people 
want annotations to be something they are not, and were explicitly never 
meant to be.

On 2/15/2022 5:04 PM, Marc Miranda wrote:
> Hi Brian!
>
> I am not sure we are talking of the same use case.
> In my case I am fine with non-generic enums, it is just about letting 
> annotation attributes pass an enum reference
>
> One of the options is using an interface, the other is using a 
> generic, but not a generic of the enum instance but the very same 
> generic that defines the enum internally Enum<?> where ? is my Enum 
> not the type of a typed enum
>
> The point is being able to trick the system and pass custom instances 
> as annotation attributes. For instance like
> @CustomAnnotation(MyCutomEnum.TYPE_A_WHICH_IMPLEMENTS_AN_INTERFACE)
>
> Regards,
> Marc
>
> El mar, 15 feb 2022 a las 17:12, Brian Goetz 
> (<brian.goetz at oracle.com>) escribió:
>
>     There was a JEP effort, "Enhanced Enums", to attempt to enable
>     generic enums:
>
>     https://openjdk.java.net/jeps/301
>
>     As it turns out, this ran into difficulties:
>
>     http://cr.openjdk.java.net/~mcimadamore/amber/enhanced-enums.html
>
>     and the JEP was withdrawn.
>
>     On 2/14/2022 3:53 PM, Marc Miranda wrote:
>>     Dear colleagues,
>>
>>     I am not sure this is the appropriate medium, but I would like to propose
>>     an enhancement for the JDK, and I am not sure what is the protocol.
>>
>>     After seeing this:
>>     https://github.com/spring-projects/spring-framework/issues/28046  <https://urldefense.com/v3/__https://github.com/spring-projects/spring-framework/issues/28046__;!!ACWV5N9M2RV99hQ!Yz6L7P3Qw9NZEdT88P-PAQJKWjex780rzw9waYrFHGpNJXiB0wTmicMvQ-7zRng42A$>  and this
>>     https://stackoverflow.com/questions/1037531/is-there-a-way-to-declare-an-annotation-attribute-for-any-enum  <https://urldefense.com/v3/__https://stackoverflow.com/questions/1037531/is-there-a-way-to-declare-an-annotation-attribute-for-any-enum__;!!ACWV5N9M2RV99hQ!Yz6L7P3Qw9NZEdT88P-PAQJKWjex780rzw9waYrFHGpNJXiB0wTmicMvQ-5GgJ1zmQ$>,
>>     I was wondering whether there is any fundamental block for allowing generic
>>     enums or even better, interfaced ones as a type for annotation attribute.
>>
>>     I think the language could benefit a lot from something like the following:
>>
>>
>>
>>
>>
>>     *enum TestEnum implements Closeable {   MY_INSTANCE_A;  public void close()
>>     {*
>>     *      (...do stuff...)*
>>
>>     *  }}*
>>
>>     *@Retention(RetentionPolicy.RUNTIME)*
>>
>>     *@Target(ElementType.TYPE)public @interface CustomCloserAnnotation {*
>>     *      X enumClass();                                                *
>>     *}*
>>
>>     I am not sure how X would be defined, I guess one of :
>>     * - Closeable*
>>     * - Enum<Closeable> *
>>     * - Enum<? extends Closeable> *
>>     * - Enum<? extends Enum<Closeable>> *
>>     * - Enum<? extends Enum<?> & Closeable>*
>>
>>     And it would be used as :
>>
>>
>>     *@CustomCloserAnnotation(TestEnum.MY_INSTANCE_A)public class MyTestClass {*
>>     *(...)*
>>     *}*
>>
>>     This would trick the system so we could use instances as enum parameters. I
>>     am not sure if I am trying to be too smart and it is something already long
>>     discussed or even discarded in the past, but from my ignorance I don't see
>>     why we should disallow this, and certainly I see a lot of possibilities
>>     with this trick, starting with the TestContainers example I provided in the
>>     introduction
>>
>>     Either way, many thanks for improving the language every day as you do!
>>
>>     Regards,
>>
>>     Marc
>


More information about the amber-dev mailing list