関連付けられた列挙値への参照を取得するために、列挙値に関連付けられた注釈文字列値を使用しようとしています。
途中で行き詰まってしまいました...現在、以下の開発コードがあります。
注釈コード:
public @interface MyCustomAnnotation{
String value();
}
列挙コード:
public enum MyEnum{
@MyCustomAnnotation("v1")
VALUE1,
@MyCustomAnnotation("v2")
VALUE2,
@MyCustomAnnotation("v3")
VALUE3,
}
enum アノテーションを利用する:
String matchString = "v1";
MyEnum myEnumToMatch;
// First obtain all available annotation values
for(Annotation annotation : (MyEnum.class).getAnnotations()){
// Determine whether our String to match on is an annotation value against
// any of the enum values
if(((MyCustomAnnotation)annotation).value() == matchString){
// A matching annotation value has been found
// I need to obtain a reference to the corrext Enum value based on
// this annotation value
for(MyEnum myEnum : MyEnum.values()){
// Perhaps iterate the enum values and obtain the individual
// annotation value - if this matches then assign this as the
// value.
// I can't find a way to do this - any ideas?
myEnumToMatch = ??
}
}
}
前もって感謝します。