Skip to content

Commit a5f9a0c

Browse files
committed
org.apache.commons.lang3.ClassUtils.getCanonicalName(String) now throws
an IllegalArgumentException for array dimensions greater than 255 - No need to create strings over and over in a loop - Preallocate string builder - Javadoc since tag not needed for a private method
1 parent 34bc24a commit a5f9a0c

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

src/changes/changes.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ The <action> type attribute can be add,update,fix,remove.
8787
<action type="fix" dev="ggregory" due-to="Gary Gregory">Reimplement StringUtils.toCodePoints(CharSequence) to use java.lang.CharSequence.codePoints().</action>
8888
<action type="fix" dev="ggregory" due-to="Gary Gregory">Reimplement StringUtils.capitalize(String) to use java.lang.CharSequence.codePoints().</action>
8989
<action type="fix" dev="ggregory" due-to="Gary Gregory">Reimplement StringUtils.uncapitalize(String) to use java.lang.CharSequence.codePoints().</action>
90+
<action type="fix" dev="ggregory" due-to="Gary Gregory">org.apache.commons.lang3.ClassUtils.getCanonicalName(String) now throws an IllegalArgumentException for array dimensions greater than 255.</action>
9091
<action type="fix" dev="ggregory" due-to="Sridhar Balijepalli, Piotr P. Karwasz">Fix Javadoc typo and improve clarity in defaultIfBlank method #1376.</action>
9192
<!-- ADD -->
9293
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Strings and refactor StringUtils.</action>

src/main/java/org/apache/commons/lang3/ClassUtils.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@
5050
*/
5151
public class ClassUtils {
5252

53+
/**
54+
* The maximum number of array dimensions.
55+
*/
56+
private static final int MAX_DIMENSIONS = 255;
57+
5358
/**
5459
* Inclusivity literals for {@link #hierarchy(Class, Interfaces)}.
5560
*
@@ -470,29 +475,32 @@ public static String getCanonicalName(final Object object, final String valueIfN
470475
* </ul>
471476
* </p>
472477
*
473-
* @param className the name of class
474-
* @return canonical form of class name
475-
* @since 2.4
478+
* @param className the name of class.
479+
* @return canonical form of class name.
476480
*/
477481
private static String getCanonicalName(final String name) {
478482
String className = StringUtils.deleteWhitespace(name);
479483
if (className == null) {
480484
return null;
481485
}
482486
int dim = 0;
483-
while (className.startsWith("[")) {
487+
while (className.charAt(dim) == '[') {
484488
dim++;
485-
className = className.substring(1);
489+
if (dim > MAX_DIMENSIONS) {
490+
throw new IllegalArgumentException(String.format("Maximum array dimension %d exceeded", MAX_DIMENSIONS));
491+
}
486492
}
487493
if (dim < 1) {
488494
return className;
489495
}
496+
className = className.substring(dim);
490497
if (className.startsWith("L")) {
491498
className = className.substring(1, className.endsWith(";") ? className.length() - 1 : className.length());
492499
} else if (!className.isEmpty()) {
493500
className = reverseAbbreviationMap.get(className.substring(0, 1));
494501
}
495-
final StringBuilder canonicalClassNameBuffer = new StringBuilder(className);
502+
final StringBuilder canonicalClassNameBuffer = new StringBuilder(className.length() + dim * 2);
503+
canonicalClassNameBuffer.append(className);
496504
for (int i = 0; i < dim; i++) {
497505
canonicalClassNameBuffer.append("[]");
498506
}

src/test/java/org/apache/commons/lang3/ClassUtilsTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,13 +562,13 @@ public void test_getShortCanonicalName_String() {
562562
assertEquals("int[][]", ClassUtils.getShortCanonicalName("[[I"));
563563
assertEquals("int[]", ClassUtils.getShortCanonicalName("int[]"));
564564
assertEquals("int[][]", ClassUtils.getShortCanonicalName("int[][]"));
565-
566565
// this is to demonstrate that the documentation and the naming of the methods
567566
// uses the class name and canonical name totally mixed up, which cannot be
568567
// fixed without backward compatibility break
569568
assertEquals("int[]", int[].class.getCanonicalName());
570569
assertEquals("[I", int[].class.getName());
571-
570+
assertThrows(IllegalArgumentException.class, () -> ClassUtils.getShortCanonicalName(StringUtils.repeat("[", 256) + "I"));
571+
assertEquals("int" + StringUtils.repeat("[]", 255), ClassUtils.getShortCanonicalName(StringUtils.repeat("[", 255) + "I"));
572572
// Inner types... the problem is that these are not canonical names, classes with this name do not even have canonical
573573
// name
574574
// WARNING: this is fragile, implementation may change, naming is not guaranteed

0 commit comments

Comments
 (0)