Skip to content

Commit

Permalink
Reuse empty class array constant in ClassUtils
Browse files Browse the repository at this point in the history
Closes gh-24221
  • Loading branch information
jhoeller committed Mar 23, 2020
1 parent fdc6031 commit 2209e7c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
18 changes: 8 additions & 10 deletions spring-core/src/main/java/org/springframework/util/ClassUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ public abstract class ClassUtils {
/** Prefix for internal non-primitive array class names: {@code "[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 '.'}. */
private static final char PACKAGE_SEPARATOR = '.';

Expand Down Expand Up @@ -543,17 +546,12 @@ public static boolean isAssignable(Class<?> lhsType, Class<?> rhsType) {
}
if (lhsType.isPrimitive()) {
Class<?> resolvedPrimitive = primitiveWrapperTypeMap.get(rhsType);
if (lhsType == resolvedPrimitive) {
return true;
}
return (lhsType == resolvedPrimitive);
}
else {
Class<?> resolvedWrapper = primitiveTypeToWrapperMap.get(rhsType);
if (resolvedWrapper != null && lhsType.isAssignableFrom(resolvedWrapper)) {
return true;
}
return (resolvedWrapper != null && lhsType.isAssignableFrom(resolvedWrapper));
}
return false;
}

/**
Expand Down Expand Up @@ -681,8 +679,8 @@ public static String classNamesToString(@Nullable Collection<Class<?>> classes)
* @since 3.1
* @see StringUtils#toStringArray
*/
public static Class<?>[] toClassArray(Collection<Class<?>> collection) {
return collection.toArray(new Class<?>[0]);
public static Class<?>[] toClassArray(@Nullable Collection<Class<?>> collection) {
return (!CollectionUtils.isEmpty(collection) ? collection.toArray(EMPTY_CLASS_ARRAY) : EMPTY_CLASS_ARRAY);
}

/**
Expand Down Expand Up @@ -1062,7 +1060,7 @@ public static String getQualifiedMethodName(Method method, @Nullable Class<?> cl
* @param clazz the clazz to analyze
* @param paramTypes the parameter types of the method
* @return whether the class has a corresponding constructor
* @see Class#getMethod
* @see Class#getConstructor
*/
public static boolean hasConstructor(Class<?> clazz, Class<?>... paramTypes) {
return (getConstructorIfAvailable(clazz, paramTypes) != null);
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -232,16 +232,15 @@ private static EntityManager createProxy(

if (emIfc != null) {
interfaces = cachedEntityManagerInterfaces.computeIfAbsent(emIfc, key -> {
Set<Class<?>> ifcs = new LinkedHashSet<>();
Set<Class<?>> ifcs = new LinkedHashSet<>(4);
ifcs.add(key);
ifcs.add(EntityManagerProxy.class);
return ClassUtils.toClassArray(ifcs);
});
}
else {
interfaces = cachedEntityManagerInterfaces.computeIfAbsent(rawEm.getClass(), key -> {
Set<Class<?>> ifcs = new LinkedHashSet<>(ClassUtils
.getAllInterfacesForClassAsSet(key, cl));
Set<Class<?>> ifcs = new LinkedHashSet<>(ClassUtils.getAllInterfacesForClassAsSet(key, cl));
ifcs.add(EntityManagerProxy.class);
return ClassUtils.toClassArray(ifcs);
});
Expand Down

0 comments on commit 2209e7c

Please sign in to comment.