From 187f641409e0eee2cea80e1570527ac9a260ef02 Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Mon, 8 Apr 2024 17:02:08 +0200 Subject: [PATCH] Polishing --- .../main/java/org/springframework/util/ClassUtils.java | 2 +- spring-core/src/test/java/a/ClassHavingNestedClass.java | 5 ++++- .../java/org/springframework/util/ClassUtilsTests.java | 9 +++++++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/ClassUtils.java b/spring-core/src/main/java/org/springframework/util/ClassUtils.java index 88e4e8b4b0a7..93692fa678e4 100644 --- a/spring-core/src/main/java/org/springframework/util/ClassUtils.java +++ b/spring-core/src/main/java/org/springframework/util/ClassUtils.java @@ -305,7 +305,7 @@ public static Class forName(String name, @Nullable ClassLoader classLoader) } catch (ClassNotFoundException ex) { int lastDotIndex = name.lastIndexOf(PACKAGE_SEPARATOR); - int previousDotIndex = name.lastIndexOf(PACKAGE_SEPARATOR, lastDotIndex -1); + int previousDotIndex = name.lastIndexOf(PACKAGE_SEPARATOR, lastDotIndex - 1); if (lastDotIndex != -1 && previousDotIndex != -1 && Character.isUpperCase(name.charAt(previousDotIndex + 1))) { String nestedClassName = name.substring(0, lastDotIndex) + NESTED_CLASS_SEPARATOR + name.substring(lastDotIndex + 1); diff --git a/spring-core/src/test/java/a/ClassHavingNestedClass.java b/spring-core/src/test/java/a/ClassHavingNestedClass.java index b18d8b058789..2170cdcd860e 100644 --- a/spring-core/src/test/java/a/ClassHavingNestedClass.java +++ b/spring-core/src/test/java/a/ClassHavingNestedClass.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 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. @@ -19,6 +19,9 @@ /** * Test class for {@code org.springframework.util.ClassUtilsTests}. * + *

The use case for this test class requires that the package name is a single + * character (i.e., length of 1). + * * @author Johnny Lim */ public class ClassHavingNestedClass { diff --git a/spring-core/src/test/java/org/springframework/util/ClassUtilsTests.java b/spring-core/src/test/java/org/springframework/util/ClassUtilsTests.java index 7d93be159859..dd834e75dfa9 100644 --- a/spring-core/src/test/java/org/springframework/util/ClassUtilsTests.java +++ b/spring-core/src/test/java/org/springframework/util/ClassUtilsTests.java @@ -33,6 +33,7 @@ import java.util.Set; import java.util.function.Supplier; +import a.ClassHavingNestedClass; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; @@ -86,8 +87,11 @@ void forName() throws ClassNotFoundException { void forNameWithNestedType() throws ClassNotFoundException { assertThat(ClassUtils.forName("org.springframework.util.ClassUtilsTests$NestedClass", classLoader)).isEqualTo(NestedClass.class); assertThat(ClassUtils.forName("org.springframework.util.ClassUtilsTests.NestedClass", classLoader)).isEqualTo(NestedClass.class); - assertThat(ClassUtils.forName("a.ClassHavingNestedClass$NestedClass", classLoader)).isEqualTo(a.ClassHavingNestedClass.NestedClass.class); - assertThat(ClassUtils.forName("a.ClassHavingNestedClass.NestedClass", classLoader)).isEqualTo(a.ClassHavingNestedClass.NestedClass.class); + + // Precondition: package name must have length == 1. + assertThat(ClassHavingNestedClass.class.getPackageName().length()).isEqualTo(1); + assertThat(ClassUtils.forName("a.ClassHavingNestedClass$NestedClass", classLoader)).isEqualTo(ClassHavingNestedClass.NestedClass.class); + assertThat(ClassUtils.forName("a.ClassHavingNestedClass.NestedClass", classLoader)).isEqualTo(ClassHavingNestedClass.NestedClass.class); } @Test @@ -625,6 +629,7 @@ protected void protectedPrint() { } + @Override public void packageAccessiblePrint() { }