How to check if an object is an array of a specific type

I have a Field field object.

I want to check if field object of type Foo or an array: Foo[] .

Psuedo Code:

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

Is it possible?

I tried

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

But that would allow me to check if there is a field array.

Doing this, by contrast, only checks the Foo object

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

Any idea how to do this?

Thanks.

+8
java reflection field
source share
6 answers

Here is some code that I used once to process arrays of all primitive types in Java. Since they do not extend the Object class, checking an instance of Object [] is not enough.

 /* 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. */ } } 
+16
source share

Assuming you mentioned the java.lang.reflect.Field field, you can just do

 field.getType().equals(Foo.class) || field.getType().equals(Foo[].class) 
+2
source share

A simple comparison should work

 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[]"); } } } 

}

+2
source share
 if (field instanceof Object[]) 

That should do it.

0
source share

Since array types are reified, you can simply use

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

Full example:

 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!"); } } } 
0
source share

I would do something like this:

 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()); } 
0
source share

All Articles