List of usage examples for java.lang.reflect Array newInstance
public static Object newInstance(Class<?> componentType, int... dimensions) throws IllegalArgumentException, NegativeArraySizeException
From source file:ArrayConverter.java
/** * @param maybeArray/*from ww w.j av a 2s . c o m*/ * @param dimensions * @return */ public static Object[] getArray(final Object maybeArray, final Class arrayType, final int dims) { if (maybeArray == null) { return null; } if (dims <= 0) { return null; } if (maybeArray.getClass().isArray() == false) { Object object = maybeArray; for (int i = 0; i < dims; i++) { final Object[] array = (Object[]) Array.newInstance(arrayType, 1); array[0] = object; object = array; } return (Object[]) object; } if (ArrayConverter.getDimensionCount(maybeArray.getClass()) < dims) { return null; } return (Object[]) maybeArray; }
From source file:Main.java
private static <T, U> T[] copyOfRange(final U[] original, final int from, final int to, final Class<? extends T[]> newType) { final int newLength = to - from; if (newLength < 0) { throw new IllegalArgumentException(from + " > " + to); }//from w w w. j a va2 s.c o m final 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; }
From source file:code.elix_x.excore.utils.client.gui.elements.ListGuiElement.java
public ListGuiElement(String name, int xPos, int yPos, int width, int height, int elementY, int borderX, int borderY, RGBA backgroundColor) { super(name, xPos, yPos, width, height, borderX, borderY); // this.elements = new ListElement[0]; this.elements = (ListGuiElement<H>.ListElement[]) Array.newInstance(ListElement.class, 0); this.elementY = elementY; this.backgroundColor = backgroundColor; }
From source file:ArrayUtil.java
/** * Copies the elements of the {@code oldArray} to a new array with extra space to * append the given array {@code toAppend1} and the element {@code toAppend2}. *//*from www.j a v a 2 s . c o m*/ @SuppressWarnings("unchecked") public static <T> T[] append(T[] oldArray, T[] toAppend1, T toAppend2) { Class<?> component = oldArray.getClass().getComponentType(); T[] array = (T[]) Array.newInstance(component, oldArray.length + toAppend1.length + 1); System.arraycopy(oldArray, 0, array, 0, oldArray.length); System.arraycopy(toAppend1, 0, array, oldArray.length, toAppend1.length); array[array.length - 1] = toAppend2; return array; }
From source file:com.mmj.app.common.core.lang.ArrayUtils.java
@SuppressWarnings("unchecked") public static <E> E[] removeNullElement(E[] array) { if (Argument.isEmptyArray(array)) { logger.debug("ArrayUtils.removeNullElement: array is null!"); return null; }//from w w w . j av a2s.c o m int notNullValueCount = array.length; for (int i = 0, j = array.length; i < j; i++) { if (array[i] == null) { notNullValueCount--; } } if (notNullValueCount == 0) { return null; } E[] newInstance = (E[]) Array.newInstance(array.getClass().getComponentType(), notNullValueCount); for (int i = 0, j = 0; i < array.length; i++) { if (array[i] != null) { newInstance[j++] = array[i]; } } return newInstance; }
From source file:com.khubla.cbean.serializer.impl.json.JSONArrayFieldSerializer.java
@Override public void deserialize(Object o, Field field, String value) throws SerializerException { try {/*from w ww . jav a 2s . co m*/ final JSONArray jsonArray = new JSONArray(value); final Object od = Array.newInstance(field.getType().getComponentType(), jsonArray.length()); PropertyUtils.setProperty(o, field.getName(), od); final Class<?> componentType = field.getType().getComponentType(); if (componentType.isPrimitive()) { for (int i = 0; i < jsonArray.length(); i++) { final String v = jsonArray.getString(i); PropertyUtils.setIndexedProperty(o, field.getName(), i, ConvertUtils.convert(v, field.getType().getComponentType())); } } else { final CBean<Object> cBean = CBeanServer.getInstance().getCBean(componentType); for (int i = 0; i < jsonArray.length(); i++) { final Object co = cBean.load(new CBeanKey(jsonArray.getString(i))); PropertyUtils.setIndexedProperty(o, field.getName(), i, ConvertUtils.convert(co, field.getType().getComponentType())); } } } catch (final Exception e) { throw new SerializerException(e); } }
From source file:Main.java
/** * Produces a new array containing the elements between * the start and end indices.// ww w . ja v a 2 s. c o m * * The start index is inclusive, the end index exclusive. * Null array input produces null output. * * The component type of the subarray is always the same as * that of the input array. Thus, if the input is an array of type * <code>Date</code>, the following usage is envisaged: * * <pre> * Date[] someDates = (Date[])ArrayUtils.subarray(allDates, 2, 5); * </pre> * * @param array the array * @param startIndexInclusive the starting index. Undervalue (<0) * is promoted to 0, overvalue (>array.length) results * in an empty array. * @param endIndexExclusive elements up to endIndex-1 are present in the * returned subarray. Undervalue (< startIndex) produces * empty array, overvalue (>array.length) is demoted to * array length. * @return a new array containing the elements between * the start and end indices. * @since 2.1 */ public static Object[] subarray(Object[] array, int startIndexInclusive, int endIndexExclusive) { if (array == null) { return null; } if (startIndexInclusive < 0) { startIndexInclusive = 0; } if (endIndexExclusive > array.length) { endIndexExclusive = array.length; } int newSize = endIndexExclusive - startIndexInclusive; Class type = array.getClass().getComponentType(); if (newSize <= 0) { return (Object[]) Array.newInstance(type, 0); } Object[] subarray = (Object[]) Array.newInstance(type, newSize); System.arraycopy(array, startIndexInclusive, subarray, 0, newSize); return subarray; }
From source file:com.fengduo.bee.commons.core.lang.ArrayUtils.java
@SuppressWarnings("unchecked") public static <E> E[] removeNullElement(E[] array) { if (Argument.isEmptyArray(array)) { logger.debug("ArrayUtils.removeNullElement array is null!"); return null; }//from w w w . j a v a 2 s . c o m int notNullValueCount = array.length; for (int i = 0, j = array.length; i < j; i++) { if (array[i] == null) { notNullValueCount--; } } if (notNullValueCount == 0) { return null; } E[] newInstance = (E[]) Array.newInstance(array.getClass().getComponentType(), notNullValueCount); for (int i = 0, j = 0; i < array.length; i++) { if (array[i] != null) { newInstance[j++] = array[i]; } } return newInstance; }
From source file:cn.remex.core.util.ObjectUtils.java
/** * Append the given Object to the given array, returning a new array * consisting of the input array contents plus the given Object. * @param array the array to append to (can be <code>null</code>) * @param obj the Object to append/*from w ww . ja v a 2 s . c om*/ * @return the new array (of the same component type; never <code>null</code>) */ public static Object[] addObjectToArray(final Object[] array, final Object obj) { Class<?> compType = Object.class; if (array != null) { compType = array.getClass().getComponentType(); } else if (obj != null) { compType = obj.getClass(); } int newArrLength = array != null ? array.length + 1 : 1; Object[] newArr = (Object[]) Array.newInstance(compType, newArrLength); if (array != null) { System.arraycopy(array, 0, newArr, 0, array.length); } newArr[newArr.length - 1] = obj; return newArr; }
From source file:com.dinstone.ut.faststub.internal.StubMethodInvocation.java
/** * {@inheritDoc}//from www .j av a 2s . co m * */ public Object invoke(Method method, Object[] args) throws Throwable { ApplicationContext stubContext = getStubContext(method); Object retObj = stubContext.getBean(method.getName()); // handle exception handleException(retObj); // handle array object Class<?> retType = method.getReturnType(); if (retType.isArray() && retObj instanceof List<?>) { List<?> list = (List<?>) retObj; int len = list.size(); Object arrObj = Array.newInstance(retType.getComponentType(), len); for (int i = 0; i < len; i++) { Array.set(arrObj, i, list.get(i)); } return arrObj; } return retObj; }