如何检查java.lang.reflect.Type是否是枚举 [英] How to check if java.lang.reflect.Type is an Enum

查看:230
本文介绍了如何检查java.lang.reflect.Type是否是枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查 java.lang.reflect.Type 实例是否代表一个Emum对象。

I want to check whether a java.lang.reflect.Type instance represents an Emum object or not.

我可以检查它是否是使用==比较的特定类的实例例如:

I can check whether it's an instance of a specific class using == comparisons e.g:

type == String.class // works

但这似乎不适用于Enum类:

but this doesn't seem to work for the Enum class:

type == Enum.class // doesn't work

...这是有意义的实例将是一个特定的枚举,但我想检查类型是否为任何枚举。

... this makes sense as the instance would be of a specific enum but I would like to check whether the type is for any enum or not.

有人可以向我解释如何判断类型是否为枚举类型

Could someone explain the obvious to me of how to tell whether the Type is an enum or not please

推荐答案

if(type instanceof Class && ((Class<?>)type).isEnum())

这篇关于如何检查java.lang.reflect.Type是否是枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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