【问题标题】:How to check if an object is an array of a certain type如何检查对象是否是某种类型的数组
【发布时间】:2012-06-21 21:30:49
【问题描述】:

我有一个对象Field field

我想检查 fieldFoo 类型的对象还是数组:Foo[]

伪代码:

if field.getType() is Foo || field.getType is Foo[]

这可能吗?

我试过了

if (field.getType().isArray())
    // do something

但这只会让我检查field 是否是一个数组。

相反,这样做只会检查它是否是Foo的对象

if (Foo.class.isAssignableFrom(field.getType())
     // do something

知道怎么做吗?

谢谢。

【问题讨论】:

  • "我有一个对象字段" ??您的对象是字段类型。你是在检查它的 Foo 还是 Foo[]
  • 我编辑了我的答案(现在是一个新答案:-))

标签: java reflection field


【解决方案1】:
if (field instanceof Object[])

应该可以的。

【讨论】:

    【解决方案2】:

    由于数组类型被具体化,你可以使用

    if ( field.getType() == Foo.class || field.getType() == Foo[].class ) {
    }
    

    完整示例:

    public class Scratchpad {
        String[] strings;
    
        public static void main(String[] args) throws NoSuchFieldException {
            if (Scratchpad.class.getDeclaredField("strings").getType() == String[].class) {
                System.out.println("They're Strings!");
            }
    
            if (Scratchpad.class.getDeclaredField("strings").getType() == Long[].class) {
                System.out.println("They're Longs!");
            }
        }
    }
    

    【讨论】:

    • 我不相信Foo[].class 会编译。
    • 显然,我刚试过Foo[].class,这不是一个有效的语法
    【解决方案3】:

    假设你提到的字段是java.lang.reflect.Field,你可以这样做

    field.getType().equals(Foo.class) || field.getType().equals(Foo[].class)
    

    【讨论】:

      【解决方案4】:

      简单的比较应该可以工作

      import java.lang.reflect.Field;
      
      public class Main {
      
      String[] myStringArray;
      String[] myStringArray2;
      
      Object[] myObjectArray;
      
      String str;
      
      
      public static void main(String... args) {
              Field[] flds = Main.class.getDeclaredFields();
              for (Field f : flds) {
                  Class<?> c = f.getType();
      
                  if (c == String[].class) {
                      System.out.println("field" + f.getName() + " is String[]");
                  } 
                  if (c == String.class) {
                      System.out.println("field" + f.getName() + " is String");
                  }
                  if (c == Object[].class) {
                      System.out.println("field" + f.getName() + " is Object[]");
                  } 
              }
      }
      

      }

      【讨论】:

        【解决方案5】:

        这是我曾经用来处理 Java 中所有基本类型的数组的一些代码。由于它们没有扩展 Object 类,因此对 Object[] 的 instanceof 检查是不够的。

        /* Check if the given object is an array. */
        if (object.getClass().isArray()) {
        
            Class<?> componentType;
            componentType = object.getClass().getComponentType();
        
            if (componentType.isPrimitive()) {
        
                if (boolean.class.isAssignableFrom(componentType)) {
                    for (boolean anElement : (boolean[]) object) {
                        /* ... */
                    }
                }
        
                else if (byte.class.isAssignableFrom(componentType)) {
                    /* ... */
                }
        
                else if (char.class.isAssignableFrom(componentType)) {
                    /* ... */
                }
        
                else if (double.class.isAssignableFrom(componentType)) {
                    /* ... */
                }
        
                else if (float.class.isAssignableFrom(componentType)) {
                    /* ... */
                }
        
                else if (int.class.isAssignableFrom(componentType)) {
                    /* ... */
                }
        
                else if (long.class.isAssignableFrom(componentType)) {
                    /* ... */
                }
        
                else if (short.class.isAssignableFrom(componentType)) {
                    /* ... */
                }
        
                /* No else. No other primitive types exist. */
            }
        
            else {
                /* Do something with Object[] here. */
            }
        }
        

        【讨论】:

          【解决方案6】:

          我会这样做:

          public static void main(String[] args) {
              Foo foo = new Foo();
              Foo[] other = new Foo[1];
              other[0] = new Foo();
          
              System.out.println(isFooOrArrayOfFoo(foo));
              System.out.println(isFooOrArrayOfFoo(other[0]));
              System.out.println(isFooOrArrayOfFoo(new Object()));
          }
          
          private static boolean isFooOrArrayOfFoo(Object o) {
              return (o instanceof Foo || o.getClass().equals(Foo.class) && o.getClass().isArray());
          }
          

          【讨论】:

            猜你喜欢
            • 2011-07-13
            • 2011-09-28
            • 1970-01-01
            • 2012-01-03
            • 1970-01-01
            • 1970-01-01
            • 2011-03-25
            • 2020-05-27
            • 2011-11-05
            相关资源
            最近更新 更多