Skip to content

Commit 2209e7c

Browse files
committed
Reuse empty class array constant in ClassUtils
Closes gh-24221
1 parent fdc6031 commit 2209e7c

File tree

2 files changed

+11
-14
lines changed

2 files changed

+11
-14
lines changed

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ public abstract class ClassUtils {
6565
/** Prefix for internal non-primitive array class names: {@code "[L"}. */
6666
private static final String NON_PRIMITIVE_ARRAY_PREFIX = "[L";
6767

68+
/** A reusable empty class array constant. */
69+
private static final Class<?>[] EMPTY_CLASS_ARRAY = {};
70+
6871
/** The package separator character: {@code '.'}. */
6972
private static final char PACKAGE_SEPARATOR = '.';
7073

@@ -543,17 +546,12 @@ public static boolean isAssignable(Class<?> lhsType, Class<?> rhsType) {
543546
}
544547
if (lhsType.isPrimitive()) {
545548
Class<?> resolvedPrimitive = primitiveWrapperTypeMap.get(rhsType);
546-
if (lhsType == resolvedPrimitive) {
547-
return true;
548-
}
549+
return (lhsType == resolvedPrimitive);
549550
}
550551
else {
551552
Class<?> resolvedWrapper = primitiveTypeToWrapperMap.get(rhsType);
552-
if (resolvedWrapper != null && lhsType.isAssignableFrom(resolvedWrapper)) {
553-
return true;
554-
}
553+
return (resolvedWrapper != null && lhsType.isAssignableFrom(resolvedWrapper));
555554
}
556-
return false;
557555
}
558556

559557
/**
@@ -681,8 +679,8 @@ public static String classNamesToString(@Nullable Collection<Class<?>> classes)
681679
* @since 3.1
682680
* @see StringUtils#toStringArray
683681
*/
684-
public static Class<?>[] toClassArray(Collection<Class<?>> collection) {
685-
return collection.toArray(new Class<?>[0]);
682+
public static Class<?>[] toClassArray(@Nullable Collection<Class<?>> collection) {
683+
return (!CollectionUtils.isEmpty(collection) ? collection.toArray(EMPTY_CLASS_ARRAY) : EMPTY_CLASS_ARRAY);
686684
}
687685

688686
/**
@@ -1062,7 +1060,7 @@ public static String getQualifiedMethodName(Method method, @Nullable Class<?> cl
10621060
* @param clazz the clazz to analyze
10631061
* @param paramTypes the parameter types of the method
10641062
* @return whether the class has a corresponding constructor
1065-
* @see Class#getMethod
1063+
* @see Class#getConstructor
10661064
*/
10671065
public static boolean hasConstructor(Class<?> clazz, Class<?>... paramTypes) {
10681066
return (getConstructorIfAvailable(clazz, paramTypes) != null);

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -232,16 +232,15 @@ private static EntityManager createProxy(
232232

233233
if (emIfc != null) {
234234
interfaces = cachedEntityManagerInterfaces.computeIfAbsent(emIfc, key -> {
235-
Set<Class<?>> ifcs = new LinkedHashSet<>();
235+
Set<Class<?>> ifcs = new LinkedHashSet<>(4);
236236
ifcs.add(key);
237237
ifcs.add(EntityManagerProxy.class);
238238
return ClassUtils.toClassArray(ifcs);
239239
});
240240
}
241241
else {
242242
interfaces = cachedEntityManagerInterfaces.computeIfAbsent(rawEm.getClass(), key -> {
243-
Set<Class<?>> ifcs = new LinkedHashSet<>(ClassUtils
244-
.getAllInterfacesForClassAsSet(key, cl));
243+
Set<Class<?>> ifcs = new LinkedHashSet<>(ClassUtils.getAllInterfacesForClassAsSet(key, cl));
245244
ifcs.add(EntityManagerProxy.class);
246245
return ClassUtils.toClassArray(ifcs);
247246
});

0 commit comments

Comments
 (0)