2012-06-19 6 views
8

Ich habe ein Objekt Field field.Wie überprüft man, ob ein Objekt ein Array eines bestimmten Typs ist

Ich möchte überprüfen, ob field entweder ein Objekt vom Typ Foo oder ein Array ist: Foo[].

Psuedo Code:

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

Ist das möglich?

Ich habe

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

Aber das würde mir nur erlauben, zu überprüfen, ob field ein Array ist.

dies tut, im Gegenteil, nur prüfen, ob es ein Objekt von Foo

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

Jede Idee, wie dies zu tun?

Danke.

+0

"Ich habe ein Objekt Feldfeld" ?? Ihr Objekt ist vom Typ Field. Y, überprüfen Sie, ob es Foo oder Foo [] –

+0

Ich habe meine Antwort bearbeitet (es ist jetzt ein neues :-)) – Ixx

Antwort

16

Hier einige Code, den ich einmal verwendet Arrays aller primitiven Typen in Java zu handhaben. Da sie die Object-Klasse nicht erweitern, ist eine instanceof-Prüfung auf Object [] nicht ausreichend.

/* 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. */ 
    } 
} 
+0

Thanhks. Das ist das, was ich benötige –

0
if (field instanceof Object[]) 

Das sollte es tun.

0

Da Array-Typen verdinglicht sind, können Sie einfach

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

Voll Beispiel verwenden:

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

Ich glaube nicht, 'Foo []. Class' würde sogar kompilieren. –

+0

Offensichtlich habe ich gerade 'Foo []. Class' versucht, und das ist keine gültige Syntax –

+0

Funktioniert gut für mich auf Java 6. –

2

das Feld Sie erwähnen Angenommen, ein java.lang.reflect.Field ist, können Sie einfach

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

Einfacher Vergleich sollte funktionieren

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

}

0

Ich würde so etwas tun:

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()); 
} 
Verwandte Themen