2012-06-19 14 views
8

Mam obiekt Field field.Jak sprawdzić, czy obiekt jest tablicą określonego typu?

Chciałbym sprawdzić, czy field jest obiektem typu Foo lub tablicą: Foo[].

kod Psuedo:

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

Czy to możliwe?

Próbowałem

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

Ale to pozwoliłoby mi tylko sprawdzić, czy field jest tablicą.

Spowoduje to, wręcz przeciwnie, będzie sprawdzać tylko wtedy, gdy jest to przedmiotem Foo

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

Każdy pomysł jak to zrobić?

Dzięki.

+0

"Mam pole pole obiektowe" ?? Twój obiekt jest typu Field. Sprawdzasz, czy jego Foo lub Foo [] –

+0

Edytowałem swoją odpowiedź (teraz jest nowa :-)) – Ixx

Odpowiedz

16

Oto kod użyłem raz obsłużyć tablice wszystkich typów pierwotnych w Javie. Ponieważ nie rozszerzają klasy Object, sprawdzanie instancji dla Object [] jest niewystarczające.

/* 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. to jest to, czego potrzebuję –

0
if (field instanceof Object[]) 

To powinno wystarczyć.

0

Ponieważ rodzaje tablic są reified, można po prostu użyć

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

Pełny przykład:

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

Nie wierzę, że 'Foo []. Class' by nawet skompilował. –

+0

Najwyraźniej po prostu próbowałem 'Foo []. Class', a to nie jest poprawna składnia –

+0

Działa dobrze dla mnie na java 6. –

2

Zakładając, że pole wspomnieć jest java.lang.reflect.Field, można po prostu zrobić

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

Proste porównanie powinno zadziałać

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

chciałbym zrobić coś takiego:

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()); 
} 
Powiązane problemy