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.wavemaker.tools.apidocs.tools.parser.util.TypeUtil.java
protected static TypeInformation getArrayTypeInformation(Type type) { Class<?> actualType;//www .j a v a2 s. co m List<Class<?>> typeArguments = new LinkedList<>(); if (type instanceof GenericArrayType) { // for cases like T[] Type rawType = ((GenericArrayType) type).getGenericComponentType(); Class<?> rawComponentType = extractTypeInformation(rawType).getActualType(); actualType = Array.newInstance(rawComponentType, 0).getClass(); // instantiating array type typeArguments.add(rawComponentType); } else { actualType = TypeUtils.getRawType(type, null); typeArguments.add((Class<?>) TypeUtils.getArrayComponentType(type));// check type. } return new TypeInformation(actualType, typeArguments, true, type); }
From source file:bb.mcmc.analysis.ConvergeStatUtils.java
protected static <T> T[] thinWindow(T[] array, int kthin) { if (kthin == 1) { return array; } else {/*from w w w. j a v a2 s .c o m*/ final int count = (int) Math.ceil(array.length / (double) kthin); final Class<?> type = array.getClass().getComponentType(); @SuppressWarnings("unchecked") // OK, because array is of type T final T[] newArray = (T[]) Array.newInstance(type, count); for (int i = 0; i < newArray.length; i++) { newArray[i] = array[kthin * i]; } return newArray; } }
From source file:Main.java
public static <T> T[] concat(T[] first, T[] second) { @SuppressWarnings("unchecked") final T[] result = (T[]) Array.newInstance(first.getClass().getComponentType(), first.length + second.length); System.arraycopy(first, 0, result, 0, first.length); System.arraycopy(second, 0, result, first.length, second.length); return result; }
From source file:com.strider.datadefender.requirement.Parameter.java
private Object getArrayForValues(final List<Object> list, final String type) throws ClassNotFoundException { String dataType = type;/*from w ww . jav a 2 s . c o m*/ if ("String".equals(dataType)) { dataType = "java.lang.String"; } final Class<?> c = ClassUtils.getClass(dataType); if (c.isPrimitive()) { final Class<?> w = ClassUtils.primitiveToWrapper(c); Object array = Array.newInstance(w, list.size()); array = list.toArray((Object[]) array); if (c == boolean.class) { return ArrayUtils.toPrimitive((Boolean[]) array); } else if (c == byte.class) { return ArrayUtils.toPrimitive((Byte[]) array); } else if (c == short.class) { return ArrayUtils.toPrimitive((Short[]) array); } else if (c == char.class) { return ArrayUtils.toPrimitive((Character[]) array); } else if (c == int.class) { return ArrayUtils.toPrimitive((Integer[]) array); } else if (c == long.class) { return ArrayUtils.toPrimitive((Long[]) array); } else if (c == float.class) { return ArrayUtils.toPrimitive((Float[]) array); } else if (c == double.class) { return ArrayUtils.toPrimitive((Double[]) array); } throw new IllegalArgumentException("Unhandled primitive type: " + c.getName()); } final Object array = Array.newInstance(c, list.size()); return list.toArray((Object[]) array); }
From source file:edu.sdsc.scigraph.neo4j.GraphUtil.java
static Object getNewPropertyValue(Object originalValue, Object newValue) { Class<?> clazz = checkNotNull(newValue).getClass(); boolean reduceToString = false; if (null != originalValue && originalValue.getClass().isArray()) { Class<?> originalClazz = Array.get(originalValue, 0).getClass(); if (!originalClazz.equals(clazz)) { reduceToString = true;/* w w w. j av a2 s . c om*/ clazz = String.class; } newValue = reduceToString ? newValue.toString() : newValue; Object newArray = Array.newInstance(clazz, Array.getLength(originalValue) + 1); for (int i = 0; i < Array.getLength(originalValue); i++) { Object val = Array.get(originalValue, i); if (newValue.equals(val)) { return originalValue; } Array.set(newArray, i, reduceToString ? val.toString() : val); } Array.set(newArray, Array.getLength(originalValue), newValue); return newArray; } else if (null != originalValue) { if (!clazz.equals(originalValue.getClass())) { reduceToString = true; clazz = String.class; } originalValue = reduceToString ? originalValue.toString() : originalValue; newValue = reduceToString ? newValue.toString() : newValue; if (!originalValue.equals(newValue)) { Object newArray = Array.newInstance(clazz, 2); Array.set(newArray, 0, originalValue); Array.set(newArray, 1, newValue); return newArray; } else { return originalValue; } } else { return newValue; } }
From source file:com.scit.sling.test.MockValueMap.java
@SuppressWarnings("unchecked") private <T> T convertType(Object o, Class<T> type) { if (o == null) { return null; }/*from ww w . java 2s . c o m*/ if (o.getClass().isArray() && type.isArray()) { if (type.getComponentType().isAssignableFrom(o.getClass().getComponentType())) { return (T) o; } else { // we need to convert the elements in the array Object array = Array.newInstance(type.getComponentType(), Array.getLength(o)); for (int i = 0; i < Array.getLength(o); i++) { Array.set(array, i, convertType(Array.get(o, i), type.getComponentType())); } return (T) array; } } if (o.getClass().isAssignableFrom(type)) { return (T) o; } if (String.class.isAssignableFrom(type)) { // Format dates if (o instanceof Calendar) { return (T) formatDate((Calendar) o); } else if (o instanceof Date) { return (T) formatDate((Date) o); } else if (o instanceof DateTime) { return (T) formatDate((DateTime) o); } return (T) String.valueOf(o); } else if (o instanceof DateTime) { DateTime dt = (DateTime) o; if (Calendar.class.isAssignableFrom(type)) { return (T) dt.toCalendar(Locale.getDefault()); } else if (Date.class.isAssignableFrom(type)) { return (T) dt.toDate(); } else if (Number.class.isAssignableFrom(type)) { return convertType(dt.getMillis(), type); } } else if (o instanceof Number && Number.class.isAssignableFrom(type)) { if (Byte.class.isAssignableFrom(type)) { return (T) (Byte) ((Number) o).byteValue(); } else if (Double.class.isAssignableFrom(type)) { return (T) (Double) ((Number) o).doubleValue(); } else if (Float.class.isAssignableFrom(type)) { return (T) (Float) ((Number) o).floatValue(); } else if (Integer.class.isAssignableFrom(type)) { return (T) (Integer) ((Number) o).intValue(); } else if (Long.class.isAssignableFrom(type)) { return (T) (Long) ((Number) o).longValue(); } else if (Short.class.isAssignableFrom(type)) { return (T) (Short) ((Number) o).shortValue(); } else if (BigDecimal.class.isAssignableFrom(type)) { return (T) new BigDecimal(o.toString()); } } else if (o instanceof Number && type.isPrimitive()) { final Number num = (Number) o; if (type == byte.class) { return (T) new Byte(num.byteValue()); } else if (type == double.class) { return (T) new Double(num.doubleValue()); } else if (type == float.class) { return (T) new Float(num.floatValue()); } else if (type == int.class) { return (T) new Integer(num.intValue()); } else if (type == long.class) { return (T) new Long(num.longValue()); } else if (type == short.class) { return (T) new Short(num.shortValue()); } } else if (o instanceof String && Number.class.isAssignableFrom(type)) { if (Byte.class.isAssignableFrom(type)) { return (T) new Byte((String) o); } else if (Double.class.isAssignableFrom(type)) { return (T) new Double((String) o); } else if (Float.class.isAssignableFrom(type)) { return (T) new Float((String) o); } else if (Integer.class.isAssignableFrom(type)) { return (T) new Integer((String) o); } else if (Long.class.isAssignableFrom(type)) { return (T) new Long((String) o); } else if (Short.class.isAssignableFrom(type)) { return (T) new Short((String) o); } else if (BigDecimal.class.isAssignableFrom(type)) { return (T) new BigDecimal((String) o); } } throw new NotImplementedException( "Can't handle conversion from " + o.getClass().getName() + " to " + type.getName()); }
From source file:com.carmanconsulting.cassidy.pojo.assembly.step.ArrayStep.java
@Override public void execute(AssemblyStack assemblyStack) { final List<Object> elements = assemblyStack.drain(); Object array = Array.newInstance(elementType, elements.size()); for (int i = 0; i < elements.size(); i++) { Object element = elements.get(i); Array.set(array, i, element); }// w w w. ja va 2 s .c o m assemblyStack.push(array); }
From source file:com.swordlord.gozer.eventhandler.generic.GozerEventListenerList.java
@SuppressWarnings("unchecked") public <T extends GozerEventListener> T[] getListeners(Class<T> t) { Object[] lList = listenerList; int nbr = getListenerCount(lList, t); T[] result = (T[]) Array.newInstance(t, nbr); int j = 0;// w ww.ja va 2s .co m for (int i = lList.length - 2; i >= 0; i -= 2) { if (lList[i].equals(t)) { result[j++] = (T) lList[i + 1]; } } return result; }
From source file:Main.java
@SuppressWarnings("unchecked") public static <T> T[] merge(T[] c1, T[] c2) { if (c1 == null || c1.length == 0) { return c2; }// w ww .ja v a 2 s . co m if (c2 == null || c2.length == 0) { return c1; } Class<?> t; if (c1.getClass().isAssignableFrom(c2.getClass())) { t = c1.getClass().getComponentType(); } else if (c2.getClass().isAssignableFrom(c1.getClass())) { t = c2.getClass().getComponentType(); } else { throw new IllegalStateException("Can not merge " + c1.getClass().getCanonicalName() + " and " + c2.getClass().getCanonicalName()); } T[] all = (T[]) Array.newInstance(t, c1.length + c2.length); System.arraycopy(c1, 0, all, 0, c1.length); System.arraycopy(c2, 0, all, c1.length, c2.length); return all; }
From source file:net.radai.beanz.util.ReflectionUtil.java
public static Object instatiateArray(Type elementType, int size) { Class<?> elementClass = erase(elementType); return Array.newInstance(elementClass, size); }