Use ClassLoaderAwareGeneratorStrategy with UndeclaredThrowableStrategy delegate
See gh-32469
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* 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.
|
||||
@@ -16,10 +16,6 @@
|
||||
|
||||
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.
|
||||
@@ -29,19 +25,37 @@ import org.springframework.cglib.transform.impl.UndeclaredThrowableStrategy;
|
||||
* @author Juergen Hoeller
|
||||
* @since 5.2
|
||||
*/
|
||||
public class ClassLoaderAwareGeneratorStrategy extends UndeclaredThrowableStrategy {
|
||||
public class ClassLoaderAwareGeneratorStrategy extends DefaultGeneratorStrategy {
|
||||
|
||||
private final ClassLoader classLoader;
|
||||
|
||||
private final GeneratorStrategy delegate;
|
||||
|
||||
|
||||
/**
|
||||
* Create a default GeneratorStrategy, exposing the given ClassLoader.
|
||||
* @param classLoader the ClassLoader to expose as current thread context ClassLoader
|
||||
*/
|
||||
public ClassLoaderAwareGeneratorStrategy(ClassLoader classLoader) {
|
||||
super(UndeclaredThrowableException.class);
|
||||
this.classLoader = classLoader;
|
||||
this.delegate = super::generate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a decorator for the given GeneratorStrategy delegate, exposing the given ClassLoader.
|
||||
* @param classLoader the ClassLoader to expose as current thread context ClassLoader
|
||||
* @since 6.2
|
||||
*/
|
||||
public ClassLoaderAwareGeneratorStrategy(ClassLoader classLoader, GeneratorStrategy delegate) {
|
||||
this.classLoader = classLoader;
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public byte[] generate(ClassGenerator cg) throws Exception {
|
||||
if (this.classLoader == null) {
|
||||
return super.generate(cg);
|
||||
return this.delegate.generate(cg);
|
||||
}
|
||||
|
||||
Thread currentThread = Thread.currentThread();
|
||||
@@ -51,7 +65,7 @@ public class ClassLoaderAwareGeneratorStrategy extends UndeclaredThrowableStrate
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
// Cannot access thread context ClassLoader - falling back...
|
||||
return super.generate(cg);
|
||||
return this.delegate.generate(cg);
|
||||
}
|
||||
|
||||
boolean overrideClassLoader = !this.classLoader.equals(threadContextClassLoader);
|
||||
@@ -59,7 +73,7 @@ public class ClassLoaderAwareGeneratorStrategy extends UndeclaredThrowableStrate
|
||||
currentThread.setContextClassLoader(this.classLoader);
|
||||
}
|
||||
try {
|
||||
return super.generate(cg);
|
||||
return this.delegate.generate(cg);
|
||||
}
|
||||
finally {
|
||||
if (overrideClassLoader) {
|
||||
|
||||
@@ -27,7 +27,7 @@ import org.springframework.cglib.core.Signature;
|
||||
import org.springframework.cglib.core.TypeUtils;
|
||||
import org.springframework.cglib.transform.ClassEmitterTransformer;
|
||||
|
||||
@SuppressWarnings({"rawtypes" })
|
||||
@SuppressWarnings("rawtypes")
|
||||
public class UndeclaredThrowableTransformer extends ClassEmitterTransformer {
|
||||
|
||||
private final Type wrapper;
|
||||
@@ -57,18 +57,16 @@ public class UndeclaredThrowableTransformer extends ClassEmitterTransformer {
|
||||
return new CodeEmitter(e) {
|
||||
private final boolean isConstructor = Constants.CONSTRUCTOR_NAME.equals(sig.getName());
|
||||
private Block handler = begin_block();
|
||||
private boolean calToSuperWasSeen;
|
||||
|
||||
private boolean callToSuperSeen;
|
||||
@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)) {
|
||||
if (isConstructor && !callToSuperSeen && Constants.CONSTRUCTOR_NAME.equals(name)) {
|
||||
// we start the entry in the exception table after the call to super
|
||||
handler = begin_block();
|
||||
calToSuperWasSeen = true;
|
||||
callToSuperSeen = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitMaxs(int maxStack, int maxLocals) {
|
||||
handler.end();
|
||||
|
||||
Reference in New Issue
Block a user