Skip to content

Commit 0a5aff1

Browse files
committed
Specific check for parent of spring-aop ClassLoader
Also applied to getProxyClass now. Closes gh-30389
1 parent 3d61d9e commit 0a5aff1

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

spring-aop/src/main/java/org/springframework/aop/framework/JdkDynamicAopProxy.java

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,18 +120,39 @@ public Object getProxy(@Nullable ClassLoader classLoader) {
120120
if (logger.isTraceEnabled()) {
121121
logger.trace("Creating JDK dynamic proxy: " + this.advised.getTargetSource());
122122
}
123-
if (classLoader == null || classLoader.getParent() == null) {
124-
// JDK bootstrap loader or platform loader suggested ->
125-
// use higher-level loader which can see Spring infrastructure classes
126-
classLoader = getClass().getClassLoader();
127-
}
128-
return Proxy.newProxyInstance(classLoader, this.proxiedInterfaces, this);
123+
return Proxy.newProxyInstance(determineClassLoader(classLoader), this.proxiedInterfaces, this);
129124
}
130125

131126
@SuppressWarnings("deprecation")
132127
@Override
133128
public Class<?> getProxyClass(@Nullable ClassLoader classLoader) {
134-
return Proxy.getProxyClass(classLoader, this.proxiedInterfaces);
129+
return Proxy.getProxyClass(determineClassLoader(classLoader), this.proxiedInterfaces);
130+
}
131+
132+
/**
133+
* Determine whether the JDK bootstrap or platform loader has been suggested ->
134+
* use higher-level loader which can see Spring infrastructure classes instead.
135+
*/
136+
private ClassLoader determineClassLoader(@Nullable ClassLoader classLoader) {
137+
if (classLoader == null) {
138+
// JDK bootstrap loader -> use spring-aop ClassLoader instead.
139+
return getClass().getClassLoader();
140+
}
141+
if (classLoader.getParent() == null) {
142+
// Potentially the JDK platform loader on JDK 9+
143+
ClassLoader aopClassLoader = getClass().getClassLoader();
144+
ClassLoader aopParent = aopClassLoader.getParent();
145+
while (aopParent != null) {
146+
if (classLoader == aopParent) {
147+
// Suggested ClassLoader is ancestor of spring-aop ClassLoader
148+
// -> use spring-aop ClassLoader itself instead.
149+
return aopClassLoader;
150+
}
151+
aopParent = aopParent.getParent();
152+
}
153+
}
154+
// Regular case: use suggested ClassLoader as-is.
155+
return classLoader;
135156
}
136157

137158
/**

spring-aop/src/test/java/org/springframework/aop/framework/ProxyFactoryTests.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,9 @@ public void testCharSequenceProxy() {
389389
CharSequence target = "test";
390390
ProxyFactory pf = new ProxyFactory(target);
391391
ClassLoader cl = target.getClass().getClassLoader();
392-
assertThat(((CharSequence) pf.getProxy(cl)).toString()).isEqualTo(target);
392+
CharSequence proxy = (CharSequence) pf.getProxy(cl);
393+
assertThat(proxy.toString()).isEqualTo(target);
394+
assertThat(pf.getProxyClass(cl)).isSameAs(proxy.getClass());
393395
}
394396

395397
@Test
@@ -398,7 +400,9 @@ public void testDateProxy() {
398400
ProxyFactory pf = new ProxyFactory(target);
399401
pf.setProxyTargetClass(true);
400402
ClassLoader cl = target.getClass().getClassLoader();
401-
assertThat(((Date) pf.getProxy(cl)).getTime()).isEqualTo(target.getTime());
403+
Date proxy = (Date) pf.getProxy(cl);
404+
assertThat(proxy.getTime()).isEqualTo(target.getTime());
405+
assertThat(pf.getProxyClass(cl)).isSameAs(proxy.getClass());
402406
}
403407

404408
@Test
@@ -415,7 +419,9 @@ public String getSavepointName() throws SQLException {
415419
};
416420
ProxyFactory pf = new ProxyFactory(target);
417421
ClassLoader cl = Savepoint.class.getClassLoader();
418-
assertThat(((Savepoint) pf.getProxy(cl)).getSavepointName()).isEqualTo("sp");
422+
Savepoint proxy = (Savepoint) pf.getProxy(cl);
423+
assertThat(proxy.getSavepointName()).isEqualTo("sp");
424+
assertThat(pf.getProxyClass(cl)).isSameAs(proxy.getClass());
419425
}
420426

421427

0 commit comments

Comments
 (0)