Skip to content

Commit eebaa35

Browse files
committed
Fix a regression in Cglib Kotlin proxies
The commit skips using UndeclaredThrowableStrategy for Kotlin classes in CglibAopProxy in order to fix a related regression caused by gh-32469. See gh-33585
1 parent 02094b2 commit eebaa35

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,10 @@ private Object buildProxy(@Nullable ClassLoader classLoader, boolean classOnly)
206206
enhancer.setInterfaces(AopProxyUtils.completeProxiedInterfaces(this.advised));
207207
enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
208208
enhancer.setAttemptLoad(true);
209-
enhancer.setStrategy(new ClassLoaderAwareGeneratorStrategy(classLoader, undeclaredThrowableStrategy));
209+
enhancer.setStrategy(KotlinDetector.isKotlinType(proxySuperClass) ?
210+
new ClassLoaderAwareGeneratorStrategy(classLoader) :
211+
new ClassLoaderAwareGeneratorStrategy(classLoader, undeclaredThrowableStrategy)
212+
);
210213

211214
Callback[] callbacks = getCallbacks(rootClass);
212215
Class<?>[] types = new Class<?>[callbacks.length];
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.aop.framework
18+
19+
import org.assertj.core.api.Assertions.assertThat
20+
import org.assertj.core.api.Assertions.assertThatThrownBy
21+
import org.junit.jupiter.api.Test
22+
23+
/**
24+
* Tests for Kotlin support in [CglibAopProxy].
25+
*
26+
* @author Sebastien Deleuze
27+
*/
28+
class CglibAopProxyKotlinTests {
29+
30+
@Test
31+
fun proxiedInvocation() {
32+
val proxyFactory = ProxyFactory(MyKotlinBean())
33+
val proxy = proxyFactory.proxy as MyKotlinBean
34+
assertThat(proxy.capitalize("foo")).isEqualTo("FOO")
35+
}
36+
37+
@Test
38+
fun proxiedUncheckedException() {
39+
val proxyFactory = ProxyFactory(MyKotlinBean())
40+
val proxy = proxyFactory.proxy as MyKotlinBean
41+
assertThatThrownBy { proxy.uncheckedException() }.isInstanceOf(IllegalStateException::class.java)
42+
}
43+
44+
@Test
45+
fun proxiedCheckedException() {
46+
val proxyFactory = ProxyFactory(MyKotlinBean())
47+
val proxy = proxyFactory.proxy as MyKotlinBean
48+
assertThatThrownBy { proxy.checkedException() }.isInstanceOf(CheckedException::class.java)
49+
}
50+
51+
52+
open class MyKotlinBean {
53+
54+
open fun capitalize(value: String) = value.uppercase()
55+
56+
open fun uncheckedException() {
57+
throw IllegalStateException()
58+
}
59+
60+
open fun checkedException() {
61+
throw CheckedException()
62+
}
63+
}
64+
65+
class CheckedException() : Exception()
66+
}

0 commit comments

Comments
 (0)