Beispiel #1
0
  @SuppressWarnings("unchecked")
  public static <T> T toPrimitiveArrayIfPossible(Class<T> clz, Object value) {
    if (clz == Typ.intArray) {
      return (T) iarray(value);
    } else if (clz == Typ.byteArray) {
      return (T) barray(value);
    } else if (clz == Typ.charArray) {
      return (T) carray(value);
    } else if (clz == Typ.shortArray) {
      return (T) sarray(value);
    } else if (clz == Typ.longArray) {
      return (T) larray(value);
    } else if (clz == Typ.floatArray) {
      return (T) farray(value);
    } else if (clz == Typ.doubleArray) {
      return (T) darray(value);
    } else if (value.getClass() == clz) {
      return (T) value;
    } else {
      int index = 0;
      Object newInstance = Array.newInstance(clz.getComponentType(), Boon.len(value));
      Iterator<Object> iterator = iterator(Typ.object, value);
      while (iterator.hasNext()) {

        Object item = iterator.next();

        if (clz.getComponentType().isAssignableFrom(item.getClass())) {

          BeanUtils.idx(newInstance, index, item);

        } else {

          item = coerce(clz.getComponentType(), item);

          BeanUtils.idx(newInstance, index, item);
        }
        index++;
      }
      return (T) newInstance;
    }
  }
Beispiel #2
0
 public static Object toArrayGuessType(Collection<?> value) {
   Class<?> componentType = Reflection.getComponentType(value);
   Object array = Array.newInstance(componentType, value.size());
   @SuppressWarnings("unchecked")
   Iterator<Object> iterator = (Iterator<Object>) value.iterator();
   int index = 0;
   while (iterator.hasNext()) {
     BeanUtils.idx(array, index, iterator.next());
     index++;
   }
   return array;
 }