java - 我怎样才能找出一个类定义了哪些枚举?

标签 java

我知道我可以通过执行以下操作来获取类的公共(public)静态成员:

obj.getClass().getFields()

但这并没有让我得到枚举。我希望能够从 getClass 方法返回的 Class 对象中获取它们。有什么想法吗?

最佳答案

(变成了一个社区维基,因为它看起来有相当大的扩展空间,例如包括 tackline 的评论。当每个人都可以扩展它时,我只是抄录评论是没有意义的。)

您是说枚举嵌套在顶级类中吗?如果是这样,请使用 <a href="http://java.sun.com/javase/6/docs/api/java/lang/Class.html#getDeclaredClasses()" rel="noreferrer noopener nofollow">Class.getDeclaredClasses()</a>并遍历结果以查看是否有任何嵌套类是枚举。测试每个嵌套类的最简单方法是使用 <a href="http://java.sun.com/javase/6/docs/api/java/lang/Class.html#isEnum()" rel="noreferrer noopener nofollow">Class.isEnum()</a> ;如果您想遍历枚举中的值,则 <a href="http://java.sun.com/javase/6/docs/api/java/lang/Class.html#getEnumConstants()" rel="noreferrer noopener nofollow">Class.getEnumConstants()</a>是要走的路。

关于java - 我怎样才能找出一个类定义了哪些枚举?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/196094/

相关文章:

java - JPA : Error in your SQL syntax in spring entity

java - 警告 : The (sub)resource method contains empty path annotation

java:如何模拟 Calendar.getInstance()?

mvn 版本和 pom.xml 中的 Java_home 为 1.7,但出现错误 "Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin"

java - 你将如何遍历 retrolambda 中的列表?

java - Spring数据Elasticsearch |通过存储库进行全文本搜索

java - 单例客户端应该如何使用单例?

java - J2SE JNDI : Issue starting standalone JNP server

java - 在从 Maven 运行的测试中使用 Kotlin `ScriptException` 时获取 `ScriptEngine`

java - 从数组填充列表的不同方法