Browse Source

Reuse empty class array constant in ClassUtils

Closes gh-24221
pull/24766/head
Juergen Hoeller 6 years ago
parent
commit
2209e7cb92
  1. 18
      spring-core/src/main/java/org/springframework/util/ClassUtils.java
  2. 7
      spring-orm/src/main/java/org/springframework/orm/jpa/ExtendedEntityManagerCreator.java

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

@ -65,6 +65,9 @@ public abstract class ClassUtils {
/** Prefix for internal non-primitive array class names: {@code "[L"}. */ /** Prefix for internal non-primitive array class names: {@code "[L"}. */
private static final String NON_PRIMITIVE_ARRAY_PREFIX = "[L"; private static final String NON_PRIMITIVE_ARRAY_PREFIX = "[L";
/** A reusable empty class array constant. */
private static final Class<?>[] EMPTY_CLASS_ARRAY = {};
/** The package separator character: {@code '.'}. */ /** The package separator character: {@code '.'}. */
private static final char PACKAGE_SEPARATOR = '.'; private static final char PACKAGE_SEPARATOR = '.';
@ -543,17 +546,12 @@ public abstract class ClassUtils {
} }
if (lhsType.isPrimitive()) { if (lhsType.isPrimitive()) {
Class<?> resolvedPrimitive = primitiveWrapperTypeMap.get(rhsType); Class<?> resolvedPrimitive = primitiveWrapperTypeMap.get(rhsType);
if (lhsType == resolvedPrimitive) { return (lhsType == resolvedPrimitive);
return true;
}
} }
else { else {
Class<?> resolvedWrapper = primitiveTypeToWrapperMap.get(rhsType); Class<?> resolvedWrapper = primitiveTypeToWrapperMap.get(rhsType);
if (resolvedWrapper != null && lhsType.isAssignableFrom(resolvedWrapper)) { return (resolvedWrapper != null && lhsType.isAssignableFrom(resolvedWrapper));
return true;
}
} }
return false;
} }
/** /**
@ -681,8 +679,8 @@ public abstract class ClassUtils {
* @since 3.1 * @since 3.1
* @see StringUtils#toStringArray * @see StringUtils#toStringArray
*/ */
public static Class<?>[] toClassArray(Collection<Class<?>> collection) { public static Class<?>[] toClassArray(@Nullable Collection<Class<?>> collection) {
return collection.toArray(new Class<?>[0]); return (!CollectionUtils.isEmpty(collection) ? collection.toArray(EMPTY_CLASS_ARRAY) : EMPTY_CLASS_ARRAY);
} }
/** /**
@ -1062,7 +1060,7 @@ public abstract class ClassUtils {
* @param clazz the clazz to analyze * @param clazz the clazz to analyze
* @param paramTypes the parameter types of the method * @param paramTypes the parameter types of the method
* @return whether the class has a corresponding constructor * @return whether the class has a corresponding constructor
* @see Class#getMethod * @see Class#getConstructor
*/ */
public static boolean hasConstructor(Class<?> clazz, Class<?>... paramTypes) { public static boolean hasConstructor(Class<?> clazz, Class<?>... paramTypes) {
return (getConstructorIfAvailable(clazz, paramTypes) != null); return (getConstructorIfAvailable(clazz, paramTypes) != null);

7
spring-orm/src/main/java/org/springframework/orm/jpa/ExtendedEntityManagerCreator.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -232,7 +232,7 @@ public abstract class ExtendedEntityManagerCreator {
if (emIfc != null) { if (emIfc != null) {
interfaces = cachedEntityManagerInterfaces.computeIfAbsent(emIfc, key -> { interfaces = cachedEntityManagerInterfaces.computeIfAbsent(emIfc, key -> {
Set<Class<?>> ifcs = new LinkedHashSet<>(); Set<Class<?>> ifcs = new LinkedHashSet<>(4);
ifcs.add(key); ifcs.add(key);
ifcs.add(EntityManagerProxy.class); ifcs.add(EntityManagerProxy.class);
return ClassUtils.toClassArray(ifcs); return ClassUtils.toClassArray(ifcs);
@ -240,8 +240,7 @@ public abstract class ExtendedEntityManagerCreator {
} }
else { else {
interfaces = cachedEntityManagerInterfaces.computeIfAbsent(rawEm.getClass(), key -> { interfaces = cachedEntityManagerInterfaces.computeIfAbsent(rawEm.getClass(), key -> {
Set<Class<?>> ifcs = new LinkedHashSet<>(ClassUtils Set<Class<?>> ifcs = new LinkedHashSet<>(ClassUtils.getAllInterfacesForClassAsSet(key, cl));
.getAllInterfacesForClassAsSet(key, cl));
ifcs.add(EntityManagerProxy.class); ifcs.add(EntityManagerProxy.class);
return ClassUtils.toClassArray(ifcs); return ClassUtils.toClassArray(ifcs);
}); });

Loading…
Cancel
Save