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

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

问题描述

我有一些Enum 类型.问题是:如何获取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天全站免登陆