本文整理了Java中java.lang.reflect.Array.newInstance()
方法的一些代码示例,展示了Array.newInstance()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Array.newInstance()
方法的具体详情如下:
包路径:java.lang.reflect.Array
类名称:Array
方法名:newInstance
[英]Returns a new array of the specified component type and length. Equivalent to new componentType[size].
[中]返回指定组件类型和长度的新数组。等效于新的componentType[尺寸]。
代码示例来源:origin: apache/incubator-dubbo
protected Object[] createArray(int length) {
if (_componentType != null)
return (Object[]) Array.newInstance(_componentType, length);
else
return new Object[length];
}
代码示例来源:origin: libgdx/libgdx
/** 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);
}
代码示例来源:origin: libgdx/libgdx
/** 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);
}
代码示例来源:origin: google/guava
/** 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();
}
代码示例来源:origin: spring-projects/spring-framework
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;
}
代码示例来源:origin: apache/incubator-dubbo
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;
}
代码示例来源:origin: google/guava
/**
* 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;
}
代码示例来源:origin: google/guava
private static <T> T createEmptyArray(Class<T> arrayType) {
return arrayType.cast(Array.newInstance(arrayType.getComponentType(), 0));
}
代码示例来源:origin: spring-projects/spring-framework
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);
}
}
代码示例来源:origin: spring-projects/spring-framework
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;
}
代码示例来源:origin: org.mockito/mockito-core
Object returnValueFor(Class<?> type) {
if (type == String.class) {
return "";
} else if (type.isArray()) {
Class<?> componentType = type.getComponentType();
return Array.newInstance(componentType, 0);
}
return null;
}
}
代码示例来源:origin: google/guava
/**
* 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);
}
代码示例来源:origin: spring-projects/spring-framework
@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;
}
代码示例来源:origin: spring-projects/spring-framework
/**
* 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;
}
代码示例来源:origin: google/guava
/**
* 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;
}
代码示例来源:origin: spring-projects/spring-framework
/**
* 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);
}
代码示例来源:origin: spring-projects/spring-framework
@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;
}
代码示例来源:origin: org.mockito/mockito-core
@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] typedArray) {
T[] array = typedArray.length >= size() ? typedArray : (T[]) newInstance(typedArray.getClass().getComponentType(), size());
return unwrapTo(array);
}
代码示例来源:origin: spring-projects/spring-framework
@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();
}
代码示例来源:origin: jenkinsci/jenkins
@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;
}
内容来源于网络,如有侵权,请联系作者删除!