Skip to content

Commit 155a37d

Browse files
committed
Merge branch '6.0.x'
2 parents f8a3253 + 0a5aff1 commit 155a37d

File tree

4 files changed

+58
-18
lines changed

4 files changed

+58
-18
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

spring-jms/src/main/java/org/springframework/jms/support/JmsAccessor.java

Lines changed: 5 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.
@@ -223,7 +223,10 @@ protected Session createSession(Connection con) throws JMSException {
223223
* @see jakarta.jms.Session#CLIENT_ACKNOWLEDGE
224224
*/
225225
protected boolean isClientAcknowledge(Session session) throws JMSException {
226-
return (session.getAcknowledgeMode() == Session.CLIENT_ACKNOWLEDGE);
226+
int mode = session.getAcknowledgeMode();
227+
return (mode != Session.SESSION_TRANSACTED &&
228+
mode != Session.AUTO_ACKNOWLEDGE &&
229+
mode != Session.DUPS_OK_ACKNOWLEDGE);
227230
}
228231

229232
}

spring-jms/src/test/java/org/springframework/jms/support/JmsAccessorTests.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 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.
@@ -21,44 +21,54 @@
2121

2222
import static org.assertj.core.api.Assertions.assertThat;
2323
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
24+
import static org.mockito.BDDMockito.given;
25+
import static org.mockito.Mockito.mock;
2426

2527
/**
2628
* Unit tests for the {@link JmsAccessor} class.
2729
*
2830
* @author Rick Evans
2931
* @author Chris Beams
32+
* @author Vedran Pavic
3033
*/
31-
public class JmsAccessorTests {
34+
class JmsAccessorTests {
3235

3336
@Test
34-
public void testChokesIfConnectionFactoryIsNotSupplied() throws Exception {
37+
void testChokesIfConnectionFactoryIsNotSupplied() {
3538
JmsAccessor accessor = new StubJmsAccessor();
3639
assertThatIllegalArgumentException().isThrownBy(
3740
accessor::afterPropertiesSet);
3841
}
3942

4043
@Test
41-
public void testSessionTransactedModeReallyDoesDefaultToFalse() throws Exception {
44+
void testSessionTransactedModeReallyDoesDefaultToFalse() {
4245
JmsAccessor accessor = new StubJmsAccessor();
4346
assertThat(accessor.isSessionTransacted()).as("The [sessionTransacted] property of JmsAccessor must default to " +
4447
"false. Change this test (and the attendant Javadoc) if you have " +
4548
"changed the default.").isFalse();
4649
}
4750

4851
@Test
49-
public void testAcknowledgeModeReallyDoesDefaultToAutoAcknowledge() throws Exception {
52+
void testAcknowledgeModeReallyDoesDefaultToAutoAcknowledge() {
5053
JmsAccessor accessor = new StubJmsAccessor();
5154
assertThat(accessor.getSessionAcknowledgeMode()).as("The [sessionAcknowledgeMode] property of JmsAccessor must default to " +
5255
"[Session.AUTO_ACKNOWLEDGE]. Change this test (and the attendant " +
5356
"Javadoc) if you have changed the default.").isEqualTo(Session.AUTO_ACKNOWLEDGE);
5457
}
5558

5659
@Test
57-
public void testSetAcknowledgeModeNameChokesIfBadAckModeIsSupplied() throws Exception {
60+
void testSetAcknowledgeModeNameChokesIfBadAckModeIsSupplied() {
5861
assertThatIllegalArgumentException().isThrownBy(() ->
5962
new StubJmsAccessor().setSessionAcknowledgeModeName("Tally ho chaps!"));
6063
}
6164

65+
@Test
66+
void testCustomAcknowledgeModeIsConsideredClientAcknowledge() throws Exception {
67+
Session session = mock(Session.class);
68+
given(session.getAcknowledgeMode()).willReturn(100);
69+
JmsAccessor accessor = new StubJmsAccessor();
70+
assertThat(accessor.isClientAcknowledge(session)).isTrue();
71+
}
6272

6373
/**
6474
* Crummy, stub, do-nothing subclass of the JmsAccessor class for use in testing.

0 commit comments

Comments
 (0)