JEP proposal: Generic/Interfaced Enums as annotation attributes
Brian Goetz
brian.goetz at oracle.com
Tue Feb 15 16:12:30 UTC 2022
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 this
> https://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