JEP proposal: Generic/Interfaced Enums as annotation attributes

Marc Miranda marc.miranda84 at gmail.com
Tue Feb 15 22:04:56 UTC 2022


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 and thishttps://stackoverflow.com/questions/1037531/is-there-a-way-to-declare-an-annotation-attribute-for-any-enum,
> 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