From 11ca2952e5a379b6bbc94a024b5c29504d6fad1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Sun, 2 Jul 2023 17:50:21 +0200 Subject: [PATCH] Add test --- .../internal/util/ClasspathClassLoader.java | 5 +- .../symbols/ClassLoadingChildFirstTest.java | 55 ++++++++++++++++++ .../lang/java/symbols/custom_java_lang.jar | Bin 0 -> 1359 bytes 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 pmd-java/src/test/java/net/sourceforge/pmd/lang/java/symbols/ClassLoadingChildFirstTest.java create mode 100644 pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/symbols/custom_java_lang.jar diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/internal/util/ClasspathClassLoader.java b/pmd-core/src/main/java/net/sourceforge/pmd/internal/util/ClasspathClassLoader.java index 4a17f6550a..024542c01f 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/internal/util/ClasspathClassLoader.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/internal/util/ClasspathClassLoader.java @@ -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; diff --git a/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/symbols/ClassLoadingChildFirstTest.java b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/symbols/ClassLoadingChildFirstTest.java new file mode 100644 index 0000000000..692edadee8 --- /dev/null +++ b/pmd-java/src/test/java/net/sourceforge/pmd/lang/java/symbols/ClassLoadingChildFirstTest.java @@ -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 declaredMethods = voidClass.getSymbol().getDeclaredMethods(); + assertThat(declaredMethods, hasSize(1)); + assertThat(declaredMethods.get(0), hasProperty("simpleName", equalTo("customMethodOnJavaLangVoid"))); + + } + +} diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/symbols/custom_java_lang.jar b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/symbols/custom_java_lang.jar new file mode 100644 index 0000000000000000000000000000000000000000..5b665d80562c24cf3bc40c6d7b57cb3b48c6b53d GIT binary patch literal 1359 zcmWIWW@Zs#;Nak3INtLpi~$L7GO#fCx`sIFdiuHP|2xINz|0Wf&CUT*!30$nfK#&w zPz7AGucM!*n`>~0p0C?y-!rFuymj?1@_OrPojY@WbCAIm;|EWR^t^m^Jbf>gu43Vg zcp-U2I#NP2*gLshnB=O{W-Up*q-$);Xz$I+%3s$YuX?TL zk&sP^=+{;ERxiDOFFuY@CgE~ptEBYi8LJcS9b^*I)w!vWc=HgK^T$`eKFvs6ReG3x zk6hu^weqeuo>Dwz(Pg6MnK}|??Vr!aJGh_TQ0UQ96s(oWC&_i&TK1CNCB;JZsG!cz zTi$p7==v?F=zyGt3e}$vJ+d}aaWA|C@O4}anj5yAA_{V+G zn2-cxpMML)|Mha5kzY|eUE<<$-cqm5o9DGXV)yqfHOdv{zPH@j=RdP?#P&!L%}2Ta zt>l+3ePvc=bz^z{#uv9G#5PBsuome}5MkZ4#YFLO%bLmQPrP@ZPxx+`*tEvk)j`r# zXrtlJ*nLwUH~ejwnDvnJXkwT1UALFMwZU0C?K(rf`!)VwnO?ldW7{l$v%id>T(gjapX2c{L`kB?@UT%1v$6W#jq z28XtH$H%*}9u_yV|Nnn|UF;~gc9VM58IxIB{foBgZx#9wz-G$Pd+y*}Ezb8Ght5PF zKjEEW-gYfHgM0go|oLE+*;J4SWGG&&Sr`2J*i}K6l`#bM#%=J66b!vJsN7eC! z?Nh?9A2c+b`PPy<fr{^0=nI>4KeNrVA4Kf^LBC_keDcm@UKZgj24ISiEe5x^G6glk30+aMFT z7;xuAgb9a%OlZDDXu*~<5n7(Xv_SJI!k?g=iX7{xj^zV}3buTU>|IdmKn_Jv>Og=y azyL%u0mKdPW@Q5@Vgtf#pe5&+K|BC&4WTmt literal 0 HcmV?d00001