简体   繁体   English

检查原始字段的类型

[英]Check type of primitive field

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. 当它传递给我时,我不知道对象的类型,但我需要找到long的字段。 It is easy enough to distinguish the boxed Long s but the primitive long seems more difficult. 很容易区分盒装Long但原始long似乎更难。

I can make sure that the objects passed to me only have Longs , not the primitives, but I'd rather not. 可以确保传递给我的对象只有Longs ,而不是原语,但我宁愿没有。 So what I have is: 所以我拥有的是:

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 (不long )将是一个更好的API。

Any ideas? 有任何想法吗?

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. 您正在使用错误的常量来检查Long基元 - 使用Long.TYPE ,可以在包装器上找到具有类似命名常量的其他基本类型。 eg: Byte.TYPE , Character.TYPE , etc. 例如: Byte.TYPECharacter.TYPE

o.getClass().getField("fieldName").getType().isPrimitive();

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. 实际上,对于方法get(),您不需要知道它是哪种类型。 You can do 你可以做

// 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 如果您不确定它是否为数字类型,则可以执行此操作

Object o = field.get(object); // will always be an Object or null.
if (o instanceof Number) {
     Number n = (Number) o;
     long l = n.longValue();
  • To detect fields with long type use long.class or Long.TYPE . 要检测long类型的字段,请使用long.classLong.TYPE

  • To detect fields with Long type use Long.class . 要检测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 Class

 /** * 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 answer for Difference between Integer.class and Integer.TYPE question 还要检查Integer.class和Integer.TYPE问题之间差异的 答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM