JEP proposal: Generic/Interfaced Enums as annotation attributes

Marc Miranda marc.miranda84 at gmail.com
Mon Feb 14 20:53:24 UTC 2022


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