List of usage examples for java.lang.reflect Array newInstance
public static Object newInstance(Class<?> componentType, int... dimensions) throws IllegalArgumentException, NegativeArraySizeException
From source file:ArrayExpander.java
public static Object expandAtHead(Object array, int newSize) { if (array == null) { return null; }/*from w w w. j a v a 2s.c o m*/ Class c = array.getClass(); if (c.isArray()) { int len = Array.getLength(array); if (len >= newSize) { return array; } else { Class cc = c.getComponentType(); Object newArray = Array.newInstance(cc, newSize); System.arraycopy(array, 0, newArray, newSize - len, len); return newArray; } } else { throw new ClassCastException("need array"); } }
From source file:Main.java
public static Class<?> classOfType(Type type) { if (type instanceof Class<?>) return (Class<?>) type; if (type instanceof ParameterizedType) return (Class<?>) ((ParameterizedType) type).getRawType(); if (type instanceof WildcardType) { // Forget lower bounds and only deal with first upper bound... Type[] ubs = ((WildcardType) type).getUpperBounds(); if (ubs.length > 0) return classOfType(ubs[0]); }/*from w w w .j a v a2 s . c om*/ if (type instanceof GenericArrayType) { Class<?> ct = classOfType(((GenericArrayType) type).getGenericComponentType()); return (ct != null ? Array.newInstance(ct, 0).getClass() : Object[].class); } if (type instanceof TypeVariable) { // Only deal with first (upper) bound... Type[] ubs = ((TypeVariable<?>) type).getBounds(); if (ubs.length > 0) return classOfType(ubs[0]); } // Should never append... return Object.class; }
From source file:Main.java
/** * @return an Object array for the given object. * * @param obj Object to convert to an array. Converts primitive * arrays to Object arrays consisting of their wrapper * classes. If the object is not an array (object or primitve) * then a new array of the given type is created and the * object is set as the sole element. *///from w w w . j ava2 s. co m public static Object[] toArray(final Object obj) { // if the object is an array, the cast and return it. if (obj instanceof Object[]) { return (Object[]) obj; } // if the object is an array of primitives then wrap the array Class type = obj.getClass(); Object array; if (type.isArray()) { int length = Array.getLength(obj); Class componentType = type.getComponentType(); array = Array.newInstance(componentType, length); for (int i = 0; i < length; i++) { Array.set(array, i, Array.get(obj, i)); } } else { array = Array.newInstance(type, 1); Array.set(array, 0, obj); } return (Object[]) array; }
From source file:Main.java
public static Class<?> getRawType(Type t) { if (t instanceof Class<?>) { return (Class<?>) t; } else if (t instanceof ParameterizedType) { return (Class<?>) ((ParameterizedType) t).getRawType(); } else if (t instanceof GenericArrayType) { Class<?> cls = null; try {/* w ww .j a v a 2 s . c o m*/ cls = Array.newInstance(getRawType(((GenericArrayType) t).getGenericComponentType()), 0).getClass(); } catch (Exception e) { throw new RuntimeException(e); } return cls; } else if (t instanceof WildcardType) { Type[] types = ((WildcardType) t).getUpperBounds(); return (types.length > 0) ? getRawType(types[0]) : Object.class; } else { return Object.class; } }
From source file:Main.java
@SuppressWarnings("unchecked") public static <E> E[] toArray(Collection<?> collection, Class<E> elementType, boolean sort) { if (collection == null || collection.isEmpty()) return null; ArrayList arraylist = new ArrayList(collection); if (sort) {/*ww w . ja v a 2s . c om*/ Collections.sort(arraylist); } E[] array = (E[]) Array.newInstance(elementType, arraylist.size()); arraylist.toArray(array); return array; }
From source file:ArrayUtils.java
/** * Finds member in array and if finds it, creates the same array without this member. * // w w w . jav a 2 s. c om * @param array * @param member * @return new array without member or the old array if member wasn't found */ public static Object[] removeFromArray(Object[] array, Object member) { int i; for (i = 0; i < array.length; i++) { if (array[i] == member) { break; } } // if we found the listener, construct new arrray without it. if (i < array.length) { Object[] newArray = (Object[]) Array.newInstance(array.getClass().getComponentType(), array.length - 1); System.arraycopy(array, 0, newArray, 0, i); System.arraycopy(array, i + 1, newArray, i, array.length - i - 1); return newArray; } return array; }
From source file:jp.co.opentone.bsol.framework.core.util.CloneUtil.java
/** * ??????./* w w w . ja va2 s.c o m*/ * @param <T> ?? * @param clazz ?? * @param array * @return ??. array?null???????. */ @SuppressWarnings("unchecked") public static <T> T[] cloneArray(Class<T> clazz, T[] array) { if (array == null) { return (T[]) Array.newInstance(clazz, 0); } return (T[]) ArrayUtils.clone(array); }
From source file:Main.java
/** * get generic class by actual type argument index. *///from w w w .j a v a 2 s .co m public static Class<?> getGenericClass(Class<?> cls, int actualTypeArgIndex) { try { ParameterizedType parameterizedType; if (cls.getGenericInterfaces().length > 0 && cls.getGenericInterfaces()[0] instanceof ParameterizedType) { parameterizedType = ((ParameterizedType) cls.getGenericInterfaces()[0]); } else if (cls.getGenericSuperclass() instanceof ParameterizedType) { parameterizedType = (ParameterizedType) cls.getGenericSuperclass(); } else { parameterizedType = null; } if (parameterizedType != null) { Object genericClass = parameterizedType.getActualTypeArguments()[actualTypeArgIndex]; if (genericClass instanceof ParameterizedType) { return (Class<?>) ((ParameterizedType) genericClass).getRawType(); } else if (genericClass instanceof GenericArrayType) { Class<?> componentType = (Class<?>) ((GenericArrayType) genericClass).getGenericComponentType(); if (componentType.isArray()) { return componentType; } else { return Array.newInstance(componentType, 0).getClass(); } } else if (genericClass instanceof Class) { return (Class<?>) genericClass; } } } catch (Exception e) { } if (cls.getSuperclass() != null && cls.getSuperclass() != Object.class) { return getGenericClass(cls.getSuperclass(), actualTypeArgIndex); } else { throw new IllegalArgumentException(cls.getName() + " generic type undefined!"); } }
From source file:edu.scripps.fl.pubchem.app.util.GroupingIterator.java
public boolean hasNext() { array = Array.newInstance(clazz, groupSize); int ii = 0;//from w w w . j a va 2 s . c o m for (ii = 0; ii < groupSize; ii++) if (iterator.hasNext()) { Object obj = iterator.next(); obj = ConvertUtils.convert(obj, clazz); Array.set(array, ii, obj); } else break; if (ii < groupSize) { Object dest = Array.newInstance(clazz, ii); System.arraycopy(array, 0, dest, 0, ii); array = dest; } return ii > 0; }
From source file:Main.java
/** * This method extracts a {@link Parcelable} array from the {@link Bundle}, * and returns it in an array whose type is the exact {@link Parcelable} * subclass. This is needed because {@link Bundle#getParcelable(String)} * returns an array of {@link Parcelable}, and we would get * {@link ClassCastException} when we assign it to {@link Parcelable} * subclass arrays.// w w w . jav a2 s .c o m * * For more info, see <a * href="https://github.com/excilys/androidannotations/issues/1208">this</a> * url. * * @param bundle * the bundle holding the array which is extracted * @param key * the array is associated with this key * @param type * the desired type of the returned array * @param <T> * the element type of the returned array * @return a {@link Parcelable} subclass typed array which holds the objects * from {@link Bundle#getParcelableArray(String)} or * <code>null</code> if {@link Bundle#getParcelableArray(String)} * returned <code>null</code> for the key */ @SuppressWarnings("unchecked") public static <T extends Parcelable> T[] getParcelableArray(Bundle bundle, String key, Class<T[]> type) { Parcelable[] value = bundle.getParcelableArray(key); if (value == null) { return null; } Object copy = Array.newInstance(type.getComponentType(), value.length); System.arraycopy(value, 0, copy, 0, value.length); return (T[]) copy; }