获取枚举类型变量的注解 [英] Get annotations for enum type variable
本文介绍了获取枚举类型变量的注解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的事情:
public enum En {
@Anno1("This is AAA")
@Anno2(500)
AAA,
@Anno1("What is this?")
@Anno3(secret="AH239B0EC")
BBB,
@Anno2(9000)
CCC;
}
和地方我有变量对象
键入,以加入WTO要求一些 EN1
) >恩恒定分配。同时,我只知道 EN1
的一些枚举
键入
And somewhere I have variable (e.g. en1
) of Object
type, to whitch some En
constant assigned. At the same time I know only that en1
of some enum
type.
现在的问题是:如何去枚举,在 EN1
变量holded者不变的有关注释
The question is: how to get annotations related to enumeration constant that holded in en1
variable?
推荐答案
正如我已经提出:
en1.getClass().getField(((Enum)en1).name()).getAnnotations();
要更清楚:
Enum e = (Enum)en1; // Unsafe class cast, but it is already known that en1 is Enum
String name = e.name(); // Enum method to get name of presented enum constant
Annotation[] annos = e.getClass().getField(name).getAnnotations(); // Classical reflection technique
在这种情况下,我们没有必要知道真正的类 EN1
。
In this case we have no need to know real class of en1
.
这篇关于获取枚举类型变量的注解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文