有没有办法为* any *枚举声明一个注释属性? [英] Is there a way to declare an annotation attribute for *any* enum?

查看:114
本文介绍了有没有办法为* any *枚举声明一个注释属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在为Java Swing开发基于注释的绑定框架,该框架使用 JGoodies绑定引擎盖。不幸的是,我被卡住了一个JRadioButton绑定的注释。
我想做的是指定一个具有特殊值(枚举)的模型的属性名称。如果此属性具有特定值,则应选择单选按钮。现在我想在这样的注释中指定值:

  @RadioButtonBinding(property =selectedItem,selectedValue = MyEnum。 FIRST)
JRadioButton firstButton

@RadioButtonBinding(property =selectedItem,selectedValue = MyEnum.SECOND)
JRadioButton secondButton
pre>

但是,我不知道如何声明允许上述和任何其他枚举的注释。我第一次猜测是这样,但是我了解到注释属性不能是通用的:

  @Documented 
@Retention(RetentionPolicy .RUNTIME)
@Target(ElementType.FIELD)
public @interface RadioButtonBinding {

/ **所选值绑定的模型属性* /
String property();

//想法1:指定枚举类和枚举常量作为字符串 - 工作但不是类型安全

类<?扩展枚举>> enumClass();

String enumConstantName();

//想法2:直接指定枚举常量 - 给出编译时错误

< T extends Enum< T>> T enumValue();

}

任何想法如何解决?

解决方案

这不是你想要的方式。您已经发现,您只能在注释中使用非常简单的返回类型。另外,尝试通过像滥用String这样的东西来解决这些限制是不行的,因为您需要使用常量表达式来初始化注释的值。



我认为最接近的是使用String初始化,然后使用代码与枚举的名称()进行比较。但是你的类型安全...


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 JRadioButton-binding. What I want to do is specify a property-name of a model which holds a special value (enum). The radio-button shall be selected if this property has a specific value. Now I want to specify the value in the annotation like this:

@RadioButtonBinding(property = "selectedItem", selectedValue = MyEnum.FIRST)
JRadioButton firstButton

@RadioButtonBinding(property = "selectedItem", selectedValue = MyEnum.SECOND)
JRadioButton secondButton

However, I do not know how to declare the annotation to allow the above and any other enum, too. My first guess was this, but I learned that annotation attributes cannot be generic:

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface RadioButtonBinding {

    /** The model-property to which the selected value is bound */
    String property();

    // Idea 1: Specifying the enum class and the enum constant as String - works but is not typesafe

    Class<? extends Enum<?>> enumClass();

    String enumConstantName();

    // Idea 2: Directly specifying the enum constant - gives a compile-time error

    <T extends Enum<T>> T enumValue();

}

Any ideas how to solve this?

解决方案

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...

这篇关于有没有办法为* any *枚举声明一个注释属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆