Skip to content

Commit 18456de

Browse files
committed
Reintroduce FastClass in CGLIB class names for @⁠Configuration classes
Given a @⁠Configuration class named org.example.AppConfig which contains @⁠Bean methods, in Spring Framework 5.3.x and previous versions, the following classes were created when generating the CGLIB proxy. org.example.AppConfig$$EnhancerBySpringCGLIB$$fd7e9baa org.example.AppConfig$$FastClassBySpringCGLIB$$3fec86e org.example.AppConfig$$EnhancerBySpringCGLIB$$fd7e9baa$$FastClassBySpringCGLIB$$82534900 Those class names indicate that 1 class was generated for the proxy for the @⁠Configuration class itself and that 2 additional FastClass classes were generated to support proxying of @⁠Bean methods in superclasses. However, since Spring Framework 6.0, the following classes are created when generating the CGLIB proxy. org.example.AppConfig$$SpringCGLIB$$0 org.example.AppConfig$$SpringCGLIB$$1 org.example.AppConfig$$SpringCGLIB$$2 The above class names make it appear that 3 proxy classes are generated for each @⁠Configuration class, which is misleading. To address that and to align more closely with how such generated classes were named in previous versions of the framework, this commit modifies SpringNamingPolicy so that generated class names once again include "FastClass" when the generated class is for a CGLIB FastClass as opposed to the actual proxy for the @⁠Configuration class. Consequently, with this commit the following classes are created when generating the CGLIB proxy. org.example.AppConfig$$SpringCGLIB$$0 org.example.AppConfig$$SpringCGLIB$$FastClass$$0 org.example.AppConfig$$SpringCGLIB$$FastClass$$1 Closes gh-31272
1 parent d17c75a commit 18456de

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

spring-context/src/test/java/org/springframework/context/aot/ApplicationContextAotGeneratorTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,8 @@ void processAheadOfTimeWhenHasCglibProxyWriteProxyAndGenerateReflectionHints() t
359359
applicationContext.registerBean(CglibConfiguration.class);
360360
TestGenerationContext context = processAheadOfTime(applicationContext);
361361
isRegisteredCglibClass(context, CglibConfiguration.class.getName() + "$$SpringCGLIB$$0");
362-
isRegisteredCglibClass(context, CglibConfiguration.class.getName() + "$$SpringCGLIB$$1");
363-
isRegisteredCglibClass(context, CglibConfiguration.class.getName() + "$$SpringCGLIB$$2");
362+
isRegisteredCglibClass(context, CglibConfiguration.class.getName() + "$$SpringCGLIB$$FastClass$$0");
363+
isRegisteredCglibClass(context, CglibConfiguration.class.getName() + "$$SpringCGLIB$$FastClass$$1");
364364
}
365365

366366
private void isRegisteredCglibClass(TestGenerationContext context, String cglibClassName) throws IOException {

spring-core/src/main/java/org/springframework/cglib/core/SpringNamingPolicy.java

+10
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* in the classpath.
2626
*
2727
* @author Juergen Hoeller
28+
* @author Sam Brannen
2829
* @since 3.2.8 / 6.0
2930
*/
3031
public final class SpringNamingPolicy implements NamingPolicy {
@@ -33,6 +34,8 @@ public final class SpringNamingPolicy implements NamingPolicy {
3334

3435
private static final String SPRING_LABEL = "$$SpringCGLIB$$";
3536

37+
private static final String FAST_CLASS_SUFFIX = "FastClass$$";
38+
3639

3740
private SpringNamingPolicy() {
3841
}
@@ -55,6 +58,13 @@ else if (prefix.startsWith("java.") || prefix.startsWith("javax.")) {
5558
base = prefix + SPRING_LABEL;
5659
}
5760

61+
// When the generated class name is for a FastClass, the source is
62+
// "org.springframework.cglib.reflect.FastClass".
63+
boolean isFastClass = (source != null && source.endsWith(".FastClass"));
64+
if (isFastClass && !prefix.contains(FAST_CLASS_SUFFIX)) {
65+
base += FAST_CLASS_SUFFIX;
66+
}
67+
5868
int index = 0;
5969
String attempt = base + index;
6070
while (names.evaluate(attempt)) {

spring-core/src/test/java/org/springframework/cglib/core/SpringNamingPolicyTests.java

+11
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
import org.junit.jupiter.api.Test;
2323

24+
import org.springframework.cglib.reflect.FastClass;
25+
2426
import static org.assertj.core.api.Assertions.assertThat;
2527

2628
/**
@@ -67,6 +69,15 @@ void prefixContainingSpringLabel() {
6769
assertThat(getClassName(generated1)).isEqualTo(generated2);
6870
}
6971

72+
@Test
73+
void fastClass() {
74+
String prefix = "example.MyComponent";
75+
String source = FastClass.class.getName();
76+
assertThat(getClassName(prefix, "a.b.c", null)).isEqualTo("example.MyComponent$$SpringCGLIB$$0");
77+
assertThat(getClassName(prefix, source, null)).isEqualTo("example.MyComponent$$SpringCGLIB$$FastClass$$0");
78+
assertThat(getClassName(prefix, source, null)).isEqualTo("example.MyComponent$$SpringCGLIB$$FastClass$$1");
79+
}
80+
7081
private String getClassName(String prefix) {
7182
return getClassName(prefix, null, null);
7283
}

0 commit comments

Comments
 (0)