Skip to content

Commit

Permalink
Fix a regression in Cglib Kotlin proxies
Browse files Browse the repository at this point in the history
The commit skips using UndeclaredThrowableStrategy for
Kotlin classes in CglibAopProxy in order to fix a
related regression caused by spring-projectsgh-32469.

See spring-projectsgh-33585
  • Loading branch information
sdeleuze committed Oct 3, 2024
1 parent 02094b2 commit eebaa35
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,10 @@ private Object buildProxy(@Nullable ClassLoader classLoader, boolean classOnly)
enhancer.setInterfaces(AopProxyUtils.completeProxiedInterfaces(this.advised));
enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
enhancer.setAttemptLoad(true);
enhancer.setStrategy(new ClassLoaderAwareGeneratorStrategy(classLoader, undeclaredThrowableStrategy));
enhancer.setStrategy(KotlinDetector.isKotlinType(proxySuperClass) ?
new ClassLoaderAwareGeneratorStrategy(classLoader) :
new ClassLoaderAwareGeneratorStrategy(classLoader, undeclaredThrowableStrategy)
);

Callback[] callbacks = getCallbacks(rootClass);
Class<?>[] types = new Class<?>[callbacks.length];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.aop.framework

import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatThrownBy
import org.junit.jupiter.api.Test

/**
* Tests for Kotlin support in [CglibAopProxy].
*
* @author Sebastien Deleuze
*/
class CglibAopProxyKotlinTests {

@Test
fun proxiedInvocation() {
val proxyFactory = ProxyFactory(MyKotlinBean())
val proxy = proxyFactory.proxy as MyKotlinBean
assertThat(proxy.capitalize("foo")).isEqualTo("FOO")
}

@Test
fun proxiedUncheckedException() {
val proxyFactory = ProxyFactory(MyKotlinBean())
val proxy = proxyFactory.proxy as MyKotlinBean
assertThatThrownBy { proxy.uncheckedException() }.isInstanceOf(IllegalStateException::class.java)
}

@Test
fun proxiedCheckedException() {
val proxyFactory = ProxyFactory(MyKotlinBean())
val proxy = proxyFactory.proxy as MyKotlinBean
assertThatThrownBy { proxy.checkedException() }.isInstanceOf(CheckedException::class.java)
}


open class MyKotlinBean {

open fun capitalize(value: String) = value.uppercase()

open fun uncheckedException() {
throw IllegalStateException()
}

open fun checkedException() {
throw CheckedException()
}
}

class CheckedException() : Exception()
}

0 comments on commit eebaa35

Please sign in to comment.