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.opengamma.analytics.math.curve.ObjectsCurve.java
/** * //from www.j a va 2s .com * @param xData A list of <i>x</i> data, not null * @param yData A list of <i>y</i> data, not null, contains same number of entries as <i>x</i> * @param isSorted Is the <i>x</i>-data sorted * @param name The name of the curve */ public ObjectsCurve(final List<T> xData, final List<U> yData, final boolean isSorted, final String name) { super(name); ArgumentChecker.notNull(xData, "x data"); ArgumentChecker.notNull(yData, "y data"); _n = xData.size(); ArgumentChecker.isTrue(_n == yData.size(), "size of x data {} does not match size of y data {}", _n, yData.size()); _xData = xData.toArray((T[]) Array.newInstance(xData.get(0).getClass(), 0)); _yData = yData.toArray((U[]) Array.newInstance(yData.get(0).getClass(), 0)); if (!isSorted) { ParallelArrayBinarySort.parallelBinarySort(_xData, _yData); } }
From source file:de.javakaffee.web.msm.serializer.javolution.AaltoTranscoderTest.java
@Test(enabled = false) void testInstantiate() throws InstantiationException, IllegalAccessException, ClassNotFoundException { final Object instance = Class .forName("de.javakaffee.web.msm.MemcachedBackupSessionManager$MemcachedBackupSession") .newInstance();/*from w w w . j a v a 2s . c om*/ System.out.println("got instance: " + instance); System.out.println(ConcurrentHashMap.class.isAssignableFrom(Map.class)); System.out.println(Map.class.isAssignableFrom(ConcurrentHashMap.class)); System.out.println(Integer.class.isAssignableFrom(Number.class)); System.out.println(Number.class.isAssignableFrom(Integer.class)); System.out.println(String[].class.isArray()); final String[] foo = new String[] { "foo" }; System.out.println(foo.getClass().getComponentType()); System.out.println(((String[]) Array.newInstance(Class.forName("java.lang.String"), 0)).length); System.out.println(Class.forName("java.util.Arrays$ArrayList")); System.out .println(Class.forName("de.javakaffee.web.msm.serializer.xstream.JavolutionTranscoderTest$Person")); }
From source file:com.g3net.tool.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</code> source value will be converted to an * empty Object array.//from ww w. j av a 2 s . c o m * @param source the (potentially primitive) array * @return the corresponding object array (never <code>null</code>) * @throws IllegalArgumentException if the parameter is not an array */ 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:com.day.cq.wcm.foundation.forms.MergedValueMap.java
/** * Converts the object to an array of the given type * @param obj tje object or object array * @param type the component type of the array * @return and array of type T/*from ww w . ja v a 2 s .com*/ */ private <T> T[] convertToArray(Object obj, Class<T> type) { List<T> values = new LinkedList<T>(); if (obj.getClass().isArray()) { for (Object o : (Object[]) obj) { values.add(convert(o, type)); } } else { values.add(convert(obj, type)); } @SuppressWarnings("unchecked") T[] result = (T[]) Array.newInstance(type, values.size()); return values.toArray(result); }
From source file:com.miranteinfo.seam.core.Init.java
@SuppressWarnings("unchecked") private <T> T[] addToArray(Class<T> type, T listener, T[] listeners) { T[] newArray = (T[]) Array.newInstance(type, listeners.length + 1); for (int i = 0; i < listeners.length; i++) newArray[i] = listeners[i];//from w w w . ja v a 2 s . c om newArray[listeners.length] = listener; return newArray; }
From source file:com.codebullets.sagalib.storage.MemoryStorageTest.java
private TestSagaState buildState(Set<String> instanceKeys) { String[] keys = (String[]) Array.newInstance(String.class, instanceKeys.size()); Iterator<String> iterator = instanceKeys.iterator(); for (int i = 0; i < instanceKeys.size() && iterator.hasNext(); ++i) { keys[i] = iterator.next();//from ww w . ja v a 2s.com } return buildState(keys); }
From source file:org.crazydog.util.spring.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}) * @param obj the object to append//from ww w . j a v a 2s.co m * @return the new array (of the same component type; never {@code null}) */ public static <A, O extends A> A[] addObjectToArray(A[] array, O 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); @SuppressWarnings("unchecked") A[] newArr = (A[]) 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.jilk.ros.rosbridge.implementation.JSON.java
private static Object convertJSONArrayToArray(JSONArray ja, Class c, Registry<Class> r) { Object result = Array.newInstance(c, ja.size()); for (int i = 0; i < ja.size(); i++) { Object lookup = ja.get(i); Object value = null;/*from www . j a v a2 s .com*/ if (lookup != null) { if (lookup.getClass().equals(JSONObject.class)) value = convertJSONObjectToMessage((JSONObject) lookup, c, r); else if (lookup.getClass().equals(JSONArray.class)) // this is not actually allowed in ROS value = convertJSONArrayToArray((JSONArray) lookup, c.getComponentType(), r); else value = convertJSONPrimitiveToPrimitive(lookup, c); Array.set(result, i, value); } } return result; }
From source file:net.sourceforge.vulcan.web.struts.actions.ManagePluginAction.java
public final ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { final PluginConfigForm configForm = (PluginConfigForm) form; final String target = configForm.getTarget(); final BeanWrapper bw = new BeanWrapperImpl(form); final Class<?> type = PropertyUtils.getPropertyType(form, target).getComponentType(); final int i; Object[] array = (Object[]) bw.getPropertyValue(target); if (array == null) { array = (Object[]) Array.newInstance(type, 1); i = 0;/* ww w. j a v a 2 s.c om*/ } else { i = array.length; Object[] tmp = (Object[]) Array.newInstance(type, i + 1); System.arraycopy(array, 0, tmp, 0, i); array = tmp; } array[i] = stateManager.getPluginManager().createObject(configForm.getPluginId(), type.getName()); bw.setPropertyValue(target, array); configForm.setFocus(target + "[" + i + "]"); configForm.introspect(request); setHelpAttributes(request, configForm); return mapping.findForward("configure"); }
From source file:com.microsoft.tfs.core.internal.db.DBStatement.java
/** * Convenience query that returns an array of a primitive component type. *//* www . jav a 2s . c o m*/ public Object executeQueryForPrimitiveArray(final Object params, final Class primitiveType) { if (primitiveType == null || !primitiveType.isPrimitive()) { throw new IllegalArgumentException("primitiveType must be a non-null primitive type"); //$NON-NLS-1$ } final List results = new ArrayList(); executeQuery(params, new ResultHandler() { @Override public void handleRow(final ResultSet rset) throws SQLException { results.add(rset.getObject(1)); } }); final int len = results.size(); final Object returnArray = Array.newInstance(primitiveType, len); for (int i = 0; i < len; i++) { Array.set(returnArray, i, results.get(i)); } return returnArray; }