获取枚举类型变量的注释 [英] Get annotations for enum type variable

查看:145
本文介绍了获取枚举类型变量的注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些非 null 变量(例如 en1 枚举 类型。问题是:如何获取与 en1 变量引用的枚举常数相关的注释?

I have some nonnull variable (e.g. en1) of Enum type. The question is: how to get annotations related to enumeration constant referenced by en1 variable?

推荐答案

正如我已经提供的:

en1.getClass().getField(((Enum)en1).name()).getAnnotations();

更清楚:

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屋!

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