Skip to content

Commit

Permalink
Remove binary array name handling in ClassUtils.forName()
Browse files Browse the repository at this point in the history
In ClassUtils.forName(), we originally delegated to
ClassLoader.loadClass(), which does not support loading classes from
binary names for arrays (such as "[[I" for "int[][]" or
"[Ljava.lang.String;" for "String[]"); whereas, Class.forName() does
support binary names for arrays.

However, in Spring Framework 5.1.1 we switched from using
ClassLoader.loadClass() to Class.forName() in ClassUtils.forName() (see
gh-21867), which makes our custom handling of binary names for arrays
in ClassUtils.forName() obsolete.

In light of that, this commit removes our custom binary array name
handling support from ClassUtils.forName().

Closes gh-34291
  • Loading branch information
sbrannen committed Jan 20, 2025
1 parent eda7af7 commit 7fedb9c
Showing 1 changed file with 1 addition and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 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 @@ -74,12 +74,6 @@ public abstract class ClassUtils {
/** Suffix for array class names: {@code "[]"}. */
public static final String ARRAY_SUFFIX = "[]";

/** Prefix for internal array class names: {@code "["}. */
private static final String INTERNAL_ARRAY_PREFIX = "[";

/** 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 = {};

Expand Down Expand Up @@ -297,20 +291,6 @@ public static Class<?> forName(String name, @Nullable ClassLoader classLoader)
return elementClass.arrayType();
}

// "[Ljava.lang.String;" style arrays
if (name.startsWith(NON_PRIMITIVE_ARRAY_PREFIX) && name.endsWith(";")) {
String elementName = name.substring(NON_PRIMITIVE_ARRAY_PREFIX.length(), name.length() - 1);
Class<?> elementClass = forName(elementName, classLoader);
return elementClass.arrayType();
}

// "[[I" or "[[Ljava.lang.String;" style arrays
if (name.startsWith(INTERNAL_ARRAY_PREFIX)) {
String elementName = name.substring(INTERNAL_ARRAY_PREFIX.length());
Class<?> elementClass = forName(elementName, classLoader);
return elementClass.arrayType();
}

ClassLoader clToUse = classLoader;
if (clToUse == null) {
clToUse = getDefaultClassLoader();
Expand Down

0 comments on commit 7fedb9c

Please sign in to comment.