Use Class#arrayType() where feasible

Closes gh-31002
This commit is contained in:
Sam Brannen 2023-08-07 12:07:46 +03:00
parent 6090eb0b42
commit fe5560400c
6 changed files with 9 additions and 14 deletions

View File

@ -149,7 +149,7 @@ class TypeConverterDelegate {
convertedValue = StringUtils.commaDelimitedListToStringArray(text);
}
if (editor == null && String.class != elementType) {
editor = findDefaultEditor(Array.newInstance(elementType, 0).getClass());
editor = findDefaultEditor(elementType.arrayType());
}
}
}

View File

@ -1373,8 +1373,8 @@ public class ResolvableType implements Serializable {
*/
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, componentType, null, null);
Class<?> arrayType = componentType.resolve().arrayType();
return new ResolvableType(arrayType, componentType, null, null);
}
/**

View File

@ -17,7 +17,6 @@
package org.springframework.core.annotation;
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.function.Predicate;
@ -164,8 +163,7 @@ abstract class AbstractMergedAnnotation<A extends Annotation> implements MergedA
@SuppressWarnings("unchecked")
public <E extends Enum<E>> E[] getEnumArray(String attributeName, Class<E> type) {
Assert.notNull(type, "Type must not be null");
Class<?> arrayType = Array.newInstance(type, 0).getClass();
return (E[]) getRequiredAttributeValue(attributeName, arrayType);
return (E[]) getRequiredAttributeValue(attributeName, type.arrayType());
}
@Override

View File

@ -327,8 +327,7 @@ public class AnnotationAttributes extends LinkedHashMap<String, Object> {
*/
@SuppressWarnings("unchecked")
public <A extends Annotation> A[] getAnnotationArray(String attributeName, Class<A> annotationType) {
Object array = Array.newInstance(annotationType, 0);
return (A[]) getRequiredAttribute(attributeName, array.getClass());
return (A[]) getRequiredAttribute(attributeName, annotationType.arrayType());
}
/**

View File

@ -16,7 +16,6 @@
package org.springframework.core.convert.support;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
@ -584,7 +583,7 @@ public class GenericConversionService implements ConfigurableConversionService {
List<Class<?>> hierarchy, Set<Class<?>> visited) {
if (asArray) {
type = Array.newInstance(type, 0).getClass();
type = type.arrayType();
}
if (visited.add(type)) {
hierarchy.add(index, type);

View File

@ -20,7 +20,6 @@ import java.io.Closeable;
import java.io.Externalizable;
import java.io.File;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
@ -273,21 +272,21 @@ public abstract class ClassUtils {
if (name.endsWith(ARRAY_SUFFIX)) {
String elementClassName = name.substring(0, name.length() - ARRAY_SUFFIX.length());
Class<?> elementClass = forName(elementClassName, classLoader);
return Array.newInstance(elementClass, 0).getClass();
return elementClass.arrayType();
}
// "[Ljava.lang.String;" style arrays
if (name.startsWith(NON_PRIMITIVE_ARRAY_PREFIX) && name.endsWith(";")) {
String elementName = name.substring(NON_PRIMITIVE_ARRAY_PREFIX.length(), name.length() - 1);
Class<?> elementClass = forName(elementName, classLoader);
return Array.newInstance(elementClass, 0).getClass();
return elementClass.arrayType();
}
// "[[I" or "[[Ljava.lang.String;" style arrays
if (name.startsWith(INTERNAL_ARRAY_PREFIX)) {
String elementName = name.substring(INTERNAL_ARRAY_PREFIX.length());
Class<?> elementClass = forName(elementName, classLoader);
return Array.newInstance(elementClass, 0).getClass();
return elementClass.arrayType();
}
ClassLoader clToUse = classLoader;