List of usage examples for java.lang.reflect Array newInstance
public static Object newInstance(Class<?> componentType, int... dimensions) throws IllegalArgumentException, NegativeArraySizeException
From source file:com.manydesigns.elements.util.Util.java
public static <T, U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) { int newLength = to - from; if (newLength < 0) throw new IllegalArgumentException(from + " > " + to); T[] copy = ((Object) newType == (Object) Object[].class) ? (T[]) new Object[newLength] : (T[]) Array.newInstance(newType.getComponentType(), newLength); System.arraycopy(original, from, copy, 0, Math.min(original.length - from, newLength)); return copy;/* www. j av a 2 s . c o m*/ }
From source file:com.medallia.spider.api.DynamicInputImpl.java
/** * @param rt some kind of array class/*from w w w . ja va 2 s . co m*/ * @param data null, String or String[] * @return parsed data as per parseSingleValue * @throws AssertionError if parseSingleValue does */ private Object parseMultiValue(Class<?> rt, Object data, AnnotatedElement anno) throws AssertionError { String[] xs; // normalize the zero-and-one cases if (data == null) { xs = new String[0]; } else if (data instanceof String[]) { xs = (String[]) data; } else { xs = new String[] { data.toString() }; } Class<?> comp = rt.getComponentType(); Object arr = Array.newInstance(rt.getComponentType(), xs.length); for (int i = 0; i < xs.length; i++) { Array.set(arr, i, parseSingleValue(comp, xs[i], anno, inputArgParsers)); } return arr; }
From source file:com.haulmont.cuba.web.app.ui.jmxcontrol.util.AttributeEditor.java
public Object getAttributeValue(boolean allowNull) { if (checkBox != null) { Boolean value = checkBox.getValue(); return BooleanUtils.isTrue(value); } else if (dateField != null) { Date date = dateField.getValue(); return date; } else if (textField != null) { String strValue = textField.getValue(); if (allowNull && StringUtils.isBlank(strValue)) { return null; } else {//from ww w . j ava2 s .c om if (strValue == null) strValue = ""; return AttributeHelper.convert(type, strValue); } } else if (layout != null) { if (AttributeHelper.isList(type)) { return getValuesFromArrayLayout(layout); } else if (AttributeHelper.isArray(type)) { Class clazz = AttributeHelper.getArrayType(type); if (clazz != null) { List<String> strValues = getValuesFromArrayLayout(layout); Object array = Array.newInstance(clazz, strValues.size()); for (int i = 0; i < strValues.size(); i++) { Array.set(array, i, AttributeHelper.convert(clazz.getName(), strValues.get(i))); } return array; } } } return null; }
From source file:com.flexive.shared.FxArrayUtils.java
/** * Adds a element to the end of the array. * * @param list original list/*w w w .j av a 2 s. co m*/ * @param element the element to add * @param unique true if the element should only be added if it is not already contained * @return the new list */ @SuppressWarnings("unchecked") public static <T> T[] addElement(T[] list, T element, boolean unique) { // Avoid null pointer exception if (list == null) { if (element == null) { return null; } list = (T[]) Array.newInstance(element.getClass(), 1); list[0] = element; return list; } if (unique) { // Determine if the element is already part of the list. // If so do nothing for (T aList : list) if (aList == element) return list; } // Add the element to the end of the list T[] result = (T[]) Array.newInstance(element.getClass(), list.length + 1); System.arraycopy(list, 0, result, 0, list.length); result[result.length - 1] = element; return result; }
From source file:ArraysX.java
/** * Concat the two specified array.//from w ww .j a v a2 s . c om * * <p>The array could be an array of objects or primiitives. * * @param ary the array * @param ary1 the array * @return an array concat the ary and ary1 * @exception IllegalArgumentException if ary and ary1 component type are different */ public static final Object concat(Object ary, Object ary1) { int len = Array.getLength(ary) + Array.getLength(ary1); if (!ary.getClass().getComponentType().equals(ary1.getClass().getComponentType())) throw new IllegalArgumentException("These concated array component type are different."); Object dst = Array.newInstance(ary.getClass().getComponentType(), len); System.arraycopy(ary, 0, dst, 0, Array.getLength(ary)); System.arraycopy(ary1, 0, dst, Array.getLength(ary), Array.getLength(ary1)); return dst; }
From source file:gedi.util.ArrayUtils.java
@SuppressWarnings("unchecked") public static <T> T[] removeAll(T[] from, T[] remove) { Set<T> set = new HashSet<T>(Arrays.asList(remove)); ArrayList<T> re = new ArrayList<T>(); for (T f : from) if (!set.contains(f)) re.add(f);//from w w w . ja v a 2 s . c o m return re.toArray((T[]) Array.newInstance(from.getClass().getComponentType(), re.size())); }
From source file:io.coala.factory.ClassUtil.java
/** * Get the underlying class for a type, or null if the type is a variable * type. See <a//from www .java2s .c om * href="http://www.artima.com/weblogs/viewpost.jsp?thread=208860" * >description</a> * * @param type the type * @return the underlying class */ public static Class<?> getClass(final Type type) { if (type instanceof Class) { // LOG.trace("Type is a class/interface: "+type); return (Class<?>) type; } if (type instanceof ParameterizedType) { // LOG.trace("Type is a ParameterizedType: "+type); return getClass(((ParameterizedType) type).getRawType()); } if (type instanceof GenericArrayType) { // LOG.trace("Type is a GenericArrayType: "+type); final Type componentType = ((GenericArrayType) type).getGenericComponentType(); final Class<?> componentClass = getClass(componentType); if (componentClass != null) return Array.newInstance(componentClass, 0).getClass(); } return null; }
From source file:eu.sathra.io.IO.java
private Object getValue(JSONArray array, Class<?> clazz) throws Exception { Object parsedArray = Array.newInstance(clazz, array.length()); for (int c = 0; c < array.length(); ++c) { if ((clazz.equals(String.class) || clazz.isPrimitive()) && !clazz.equals(float.class)) { Array.set(parsedArray, c, array.get(c)); } else if (clazz.equals(float.class)) { Array.set(parsedArray, c, (float) array.getDouble(c)); } else if (clazz.isArray()) { // nested array Array.set(parsedArray, c, getValue(array.getJSONArray(c), float.class)); // TODO } else {//from w ww.j a v a 2 s . c o m Array.set(parsedArray, c, load(array.getJSONObject(c), clazz)); } } return parsedArray; }
From source file:com.gwtjs.common.util.spring.ObjectUtils.java
/** * Convert the given array (which may be a primitive array) to an * object array (if necessary of primitive wrapper objects). * <p>A {@code null} source value will be converted to an * empty Object array./*w ww. ja va2s.c o m*/ * @param source the (potentially primitive) array * @return the corresponding object array (never {@code null}) * @throws IllegalArgumentException if the parameter is not an array */ @SuppressWarnings("rawtypes") public static Object[] toObjectArray(Object source) { if (source instanceof Object[]) { return (Object[]) source; } if (source == null) { return new Object[0]; } if (!source.getClass().isArray()) { throw new IllegalArgumentException("Source is not an array: " + source); } int length = Array.getLength(source); if (length == 0) { return new Object[0]; } Class wrapperType = Array.get(source, 0).getClass(); Object[] newArray = (Object[]) Array.newInstance(wrapperType, length); for (int i = 0; i < length; i++) { newArray[i] = Array.get(source, i); } return newArray; }
From source file:net.sf.morph.util.TestUtils.java
/** * Return a "not-same" instance./*from w w w . j a v a2 s.c om*/ * @param type * @param o * @return */ public static Object getDifferentInstance(Class type, Object o) { if (type == null) { throw new IllegalArgumentException("Non-null type must be specified"); } if (type.isPrimitive()) { type = ClassUtils.getPrimitiveWrapper(type); } if (o != null && !type.isInstance(o)) { throw new IllegalArgumentException("Negative example object should be of type " + type); } if (type == Number.class) { type = Integer.class; } if (Number.class.isAssignableFrom(type)) { byte b = (byte) (o == null ? 0 : ((Number) o).byteValue() + 1); try { return type.getConstructor(ONE_STRING).newInstance(new Object[] { Byte.toString(b) }); } catch (Exception e) { throw e instanceof RuntimeException ? (RuntimeException) e : new NestableRuntimeException(e); } } if (type == Character.class) { char c = (char) (o == null ? 0 : ((Character) o).charValue() + 1); return new Character(c); } if (type == Boolean.class) { return o != null && ((Boolean) o).booleanValue() ? Boolean.FALSE : Boolean.TRUE; } if (type.isArray()) { return Array.newInstance(type.getComponentType(), 0); } if (type == Class.class) { return o == Object.class ? Class.class : Object.class; } return ClassUtils.newInstance(convertCommonInterfaces(type)); }