This commit is contained in:
Clément Fournier
2023-07-02 17:50:21 +02:00
parent b327c13679
commit 11ca2952e5
3 changed files with 58 additions and 2 deletions

View File

@ -116,8 +116,9 @@ public class ClasspathClassLoader extends URLClassLoader {
URL url = findResource(name);
if (url == null) {
// note this will actually call back into findResource, but
// we can't avoid this as the super call
// note this will actually call back into this.findResource, but
// we can't avoid this as the super implementation uses JDK internal
// stuff that we can't copy down here.
return super.getResource(name);
}
return url;

View File

@ -0,0 +1,55 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.symbols;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.hasSize;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import org.junit.jupiter.api.Test;
import net.sourceforge.pmd.PMDConfiguration;
import net.sourceforge.pmd.internal.util.ClasspathClassLoader;
import net.sourceforge.pmd.lang.java.types.JClassType;
import net.sourceforge.pmd.lang.java.types.TypeSystem;
/**
* @author Clément Fournier
*/
class ClassLoadingChildFirstTest {
/**
* In this test I packed a jar with a custom version of {@link Void}.
* The test asserts that when putting that jar on the auxclasspath,
* a {@link TypeSystem} prefers that custom class to the one on the
* bootstrap classpath. The functionality under test is actually in
* {@link ClasspathClassLoader}.
*/
@Test
void testClassLoading() {
String jarPath = getClass().getPackage().getName().replace('.', '/') + "/custom_java_lang.jar";
URL url = Thread.currentThread().getContextClassLoader().getResource(jarPath);
Path file = Paths.get(url.getPath());
PMDConfiguration config = new PMDConfiguration();
config.prependAuxClasspath(file.toAbsolutePath().toString());
TypeSystem typeSystem = TypeSystem.usingClassLoaderClasspath(config.getClassLoader());
JClassType voidClass = typeSystem.BOXED_VOID;
List<JMethodSymbol> declaredMethods = voidClass.getSymbol().getDeclaredMethods();
assertThat(declaredMethods, hasSize(1));
assertThat(declaredMethods.get(0), hasProperty("simpleName", equalTo("customMethodOnJavaLangVoid")));
}
}