Browse Source

Use Class#arrayType() where feasible

Closes gh-31002
pull/31012/head
Sam Brannen 2 years ago
parent
commit
fe5560400c
  1. 2
      spring-beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java
  2. 4
      spring-core/src/main/java/org/springframework/core/ResolvableType.java
  3. 4
      spring-core/src/main/java/org/springframework/core/annotation/AbstractMergedAnnotation.java
  4. 3
      spring-core/src/main/java/org/springframework/core/annotation/AnnotationAttributes.java
  5. 3
      spring-core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java
  6. 7
      spring-core/src/main/java/org/springframework/util/ClassUtils.java

2
spring-beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java

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

4
spring-core/src/main/java/org/springframework/core/ResolvableType.java

@ -1373,8 +1373,8 @@ public class ResolvableType implements Serializable {
*/ */
public static ResolvableType forArrayComponent(ResolvableType componentType) { public static ResolvableType forArrayComponent(ResolvableType componentType) {
Assert.notNull(componentType, "Component type must not be null"); Assert.notNull(componentType, "Component type must not be null");
Class<?> arrayClass = Array.newInstance(componentType.resolve(), 0).getClass(); Class<?> arrayType = componentType.resolve().arrayType();
return new ResolvableType(arrayClass, componentType, null, null); return new ResolvableType(arrayType, componentType, null, null);
} }
/** /**

4
spring-core/src/main/java/org/springframework/core/annotation/AbstractMergedAnnotation.java

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

3
spring-core/src/main/java/org/springframework/core/annotation/AnnotationAttributes.java

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

3
spring-core/src/main/java/org/springframework/core/convert/support/GenericConversionService.java

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

7
spring-core/src/main/java/org/springframework/util/ClassUtils.java

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

Loading…
Cancel
Save