-
Notifications
You must be signed in to change notification settings - Fork 38.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consistent handling of undeclared checked exceptions in CGLIB proxies #32469
Merged
jhoeller
merged 3 commits into
spring-projects:main
from
LeMikaelF:fix-undeclared-exceptions
Apr 10, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 0 additions & 28 deletions
28
spring-aop/src/test/java/org/springframework/aop/framework/ClassWithConstructor.java
This file was deleted.
Oops, something went wrong.
211 changes: 211 additions & 0 deletions
211
spring-aop/src/test/java/org/springframework/aop/framework/ProxyExceptionHandlingTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
/* | ||
* 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 java.io.Serial; | ||
import java.lang.reflect.Proxy; | ||
import java.lang.reflect.UndeclaredThrowableException; | ||
import java.util.Objects; | ||
|
||
import org.aopalliance.intercept.MethodInterceptor; | ||
import org.assertj.core.api.WithAssertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
import org.mockito.stubbing.Answer; | ||
|
||
import org.springframework.cglib.proxy.Enhancer; | ||
import org.springframework.lang.Nullable; | ||
|
||
import static org.mockito.BDDMockito.doAnswer; | ||
import static org.mockito.BDDMockito.doThrow; | ||
import static org.mockito.BDDMockito.mock; | ||
|
||
abstract class ProxyExceptionHandlingTests implements WithAssertions { | ||
|
||
private static final RuntimeException uncheckedException = new RuntimeException(); | ||
private static final DeclaredCheckedException declaredCheckedException = new DeclaredCheckedException(); | ||
private static final UndeclaredCheckedException undeclaredCheckedException = new UndeclaredCheckedException(); | ||
|
||
protected final MyClass target = mock(MyClass.class); | ||
protected final ProxyFactory proxyFactory = new ProxyFactory(target); | ||
|
||
@Nullable | ||
protected MyInterface proxy; | ||
@Nullable | ||
private Throwable throwableSeenByCaller; | ||
|
||
static class ObjenesisCglibAopProxyTests extends ProxyExceptionHandlingTests { | ||
@BeforeEach | ||
void beforeEach() { | ||
proxyFactory.setProxyTargetClass(true); | ||
} | ||
|
||
@Override | ||
protected void assertProxyType(Object proxy) { | ||
assertThat(Enhancer.isEnhanced(proxy.getClass())).isTrue(); | ||
} | ||
} | ||
|
||
static class JdkAopProxyTests extends ProxyExceptionHandlingTests { | ||
@Override | ||
protected void assertProxyType(Object proxy) { | ||
assertThat(Proxy.isProxyClass(proxy.getClass())).isTrue(); | ||
} | ||
} | ||
|
||
protected void assertProxyType(Object proxy) { | ||
} | ||
|
||
@BeforeEach | ||
void beforeEach() { | ||
Mockito.clearInvocations(target); | ||
} | ||
|
||
@Nested | ||
class WhenThereIsOneInterceptor { | ||
|
||
@Nullable | ||
private Throwable throwableSeenByInterceptor; | ||
|
||
@BeforeEach | ||
void beforeEach() { | ||
proxyFactory.addAdvice(captureThrowable()); | ||
|
||
proxy = (MyInterface) proxyFactory.getProxy(); | ||
|
||
assertProxyType(proxy); | ||
} | ||
|
||
@Test | ||
void targetThrowsUndeclaredCheckedException() throws DeclaredCheckedException { | ||
doAnswer(sneakyThrow(undeclaredCheckedException)).when(target).doSomething(); | ||
|
||
invokeProxy(); | ||
|
||
assertThat(throwableSeenByInterceptor).isSameAs(undeclaredCheckedException); | ||
assertThat(throwableSeenByCaller) | ||
.isInstanceOf(UndeclaredThrowableException.class) | ||
.hasCauseReference(undeclaredCheckedException); | ||
} | ||
|
||
@Test | ||
void targetThrowsDeclaredCheckedException() throws DeclaredCheckedException { | ||
doThrow(declaredCheckedException).when(target).doSomething(); | ||
|
||
invokeProxy(); | ||
|
||
assertThat(throwableSeenByInterceptor).isSameAs(declaredCheckedException); | ||
assertThat(throwableSeenByCaller).isSameAs(declaredCheckedException); | ||
} | ||
|
||
@Test | ||
void targetThrowsUncheckedException() throws DeclaredCheckedException { | ||
doThrow(uncheckedException).when(target).doSomething(); | ||
|
||
invokeProxy(); | ||
|
||
assertThat(throwableSeenByInterceptor).isSameAs(uncheckedException); | ||
assertThat(throwableSeenByCaller).isSameAs(uncheckedException); | ||
} | ||
|
||
private MethodInterceptor captureThrowable() { | ||
return invocation -> { | ||
try { | ||
return invocation.proceed(); | ||
} | ||
catch (Exception e) { | ||
throwableSeenByInterceptor = e; | ||
throw e; | ||
} | ||
}; | ||
} | ||
} | ||
|
||
@Nested | ||
class WhenThereAreNoInterceptors { | ||
|
||
@BeforeEach | ||
void beforeEach() { | ||
proxy = (MyInterface) proxyFactory.getProxy(); | ||
|
||
assertProxyType(proxy); | ||
} | ||
|
||
@Test | ||
void targetThrowsUndeclaredCheckedException() throws DeclaredCheckedException { | ||
doAnswer(sneakyThrow(undeclaredCheckedException)).when(target).doSomething(); | ||
|
||
invokeProxy(); | ||
|
||
assertThat(throwableSeenByCaller) | ||
.isInstanceOf(UndeclaredThrowableException.class) | ||
.hasCauseReference(undeclaredCheckedException); | ||
} | ||
|
||
@Test | ||
void targetThrowsDeclaredCheckedException() throws DeclaredCheckedException { | ||
doThrow(declaredCheckedException).when(target).doSomething(); | ||
|
||
invokeProxy(); | ||
|
||
assertThat(throwableSeenByCaller).isSameAs(declaredCheckedException); | ||
} | ||
|
||
@Test | ||
void targetThrowsUncheckedException() throws DeclaredCheckedException { | ||
doThrow(uncheckedException).when(target).doSomething(); | ||
|
||
invokeProxy(); | ||
|
||
assertThat(throwableSeenByCaller).isSameAs(uncheckedException); | ||
} | ||
} | ||
|
||
private void invokeProxy() { | ||
throwableSeenByCaller = catchThrowable(() -> Objects.requireNonNull(proxy).doSomething()); | ||
} | ||
|
||
private Answer<?> sneakyThrow(@SuppressWarnings("SameParameterValue") Throwable throwable) { | ||
return invocation -> { | ||
throw throwable; | ||
}; | ||
} | ||
|
||
static class MyClass implements MyInterface { | ||
@Override | ||
public void doSomething() throws DeclaredCheckedException { | ||
throw declaredCheckedException; | ||
} | ||
} | ||
|
||
protected interface MyInterface { | ||
void doSomething() throws DeclaredCheckedException; | ||
} | ||
|
||
protected static class UndeclaredCheckedException extends Exception { | ||
@Serial | ||
private static final long serialVersionUID = -4199611059122356651L; | ||
} | ||
|
||
protected static class DeclaredCheckedException extends Exception { | ||
@Serial | ||
private static final long serialVersionUID = 8851375818059230518L; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo perhaps in
cal
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm preparing a revision of this to separate
ClassLoaderAwareGeneratorStrategy
from anUndeclaredThrowableStrategy
delegate, to be committed soon. Noticed this one already, renaming it tocallToSuperSeen
.