java 获取枚举类型变量的注释
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7254126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get annotations for enum type variable
提问by Timofey Gorshkov
采纳答案by Timofey Gorshkov
As I've already offered:
正如我已经提供的:
en1.getClass().getField(((Enum)en1).name()).getAnnotations();
To be clearer:
更清楚一点:
String name = e.name(); // Enum method to get name of presented enum constant
Annotation[] annos = e.getClass().getField(name).getAnnotations(); // Classical reflection technique
In this case we have no need to know real class of en1
.
在这种情况下,我们不需要知道en1
.
See also: remark about obfuscated case.
另见:关于混淆大小写的评论。
回答by Tobias
Try this (java reflection):
试试这个(java反射):
String field = En.AAA.name();
En.class.getField(field).getAnnotations();
It should get you the annotations from AAA
.
它应该从AAA
.
EDIT:
编辑:
As the author supposed:
正如作者所假设的:
en1.getClass().getField(((Enum)en1).name()).getAnnotations();
Works for him :)
为他工作:)
回答by Riccardo T.
I just read from your comment that you already found the answer. I just wanted to remark for other people interested that, in order for that to work, those annotations must have been declared with the correct retention policy, like this:
我刚刚从你的评论中读到你已经找到了答案。我只是想为其他感兴趣的人说一句,为了使其起作用,必须使用正确的保留策略声明这些注释,如下所示:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Anno1 {
// ...
}
Without this, they will not be accessible at runtime.
没有这个,它们将无法在运行时访问。
Further reading:
进一步阅读:
回答by Jarett Millard
If you are using an obfuscator such as Proguard, you might find that the enum fields have been renamed, while .name()
still returns the original name of the field. For example, this enum...
如果您使用混淆器(例如 Proguard),您可能会发现枚举字段已重命名,但.name()
仍返回字段的原始名称。例如,这个枚举...
enum En {
FOO,
BAR
}
...would become this after ProGuarding...
......在ProGuarding之后会变成这个......
enum En {
a,
b
}
...but En.FOO.name()
will still return "FOO"
, causing getField(En.FOO.name())
to fail because it expects the field to be named "a"
.
...但En.FOO.name()
仍会返回"FOO"
,导致getField(En.FOO.name())
失败,因为它期望该字段被命名"a"
。
If you want to get the Field
for a specific enum field from obfuscated code, you can do this:
如果Field
要从混淆代码中获取特定枚举字段的 ,可以执行以下操作:
for (Field field : En.class.getDeclaredFields()) {
if (field.isEnumConstant()) {
try {
if (en1 == field.get(null)) {
Annotation[] annotations = field.getAnnotations();
}
} catch (IllegalAccessException e) {
//
}
}
}
回答by Emmanuel
Further to the existing answers, if you are in control of the enum class (can edit it), you could simply add a method to the enum to fetch the required annotation i.e.
除了现有答案之外,如果您控制枚举类(可以编辑它),您可以简单地向枚举添加一个方法来获取所需的注释,即
AnnotationClass getAnnotation(){
Field field = this.getClass().getField(this.name());
return field.getAnnotation(AnnotationClass.class);
}
or all it's annotations:
或者所有的注释:
Annotation[] getAnnotations(){
Field field = this.getClass().getField(this.name());
return field.getAnnotations();
}
Adjust the code above to handle exceptions (NoSuchFieldException and SecurityException).
调整上面的代码来处理异常(NoSuchFieldException 和 SecurityException)。