Consistent handling of undeclared checked exceptions in CGLIB proxies (#32469)

Co-authored-by: hengyunabc <hengyunabc@gmail.com>
Co-authored-by: Mikaël Francoeur <mikael.francoeur@ticketmaster.com>
This commit is contained in:
Mikaël Francoeur
2024-04-10 06:11:33 -04:00
committed by GitHub
parent bed3689001
commit 561583842f
5 changed files with 232 additions and 56 deletions

View File

@@ -16,6 +16,10 @@
package org.springframework.cglib.core;
import java.lang.reflect.UndeclaredThrowableException;
import org.springframework.cglib.transform.impl.UndeclaredThrowableStrategy;
/**
* CGLIB GeneratorStrategy variant which exposes the application ClassLoader
* as current thread context ClassLoader for the time of class generation.
@@ -25,11 +29,12 @@ package org.springframework.cglib.core;
* @author Juergen Hoeller
* @since 5.2
*/
public class ClassLoaderAwareGeneratorStrategy extends DefaultGeneratorStrategy {
public class ClassLoaderAwareGeneratorStrategy extends UndeclaredThrowableStrategy {
private final ClassLoader classLoader;
public ClassLoaderAwareGeneratorStrategy(ClassLoader classLoader) {
super(UndeclaredThrowableException.class);
this.classLoader = classLoader;
}

View File

@@ -27,7 +27,7 @@ import org.springframework.cglib.core.Signature;
import org.springframework.cglib.core.TypeUtils;
import org.springframework.cglib.transform.ClassEmitterTransformer;
@SuppressWarnings({"rawtypes", "unchecked"})
@SuppressWarnings({"rawtypes" })
public class UndeclaredThrowableTransformer extends ClassEmitterTransformer {
private final Type wrapper;
@@ -55,10 +55,20 @@ public class UndeclaredThrowableTransformer extends ClassEmitterTransformer {
return e;
}
return new CodeEmitter(e) {
private Block handler;
/* init */ {
handler = begin_block();
}
private final boolean isConstructor = Constants.CONSTRUCTOR_NAME.equals(sig.getName());
private Block handler = begin_block();
private boolean calToSuperWasSeen;
@Override
public void visitMethodInsn(int opcode, String owner, String name, String descriptor, boolean isInterface) {
super.visitMethodInsn(opcode, owner, name, descriptor, isInterface);
if (isConstructor && !calToSuperWasSeen && Constants.CONSTRUCTOR_NAME.equals(name)) {
// we start the entry in the exception table after the call to super
handler = begin_block();
calToSuperWasSeen = true;
}
}
@Override
public void visitMaxs(int maxStack, int maxLocals) {
handler.end();