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.skyisland.questmanager.fanciful.ArrayWrapper.java
/** * Converts an iterable element collection to an array of elements. * The iteration order of the specified object will be used as the array element order. * @param list The iterable of objects which will be converted to an array. * @param c The type of the elements of the array. * @return An array of elements in the specified iterable. */// w ww . ja va 2 s . c om @SuppressWarnings("unchecked") public static <T> T[] toArray(Iterable<? extends T> list, Class<T> c) { int size = -1; if (list instanceof Collection<?>) { @SuppressWarnings("rawtypes") Collection coll = (Collection) list; size = coll.size(); } if (size < 0) { size = 0; // Ugly hack: Count it ourselves for (@SuppressWarnings("unused") T element : list) { size++; } } T[] result = (T[]) Array.newInstance(c, size); int i = 0; for (T element : list) { // Assumes iteration order is consistent result[i++] = element; // Assign array element at index THEN increment counter } return result; }
From source file:net.navasoft.madcoin.backend.services.controller.exception.impl.BusinessControllerException.java
/** * Instantiates a new session/*from w w w .j a v a 2 s . c o m*/ * net.navasoft.madcoin.backend.model.controller.impl exception. * * @param exceptionMessages * the exception messages * @param injected * the injected * @param key * the key * @param parameters * the parameters * @since 28/07/2014, 11:34:20 PM */ public BusinessControllerException(final MessageSource exceptionMessages, final Locale injected, final AllowedExceptionMessage key, Object... parameters) { super(exceptionMessages.getMessage(key.getPropertyKey(), literal(parameters), injected)); locatedMessage = key.getPropertyKey(); language = injected; if (parameters != null) { reasonsToFail = (Object[]) Array.newInstance(Object.class, 0); for (Object param : parameters) { reasonsToFail = ArrayUtils.add(reasonsToFail, param); } } }
From source file:com.iterranux.droolsjbpmCore.internal.AbstractGenericFactory.java
/** * Get the underlying class for a type, or null if the type is a variable type. * @param type the type// w ww. j a v a 2 s . c om * @return the underlying class */ public static Class<?> getClass(Type type) { if (type instanceof Class) { return (Class) type; } else if (type instanceof ParameterizedType) { return getClass(((ParameterizedType) type).getRawType()); } else if (type instanceof GenericArrayType) { Type componentType = ((GenericArrayType) type).getGenericComponentType(); Class<?> componentClass = getClass(componentType); if (componentClass != null) { return Array.newInstance(componentClass, 0).getClass(); } else { return null; } } else { return null; } }
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 array {@code toAppend2}. *//*from w w w. ja v a2 s . co 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 + toAppend2.length); System.arraycopy(oldArray, 0, array, 0, oldArray.length); System.arraycopy(toAppend1, 0, array, oldArray.length, toAppend1.length); System.arraycopy(toAppend2, 0, array, oldArray.length + toAppend1.length, toAppend2.length); return array; }
From source file:net.navasoft.madcoin.backend.services.controller.exception.impl.SessionControllerException.java
/** * Instantiates a new session//from ww w . ja va2 s . c o m * net.navasoft.madcoin.backend.model.controller.impl exception. * * @param exceptionMessages * the exception messages * @param injected * the injected * @param key * the key * @param parameters * the parameters * @since 28/07/2014, 11:34:20 PM */ public SessionControllerException(final MessageSource exceptionMessages, final Locale injected, final AllowedExceptionMessage key, Object... parameters) { super(exceptionMessages.getMessage(key.getPropertyKey(), literal(parameters), injected)); locatedMessage = key.getPropertyKey(); language = injected; reasonsToFail = (Object[]) Array.newInstance(Object.class, 0); reasonsToFail = ArrayUtils.add(reasonsToFail, parameters[0]); reasonsToFail = ArrayUtils.add(reasonsToFail, parameters[1]); setAllowedTips(parameters[parameters.length - 1]); formulateTips(); }
From source file:com.browseengine.bobo.serialize.JSONSerializer.java
private static void loadObject(Object array, Class type, int index, JSONArray jsonArray) throws JSONSerializationException, JSONException { if (type.isPrimitive()) { if (type == Integer.TYPE) { Array.setInt(array, index, jsonArray.getInt(index)); } else if (type == Long.TYPE) { Array.setLong(array, index, jsonArray.getInt(index)); } else if (type == Short.TYPE) { Array.setShort(array, index, (short) jsonArray.getInt(index)); } else if (type == Boolean.TYPE) { Array.setBoolean(array, index, jsonArray.getBoolean(index)); } else if (type == Double.TYPE) { Array.setDouble(array, index, jsonArray.getDouble(index)); } else if (type == Float.TYPE) { Array.setFloat(array, index, (float) jsonArray.getDouble(index)); } else if (type == Character.TYPE) { char ch = jsonArray.getString(index).charAt(0); Array.setChar(array, index, ch); } else if (type == Byte.TYPE) { Array.setByte(array, index, (byte) jsonArray.getInt(index)); } else {//from w w w . ja va2 s.c o m throw new JSONSerializationException("Unknown primitive: " + type); } } else if (type == String.class) { Array.set(array, index, jsonArray.getString(index)); } else if (JSONSerializable.class.isAssignableFrom(type)) { JSONObject jObj = jsonArray.getJSONObject(index); JSONSerializable serObj = deSerialize(type, jObj); Array.set(array, index, serObj); } else if (type.isArray()) { Class componentClass = type.getComponentType(); JSONArray subArray = jsonArray.getJSONArray(index); int len = subArray.length(); Object newArray = Array.newInstance(componentClass, len); for (int k = 0; k < len; ++k) { loadObject(newArray, componentClass, k, subArray); } } }
From source file:ReflectUtils.java
/** * // w w w .j a v a 2 s. co m * @param theClass * A "normal", non-array type. * * @return The array version of the given type. For example, if you pass * <em>String.class</em>, you get <em>String[].class</em>. If * you pass <em>int.class</em>, you get <em>int[].class</em>. If * the given class is already an array type, it is returned. * * @see #getClassFromArrayClass(Class) * */ public static Class getArrayClassFromClass(Class theClass) { if (theClass.isArray()) return theClass; return Array.newInstance(theClass, 0).getClass(); }
From source file:edu.duke.cabig.c3pr.accesscontrol.ArrayFilterer.java
/** * //from www . j av a 2 s . c o m * @see org.acegisecurity.afterinvocation.Filterer#getFilteredObject() */ public Object getFilteredObject() { // Recreate an array of same type and filter the removed objects. int originalSize = list.length; int sizeOfResultingList = originalSize - removeList.size(); Object[] filtered = (Object[]) Array.newInstance(list.getClass().getComponentType(), sizeOfResultingList); for (int i = 0, j = 0; i < list.length; i++) { Object object = list[i]; if (!removeList.contains(object)) { filtered[j] = object; j++; } } if (logger.isDebugEnabled()) { logger.debug("Original array contained " + originalSize + " elements; now contains " + sizeOfResultingList + " elements"); } return filtered; }
From source file:jef.tools.ArrayUtils.java
@SuppressWarnings("unchecked") public static <T> T[] toArray(Enumeration<T> e, Class<T> type) { List<T> result = new ArrayList<T>(); for (; e.hasMoreElements();) { result.add(e.nextElement());// w w w .j a v a2 s .c om } return result.toArray((T[]) Array.newInstance(type, result.size())); }
From source file:com.opengamma.analytics.financial.interestrate.annuity.derivative.Annuity.java
/** * @param payments The payments, not null or empty * @param pType The type of the payments, not null * @param isPayer True if the annuity is to be paid *//*from w w w . ja v a2s .c o m*/ public Annuity(final List<? extends P> payments, final Class<P> pType, final boolean isPayer) { ArgumentChecker.noNulls(payments, "payments"); ArgumentChecker.notNull(pType, "type"); ArgumentChecker.isTrue(payments.size() > 0, "Payments size must be greater than zero"); _payments = payments.toArray((P[]) Array.newInstance(pType, 0)); _isPayer = isPayer; }