目前我正在開發基於註釋的Java Swing綁定框架,它使用JGoodies Binding。不幸的是我被一個JRadioButton綁定的註解卡住了。 我想要做的是指定一個包含特殊值(枚舉)的模型的屬性名稱。如果此屬性具有特定值,則應選擇單選按鈕。現在,我想在這樣的批註指定的值:有沒有辦法爲* any *枚舉聲明一個註解屬性?
@RadioButtonBinding(property = "selectedItem", selectedValue = MyEnum.FIRST)
JRadioButton firstButton
@RadioButtonBinding(property = "selectedItem", selectedValue = MyEnum.SECOND)
JRadioButton secondButton
不過,我不知道如何聲明註釋允許上述和任何其他枚舉了。我的第一個猜測是這樣的,但我知道註釋屬性不能是通用的:
@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();
}
任何想法如何解決這個問題?
這裏列出的RadioButtonBinding註釋包含兩個想法:第一個是指定Enum類和從中使用的String常量。但是這當然不是類型安全的,可能會導致RuntimeExceptions。 第二個是直接指定枚舉的值,但這似乎不起作用。 – 2009-06-29 06:20:46
btw我用JGoodies做了很多這樣的東西,這是我在Java中遇到的更悲慘的時刻之一。最大的問題是他有一個「基本」類,他的大部分其他類都是擴展的,所以他的方法有類似於方法(基本,基本,基本)的簽名,其中「基本」類必須是特定實例,但是簽名不會讓你知道它必須是什麼特定的類。綁定部分是一個好主意,但他在當時(幾年前)沒有很好的支撐,而且還沒有完成,只有德文記錄。 – 2013-08-16 17:48:07