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 1/2] 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 88e4e8b4b0a..93692fa678e 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 abstract class ClassUtils { } 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 b18d8b05878..2170cdcd860 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 @@ package a; /** * 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 7d93be15985..dd834e75dfa 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.List;
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 @@ class ClassUtilsTests {
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 @@ class ClassUtilsTests {
}
+ @Override
public void packageAccessiblePrint() {
}
From e702733c7b787300d63c21bc473e1aab5c882991 Mon Sep 17 00:00:00 2001
From: Sam Brannen <104798+sbrannen@users.noreply.github.com>
Date: Tue, 9 Apr 2024 13:41:59 +0200
Subject: [PATCH 2/2] Detect bridge methods across ApplicationContexts in
MethodIntrospector
Prior to this commit, MethodIntrospector failed to properly detect
bridge methods for subsequent invocations of selectMethods() with the
same targetType and MetadataLookup, if such subsequent invocations
occurred after the ApplicationContext had been refreshed.
The reason this occurs is due to the following.
- Class#getDeclaredMethods() always returns "child copies" of the
underlying Method instances -- which means that `equals()` should be
used instead of `==` whenever the compared Method instances can come
from different sources (such as the static caches mentioned below).
- BridgeMethodResolver caches resolved bridge methods in a static cache
-- which is never cleared.
- ReflectionUtils caches declared methods in a static cache
-- which gets cleared when an ApplicationContext is refreshed.
Consequently, if you attempt to load an ApplicationContext twice in the
same ClassLoader, the second attempt uses the existing, populated cache
for bridged methods but a cleared, empty cache for declared methods.
This results in new invocations of Class#getDeclaredMethods(), and
identity checks with `==` then fail to detect equivalent bridge methods.
This commit addresses this by additionally comparing bridge methods
using `equals()` in MethodIntrospector.selectMethods().
Note that the `==` checks remain in place as an optimization for when
`equals()` is unnecessary.
Closes gh-32586
---
.../core/MethodIntrospector.java | 4 +-
.../core/MethodIntrospectorTests.java | 112 ++++++++++++++++++
2 files changed, 115 insertions(+), 1 deletion(-)
create mode 100644 spring-core/src/test/java/org/springframework/core/MethodIntrospectorTests.java
diff --git a/spring-core/src/main/java/org/springframework/core/MethodIntrospector.java b/spring-core/src/main/java/org/springframework/core/MethodIntrospector.java
index 9f905cbda47..f1ceac4a7cc 100644
--- a/spring-core/src/main/java/org/springframework/core/MethodIntrospector.java
+++ b/spring-core/src/main/java/org/springframework/core/MethodIntrospector.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.
@@ -36,6 +36,7 @@ import org.springframework.util.ReflectionUtils;
*
* @author Juergen Hoeller
* @author Rossen Stoyanchev
+ * @author Sam Brannen
* @since 4.2.3
*/
public final class MethodIntrospector {
@@ -75,6 +76,7 @@ public final class MethodIntrospector {
if (result != null) {
Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);
if (bridgedMethod == specificMethod || bridgedMethod == method ||
+ bridgedMethod.equals(specificMethod) || bridgedMethod.equals(method) ||
metadataLookup.inspect(bridgedMethod) == null) {
methodMap.put(specificMethod, result);
}
diff --git a/spring-core/src/test/java/org/springframework/core/MethodIntrospectorTests.java b/spring-core/src/test/java/org/springframework/core/MethodIntrospectorTests.java
new file mode 100644
index 00000000000..f11f8eb8659
--- /dev/null
+++ b/spring-core/src/test/java/org/springframework/core/MethodIntrospectorTests.java
@@ -0,0 +1,112 @@
+/*
+ * 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.
+ * You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.springframework.core;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.reflect.Method;
+import java.util.Map;
+
+import org.junit.jupiter.api.Test;
+
+import org.springframework.core.MethodIntrospector.MetadataLookup;
+import org.springframework.core.annotation.MergedAnnotations;
+import org.springframework.util.ReflectionUtils;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.springframework.core.annotation.MergedAnnotations.SearchStrategy.TYPE_HIERARCHY;
+
+/**
+ * Tests for {@link MethodIntrospector}.
+ *
+ * @author Sam Brannen
+ * @since 5.3.34
+ */
+class MethodIntrospectorTests {
+
+ @Test // gh-32586
+ void selectMethodsAndClearDeclaredMethodsCacheBetweenInvocations() {
+ Class> targetType = ActualController.class;
+
+ // Preconditions for this use case.
+ assertThat(targetType).isPublic();
+ assertThat(targetType.getSuperclass()).isPackagePrivate();
+
+ MetadataLookup