protected Object[] createArray(int length) { if (_componentType != null) return (Object[]) Array.newInstance(_componentType, length); else return new Object[length]; }
/** Creates a new array with the specified component type and length. */ static public Object newInstance (Class c, int size) { return java.lang.reflect.Array.newInstance(c, size); }
/** Creates a new array with the specified component type and length. */ static public Object newInstance (Class c, int size) { return java.lang.reflect.Array.newInstance(c, size); }
/** Returns the {@code Class} object of arrays with {@code componentType}. */ static Class<?> getArrayClass(Class<?> componentType) { // TODO(user): This is not the most efficient way to handle generic // arrays, but is there another way to extract the array class in a // non-hacky way (i.e. using String value class names- "[L...")? return Array.newInstance(componentType, 0).getClass(); }
private Class<?> makeArrayIfNecessary(Class<?> clazz) { if (this.dimensions != 0) { for (int i = 0; i < this.dimensions; i++) { Object array = Array.newInstance(clazz, 0); clazz = array.getClass(); } } return clazz; }
public ArrayDeserializer(Class componentType) { _componentType = componentType; if (_componentType != null) { try { _type = Array.newInstance(_componentType, 0).getClass(); } catch (Exception e) { } } if (_type == null) _type = Object[].class; }
/** * Returns a new array of the given length with the same type as a reference array. * * @param reference any array of the desired type * @param length the length of the new array */ static <T> T[] newArray(T[] reference, int length) { Class<?> type = reference.getClass().getComponentType(); // the cast is safe because // result.getClass() == reference.getClass().getComponentType() @SuppressWarnings("unchecked") T[] result = (T[]) Array.newInstance(type, length); return result; }
private static <T> T createEmptyArray(Class<T> arrayType) { return arrayType.cast(Array.newInstance(arrayType.getComponentType(), 0)); }
private void addToClassHierarchy(int index, Class<?> type, boolean asArray, List<Class<?>> hierarchy, Set<Class<?>> visited) { if (asArray) { type = Array.newInstance(type, 0).getClass(); } if (visited.add(type)) { hierarchy.add(index, type); } }
private Object convertDataArrayToTargetArray(Object[] array, Class<?> targetClass) throws NoSuchMethodException { Class<?> targetType = targetClass.getComponentType(); Method fromMethod = targetType.getMethod("from", array.getClass().getComponentType()); Object resultArray = Array.newInstance(targetType, array.length); for (int i = 0; i < array.length; i++) { Array.set(resultArray, i, ReflectionUtils.invokeMethod(fromMethod, null, array[i])); } return resultArray; }
Object returnValueFor(Class<?> type) { if (type == String.class) { return ""; } else if (type.isArray()) { Class<?> componentType = type.getComponentType(); return Array.newInstance(componentType, 0); } return null; } }
/** * Returns a new array of the given length with the specified component type. * * @param type the component type * @param length the length of the new array */ @GwtIncompatible // Array.newInstance(Class, int) @SuppressWarnings("unchecked") public static <T> T[] newArray(Class<T> type, int length) { return (T[]) Array.newInstance(type, length); }
@SuppressWarnings("unchecked") private static <T> T[] copyPropertiesToBeanArray(Collection<? extends Annotation> anns, Class<T> beanClass) { T[] beans = (T[]) Array.newInstance(beanClass, anns.size()); int i = 0; for (Annotation ann : anns) { beans[i++] = copyPropertiesToBean(ann, beanClass); } return beans; }
/** * For each element in the managed array, resolve reference if necessary. */ private Object resolveManagedArray(Object argName, List<?> ml, Class<?> elementType) { Object resolved = Array.newInstance(elementType, ml.size()); for (int i = 0; i < ml.size(); i++) { Array.set(resolved, i, resolveValueIfNecessary(new KeyedArgName(argName, i), ml.get(i))); } return resolved; }
/** * Returns a two-dimensional array with the table contents. The row and column indices correspond * to the positions of the row and column in the iterables provided during table construction. If * the table lacks a mapping for a given row and column, the corresponding array element is null. * * <p>Subsequent table changes will not modify the array, and vice versa. * * @param valueClass class of values stored in the returned array */ @GwtIncompatible // reflection public V[][] toArray(Class<V> valueClass) { @SuppressWarnings("unchecked") // TODO: safe? V[][] copy = (V[][]) Array.newInstance(valueClass, rowList.size(), columnList.size()); for (int i = 0; i < rowList.size(); i++) { System.arraycopy(array[i], 0, copy[i], 0, array[i].length); } return copy; }
/** * Return a {@link ResolvableType} as a array of the specified {@code componentType}. * @param componentType the component type * @return a {@link ResolvableType} as an array of the specified component type */ public static ResolvableType forArrayComponent(ResolvableType componentType) { Assert.notNull(componentType, "Component type must not be null"); Class<?> arrayClass = Array.newInstance(componentType.resolve(), 0).getClass(); return new ResolvableType(arrayClass, null, null, componentType); }
@Override @Nullable public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { if (source == null) { return null; } TypeDescriptor targetElementType = targetType.getElementTypeDescriptor(); Assert.state(targetElementType != null, "No target element type"); Object target = Array.newInstance(targetElementType.getType(), 1); Object targetElement = this.conversionService.convert(source, sourceType, targetElementType); Array.set(target, 0, targetElement); return target; }
@SuppressWarnings("unchecked") public <T> T[] toArray(T[] typedArray) { T[] array = typedArray.length >= size() ? typedArray : (T[]) newInstance(typedArray.getClass().getComponentType(), size()); return unwrapTo(array); }
@Nullable private Class<?> resolveClass() { if (this.type == EmptyType.INSTANCE) { return null; } if (this.type instanceof Class) { return (Class<?>) this.type; } if (this.type instanceof GenericArrayType) { Class<?> resolvedComponent = getComponentType().resolve(); return (resolvedComponent != null ? Array.newInstance(resolvedComponent, 0).getClass() : null); } return resolveType().resolve(); }
@Override @SuppressWarnings("unchecked") // cast to T[] public boolean configure(StaplerRequest req, JSONObject json) throws FormException { setInstallations(req.bindJSONToList(clazz, json.get("tool")).toArray((T[]) Array.newInstance(clazz, 0))); return true; }