From d252be0ff74318f81d50270d1701003a8b693786 Mon Sep 17 00:00:00 2001 From: Aman Sharma Date: Fri, 20 Nov 2026 00:18:02 +0100 Subject: [PATCH 1/2] Introduce failing test Signed-off-by: Aman Sharma --- .../cglib/core/ReflectUtilsTests.java | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 spring-core/src/test/java/org/springframework/cglib/core/ReflectUtilsTests.java diff --git a/spring-core/src/test/java/org/springframework/cglib/core/ReflectUtilsTests.java b/spring-core/src/test/java/org/springframework/cglib/core/ReflectUtilsTests.java new file mode 100644 index 000000000000..31231033613c --- /dev/null +++ b/spring-core/src/test/java/org/springframework/cglib/core/ReflectUtilsTests.java @@ -0,0 +1,70 @@ +/* + * Copyright 2002-present 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.cglib.core; + +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link ReflectUtils}. + * + * @author Aman Sharma + */ +class ReflectUtilsTests { + + @Test + void addAllMethodsSortsDeclaredMethods() { + List methods = new ArrayList<>(); + ReflectUtils.addAllMethods(ReverseOrder.class, methods); + + // The names are not spelled out here, because string literals would create them in sorted order + // before the JVM loads ReverseOrder. + assertThat(methods.subList(0, 6)).extracting(Method::getName).isSorted(); + } + + + /** + * {@link Class#getDeclaredMethods()} returns methods in no particular order. + * HotSpot sorts them by the memory address of their names, not alphabetically. + */ + public static class ReverseOrder { + + public void methodOrderFoxtrot() { + } + + public void methodOrderEcho() { + } + + public void methodOrderDelta() { + } + + public void methodOrderCharlie() { + } + + public void methodOrderBravo() { + } + + public void methodOrderAlpha() { + } + } + +} From 7d88c8e1383f20206e7ba957478da01807495488 Mon Sep 17 00:00:00 2001 From: Aman Sharma Date: Fri, 20 Nov 2026 00:18:13 +0100 Subject: [PATCH 2/2] Sort declared methods for deterministic CGLIB classes Signed-off-by: Aman Sharma --- .../java/org/springframework/cglib/core/ReflectUtils.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/spring-core/src/main/java/org/springframework/cglib/core/ReflectUtils.java b/spring-core/src/main/java/org/springframework/cglib/core/ReflectUtils.java index bbbdcbaeee32..5ba7a51eb58a 100644 --- a/spring-core/src/main/java/org/springframework/cglib/core/ReflectUtils.java +++ b/spring-core/src/main/java/org/springframework/cglib/core/ReflectUtils.java @@ -30,6 +30,7 @@ import java.security.ProtectionDomain; import java.util.ArrayList; import java.util.Arrays; +import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -393,7 +394,12 @@ public static List addAllMethods(final Class type, final List list) { list.addAll(OBJECT_METHODS); } else { - list.addAll(java.util.Arrays.asList(type.getDeclaredMethods())); + // Sort the methods, because getDeclaredMethods returns them in no particular order. + // Generated classes number and index the methods in this order. + Method[] methods = type.getDeclaredMethods(); + Arrays.sort(methods, Comparator.comparing(Method::getName) + .thenComparing(method -> Type.getMethodDescriptor(method))); + list.addAll(Arrays.asList(methods)); } Class superclass = type.getSuperclass();