Java 检查原始字段的类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1891595/
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
Check type of primitive field
提问by macbutch
I'm trying to determine the type of a field on an object. I don't know the type of the object when it is passed to me but I need to find fields which are long
s. It is easy enough to distinguish the boxed Long
s but the primitive long
seems more difficult.
我正在尝试确定对象上字段的类型。我不知道传递给我的对象的类型,但我需要找到long
s 的字段。区分盒装Long
s很容易,但原始类型long
似乎更难。
I canmake sure that the objects passed to me only have Longs
, not the primitives, but I'd rather not. So what I have is:
我可以确保传递给我的对象只有Longs
,而不是基元,但我宁愿没有。所以我所拥有的是:
for (Field f : o.getClass().getDeclaredFields()) {
Class<?> clazz = f.getType();
if (clazz.equals(Long.class)) {
// found one -- I don't get here for primitive longs
}
}
A hacky way, which seems to work, is this:
一种似乎有效的hacky方式是:
for (Field f : o.getClass().getDeclaredFields()) {
Class<?> clazz = f.getType();
if (clazz.equals(Long.class) || clazz.getName().equals("long")) {
// found one
}
}
I'd really like a cleaner way to do this if there is one. If there is no better way then I think that requiring the objects I receive to only use Long
(not long
) would be a better API.
如果有的话,我真的很想要一种更清洁的方法来做到这一点。如果没有更好的方法,那么我认为要求我收到的对象只使用Long
(not long
) 将是一个更好的 API。
Any ideas?
有任何想法吗?
采纳答案by mP.
You're using the wrong constant to check for Long primitives - use Long.TYPE
, each other primitive type can be found with a similarly named constant on the wrapper. eg: Byte.TYPE
, Character.TYPE
, etc.
您正在使用错误的常量来检查 Long 原语 - 使用Long.TYPE
,可以在包装器上使用类似命名的常量找到其他原语类型。如:Byte.TYPE
,Character.TYPE
等等。
回答by Droo
o.getClass().getField("fieldName").getType().isPrimitive();
回答by Peter Lawrey
You can just use
你可以使用
boolean.class
byte.class
char.class
short.class
int.class
long.class
float.class
double.class
void.class
If you are using reflection, why do you care, why do this check at all. The get/set methods always use objects so you don't need to know if the field is a primitive type (unless you try to set a primitive type to the null value.)
如果您正在使用反射,您为什么要关心,为什么要进行此检查。get/set 方法始终使用对象,因此您无需知道该字段是否为原始类型(除非您尝试将原始类型设置为空值。)
In fact, for the method get() you don't need to know which type it is. You can do
事实上,对于 get() 方法,您不需要知道它是哪种类型。你可以做
// any number type is fine.
Number n = field.get(object);
long l = n.longValue();
If you are not sure if it is a Number type you can do
如果您不确定它是否是 Number 类型,您可以这样做
Object o = field.get(object); // will always be an Object or null.
if (o instanceof Number) {
Number n = (Number) o;
long l = n.longValue();
回答by ahmed hamdy
To detect fields with
long
type uselong.class
orLong.TYPE
.To detect fields with
Long
type useLong.class
.
要检测
long
类型为long.class
或 的字段Long.TYPE
。要检测
Long
类型为的字段,请使用Long.class
.
Example:
例子:
for (Field f : o.getClass().getDeclaredFields()) {
Class<?> clazz = f.getType();
// to detect both Long and long types
if (Long.class.equals(clazz) || long.class.equals(clazz)) {
// found one
}
}
Notice:
注意:
Long.TYPE
is static Constant member and is equivalent to long.class
.
Long.TYPE
是静态常量成员,等价于long.class
.
snippet code form Long
Class
片段代码表单Long
类
/** * The {@link Class} object that represents the primitive type {@code long}. */ @SuppressWarnings("unchecked") public static final Class<Long> TYPE = (Class<Long>) long[].class.getComponentType();
/** * The {@link Class} object that represents the primitive type {@code long}. */ @SuppressWarnings("unchecked") public static final Class<Long> TYPE = (Class<Long>) long[].class.getComponentType();
Also check for answerfor Difference between Integer.class and Integer.TYPEquestion