Allow ResourcesClassLoader to participate in CGLIB's cache

This commit updates ResourcesClassLoader to be a SmartClassLoader and
expose the original ClassLoader. This has the effect of making sure
proxies that are created by CGLIB can be reused against different tests
rather than having CGLIB trying to define the class again and fail.

Closes gh-45065
This commit is contained in:
Stéphane Nicoll
2025-04-10 16:22:14 +02:00
parent 19d74cfb7f
commit b4c6c55493

View File

@@ -19,16 +19,19 @@ package org.springframework.boot.testsupport.classpath.resources;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URL;
import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import org.springframework.core.SmartClassLoader;
/**
* A {@link ClassLoader} that provides access to {@link Resources resources}.
*
* @author Andy Wilkinson
*/
class ResourcesClassLoader extends ClassLoader {
class ResourcesClassLoader extends ClassLoader implements SmartClassLoader {
private final Resources resources;
@@ -66,4 +69,14 @@ class ResourcesClassLoader extends ClassLoader {
return Collections.enumeration(urls);
}
@Override
public ClassLoader getOriginalClassLoader() {
return getParent();
}
@Override
public Class<?> publicDefineClass(String name, byte[] b, ProtectionDomain protectionDomain) {
return defineClass(name, b, 0, b.length, protectionDomain);
}
}