Skip to content

Optimize ClassUtils.forName() for more cases #1761

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import org.springframework.lang.Nullable;
Expand Down Expand Up @@ -102,10 +104,10 @@ public abstract class ClassUtils {
private static final Map<String, Class<?>> primitiveTypeNameMap = new HashMap<>(32);

/**
* Map with common "java.lang" class name as key and corresponding Class as value.
* Map with common Java language class name as key and corresponding Class as value.
* Primarily for efficient deserialization of remote invocations.
*/
private static final Map<String, Class<?>> commonClassCache = new HashMap<>(32);
private static final Map<String, Class<?>> commonClassCache = new HashMap<>(48);


static {
Expand Down Expand Up @@ -138,6 +140,9 @@ public abstract class ClassUtils {
Object.class, Object[].class, Class.class, Class[].class);
registerCommonClasses(Throwable.class, Exception.class, RuntimeException.class,
Error.class, StackTraceElement.class, StackTraceElement[].class);
registerCommonClasses(Enum.class, Optional.class);
registerCommonClasses(Collection.class, List.class, Map.class, Set.class);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would go even further here and register Iterable and Iterator as well (collapsed and reordered):

registerCommonClasses(Collection.class, List.class, Set.class, Map.class,
        Iterable.class, Iterator.class, Enum.class, Optional.class);

javaLanguageInterfaces.forEach(ClassUtils::registerCommonClasses);
}


Expand Down