Skip to content

Commit 0c80e5f

Browse files
committed
Use JdkDynamicAopProxy class loader instead of JDK bootstrap/platform loader
Closes gh-30115 (cherry picked from commit 7e905e3)
1 parent 7ad01a9 commit 0c80e5f

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -123,6 +123,11 @@ public Object getProxy(@Nullable ClassLoader classLoader) {
123123
if (logger.isTraceEnabled()) {
124124
logger.trace("Creating JDK dynamic proxy: " + this.advised.getTargetSource());
125125
}
126+
if (classLoader == null || classLoader.getParent() == null) {
127+
// JDK bootstrap loader or platform loader suggested ->
128+
// use higher-level loader which can see Spring infrastructure classes
129+
classLoader = getClass().getClassLoader();
130+
}
126131
return Proxy.newProxyInstance(classLoader, this.proxiedInterfaces, this);
127132
}
128133

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

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,10 @@
1616

1717
package org.springframework.aop.framework;
1818

19+
import java.sql.SQLException;
20+
import java.sql.Savepoint;
1921
import java.util.ArrayList;
22+
import java.util.Date;
2023
import java.util.List;
2124

2225
import javax.accessibility.Accessible;
@@ -380,6 +383,40 @@ public void testInterceptorWithoutJoinpoint() {
380383
assertThat(proxy.getName()).isEqualTo("tb");
381384
}
382385

386+
@Test
387+
public void testCharSequenceProxy() {
388+
CharSequence target = "test";
389+
ProxyFactory pf = new ProxyFactory(target);
390+
ClassLoader cl = target.getClass().getClassLoader();
391+
assertThat(((CharSequence) pf.getProxy(cl)).toString()).isEqualTo(target);
392+
}
393+
394+
@Test
395+
public void testDateProxy() {
396+
Date target = new Date();
397+
ProxyFactory pf = new ProxyFactory(target);
398+
pf.setProxyTargetClass(true);
399+
ClassLoader cl = target.getClass().getClassLoader();
400+
assertThat(((Date) pf.getProxy(cl)).getTime()).isEqualTo(target.getTime());
401+
}
402+
403+
@Test
404+
public void testJdbcSavepointProxy() throws SQLException {
405+
Savepoint target = new Savepoint() {
406+
@Override
407+
public int getSavepointId() throws SQLException {
408+
return 1;
409+
}
410+
@Override
411+
public String getSavepointName() throws SQLException {
412+
return "sp";
413+
}
414+
};
415+
ProxyFactory pf = new ProxyFactory(target);
416+
ClassLoader cl = Savepoint.class.getClassLoader();
417+
assertThat(((Savepoint) pf.getProxy(cl)).getSavepointName()).isEqualTo("sp");
418+
}
419+
383420

384421
@Order(2)
385422
public static class A implements Runnable {
@@ -391,7 +428,7 @@ public void run() {
391428

392429

393430
@Order(1)
394-
public static class B implements Runnable{
431+
public static class B implements Runnable {
395432

396433
@Override
397434
public void run() {

0 commit comments

Comments
 (0)