Skip to content

Commit

Permalink
Delete cases covered by Class.forName()
Browse files Browse the repository at this point in the history
`Class.forName()` from `java.base/java.lang` has
already built-in support for type names matching:
- _Primitive arrays such as "[I", "[[[[D", etc._ and
- _Object arrays such as "[Ljava.lang.String;", etc._

This commit deletes the redundant implementation
from JUnit's internal `ReflectionUtils` helper.
  • Loading branch information
sormuras committed Jan 20, 2025
1 parent 0fe36c7 commit 2bae68b
Showing 1 changed file with 1 addition and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,21 +129,6 @@ public enum HierarchyTraversalMode {
BOTTOM_UP
}

// Pattern: "[Ljava.lang.String;", "[[[[Ljava.lang.String;", etc.
private static final Pattern VM_INTERNAL_OBJECT_ARRAY_PATTERN = Pattern.compile("^(\\[+)L(.+);$");

/**
* Pattern: "[x", "[[[[x", etc., where x is Z, B, C, D, F, I, J, S, etc.
*
* <p>The pattern intentionally captures the last bracket with the
* capital letter so that the combination can be looked up via
* {@link #classNameToTypeMap}. For example, the last matched group
* will contain {@code "[I"} instead of {@code "I"}.
*
* @see Class#getName()
*/
private static final Pattern VM_INTERNAL_PRIMITIVE_ARRAY_PATTERN = Pattern.compile("^(\\[+)(\\[[ZBCDFIJS])$");

// Pattern: "java.lang.String[]", "int[]", "int[][][][]", etc.
// ?> => non-capturing atomic group
// ++ => possessive quantifier
Expand Down Expand Up @@ -855,32 +840,8 @@ public static Try<Class<?>> tryToLoadClass(String name, ClassLoader classLoader)
}

return Try.call(() -> {
Matcher matcher;

// Primitive arrays such as "[I", "[[[[D", etc.
matcher = VM_INTERNAL_PRIMITIVE_ARRAY_PATTERN.matcher(trimmedName);
if (matcher.matches()) {
String brackets = matcher.group(1);
String componentTypeName = matcher.group(2);
// Calculate dimensions by counting brackets.
int dimensions = brackets.length();

return loadArrayType(classLoader, componentTypeName, dimensions);
}

// Object arrays such as "[Ljava.lang.String;", "[[[[Ljava.lang.String;", etc.
matcher = VM_INTERNAL_OBJECT_ARRAY_PATTERN.matcher(trimmedName);
if (matcher.matches()) {
String brackets = matcher.group(1);
String componentTypeName = matcher.group(2);
// Calculate dimensions by counting brackets.
int dimensions = brackets.length();

return loadArrayType(classLoader, componentTypeName, dimensions);
}

// Arrays such as "java.lang.String[]", "int[]", "int[][][][]", etc.
matcher = SOURCE_CODE_SYNTAX_ARRAY_PATTERN.matcher(trimmedName);
Matcher matcher = SOURCE_CODE_SYNTAX_ARRAY_PATTERN.matcher(trimmedName);
if (matcher.matches()) {
String componentTypeName = matcher.group(1);
String bracketPairs = matcher.group(2);
Expand Down

0 comments on commit 2bae68b

Please sign in to comment.