Is there a way to declare an annotation attribute for *any* enum?

前端 未结 3 2110
终归单人心
终归单人心 2021-02-07 04:04

At the moment I am developing an annotation-based binding-framework for Java Swing that uses JGoodies Binding under the hood. Unfortunately I am stuck with an annotation for a J

相关标签:
3条回答
  • 2021-02-07 04:20

    It's not going to work the way you want it to. As you've found out, you can only use really plain return types in annotations. Additionally, trying to get around these restrictions by doing stuff like abusing String isn't going to work because you need to be using a constant expression for initialising your annotation's values.

    I think the closest you're going to get is to initialise with a String and then use code to compare with the enum's name(). But there goes your type safety...

    0 讨论(0)
  • 2021-02-07 04:30

    If your enums can implement all the same interface, you may find useful this question "Coding tip - intersection types and java enums"

    0 讨论(0)
  • 2021-02-07 04:44

    I was trying to solve this exact same problem, and as far as I know, it can not be done. It's a real bummer.

    In my case, I wanted to specify @Version annotation, where any enumeration can be used, and enum values can be compared by ordinal (to find ordering of versions). Looks like I need to do what some other frameworks (like Guice I think) do and use doubles instead; bit ugly, but works ok for >= and <= checks.

    0 讨论(0)
提交回复
热议问题