From 6f1891bd75575427bc124702f71786834148d022 Mon Sep 17 00:00:00 2001 From: Esko Luontola Date: Fri, 3 Oct 2014 21:23:05 +0300 Subject: [PATCH] Always load java.* classes by delegating to the bootstrap class loader Fixes #34 for real --- ...jackal_retrolambda_java_lang_dummies_1.xml | 9 ++++++++ end-to-end-tests/end-to-end-tests.iml | 1 + end-to-end-tests/pom.xml | 9 ++++++++ .../src/test/java/java/lang/Math.java | 20 ------------------ .../retrolambda/test/ClasspathTest.java | 14 ++++++++---- .../src/test/lib/java-lang-dummies.jar | Bin 0 -> 823 bytes .../retrolambda/NonDelegatingClassLoader.java | 3 +++ .../interfaces/ClassHierarchyAnalyzer.java | 7 +----- 8 files changed, 33 insertions(+), 30 deletions(-) create mode 100644 .idea/libraries/Maven__net_orfjackal_retrolambda_java_lang_dummies_1.xml delete mode 100644 end-to-end-tests/src/test/java/java/lang/Math.java create mode 100644 end-to-end-tests/src/test/lib/java-lang-dummies.jar diff --git a/.idea/libraries/Maven__net_orfjackal_retrolambda_java_lang_dummies_1.xml b/.idea/libraries/Maven__net_orfjackal_retrolambda_java_lang_dummies_1.xml new file mode 100644 index 00000000..1b9d58d2 --- /dev/null +++ b/.idea/libraries/Maven__net_orfjackal_retrolambda_java_lang_dummies_1.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/end-to-end-tests/end-to-end-tests.iml b/end-to-end-tests/end-to-end-tests.iml index a5a166c5..465b63ba 100644 --- a/end-to-end-tests/end-to-end-tests.iml +++ b/end-to-end-tests/end-to-end-tests.iml @@ -13,6 +13,7 @@ + diff --git a/end-to-end-tests/pom.xml b/end-to-end-tests/pom.xml index 0a379bc0..31d9ea96 100644 --- a/end-to-end-tests/pom.xml +++ b/end-to-end-tests/pom.xml @@ -28,6 +28,15 @@ test + + + net.orfjackal.retrolambda + java-lang-dummies + 1 + system + ${basedir}/src/test/lib/java-lang-dummies.jar + + diff --git a/end-to-end-tests/src/test/java/java/lang/Math.java b/end-to-end-tests/src/test/java/java/lang/Math.java deleted file mode 100644 index 1a3f938d..00000000 --- a/end-to-end-tests/src/test/java/java/lang/Math.java +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright © 2013-2014 Esko Luontola -// This software is released under the Apache License 2.0. -// The license text is at http://www.apache.org/licenses/LICENSE-2.0 - -package java.lang; - -import net.orfjackal.retrolambda.test.ClasspathTest; - -/** - * @see ClasspathTest#ignores_classes_in_explicit_classpath_that_are_under_the_java_package - */ -@SuppressWarnings("UnusedDeclaration") -public class Math { - - public Math() { - // some lambdas to cause Retrolambda try backporting this class - Runnable r = () -> { - }; - } -} diff --git a/end-to-end-tests/src/test/java/net/orfjackal/retrolambda/test/ClasspathTest.java b/end-to-end-tests/src/test/java/net/orfjackal/retrolambda/test/ClasspathTest.java index 86c71fdb..84177708 100644 --- a/end-to-end-tests/src/test/java/net/orfjackal/retrolambda/test/ClasspathTest.java +++ b/end-to-end-tests/src/test/java/net/orfjackal/retrolambda/test/ClasspathTest.java @@ -7,10 +7,12 @@ import org.hamcrest.*; import org.junit.Test; -import java.util.Arrays; +import java.io.IOException; +import java.net.URL; +import java.util.*; import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.*; import static org.junit.Assert.assertNotNull; public class ClasspathTest { @@ -65,7 +67,11 @@ public void prefers_classes_in_explicit_classpath_over_classes_in_the_JRE() { * contains {@code java.*} classes. */ @Test - public void ignores_classes_in_explicit_classpath_that_are_under_the_java_package() { - assertNotNull(getClass().getResource("/java/lang/Math.class")); + public void ignores_classes_in_explicit_classpath_that_are_under_the_java_package() throws IOException { + // We have a JAR on the classpath that contains dummy version of java.lang.Object, + // the same way as android.jar, which causes Retrolambda to try loading that class + // because the classes to be backported extend it implicitly. + List resources = Collections.list(getClass().getClassLoader().getResources("java/lang/Object.class")); + assertThat(resources, (Matcher) hasItem(hasToString(containsString("java-lang-dummies.jar")))); } } diff --git a/end-to-end-tests/src/test/lib/java-lang-dummies.jar b/end-to-end-tests/src/test/lib/java-lang-dummies.jar new file mode 100644 index 0000000000000000000000000000000000000000..4fc14fd6c0b7e3c2b8d35fbc7a646fdffc0e1f10 GIT binary patch literal 823 zcmWIWW@Zs#;Nak3IK9l-l>rHGGO#fCx`sIFdiuHP|2xINz|0Wf&CUT*!30$nfK#&w zPz7AGucM!*n`>~0p0C?y-!rFuymj?1@_OrPojY@WbCAIm;|EWR^t^m^Jbf>gu43Vg zcp-U2T2dq9hq$WR^Cemq&&5Tbh^tOxn40~m=wlHh*b(Zh8M2*$R=5B$$Pru&3_yoX zLh}JDP&O;EEKwh~5}-zilAOf6bg()|V4xc%hN8|tDJwO(L@zlfvAEd!YyNdRHa0e4 zHn}pkJU2G8*=P2g5wdn;OXEFj;KnBQ{LhpSH@3R@XZD2bzjSBGl|>VdERk8zF~PsC zD9JA(DNwD0Uo=JYNMYvQz?{j(CfA`d;W*F| literal 0 HcmV?d00001 diff --git a/retrolambda/src/main/java/net/orfjackal/retrolambda/NonDelegatingClassLoader.java b/retrolambda/src/main/java/net/orfjackal/retrolambda/NonDelegatingClassLoader.java index edfc58e4..fb44ed0a 100644 --- a/retrolambda/src/main/java/net/orfjackal/retrolambda/NonDelegatingClassLoader.java +++ b/retrolambda/src/main/java/net/orfjackal/retrolambda/NonDelegatingClassLoader.java @@ -14,6 +14,9 @@ public NonDelegatingClassLoader(URL[] urls) { @Override public Class loadClass(String name) throws ClassNotFoundException { + if (name.startsWith("java.")) { // the java.* classes can only be loaded by the bootstrap class loader + return super.loadClass(name); + } Class c = findLoadedClass(name); if (c != null) { return c; diff --git a/retrolambda/src/main/java/net/orfjackal/retrolambda/interfaces/ClassHierarchyAnalyzer.java b/retrolambda/src/main/java/net/orfjackal/retrolambda/interfaces/ClassHierarchyAnalyzer.java index 39d5fd12..202de705 100644 --- a/retrolambda/src/main/java/net/orfjackal/retrolambda/interfaces/ClassHierarchyAnalyzer.java +++ b/retrolambda/src/main/java/net/orfjackal/retrolambda/interfaces/ClassHierarchyAnalyzer.java @@ -23,12 +23,7 @@ public class ClassHierarchyAnalyzer implements MethodRelocations { public void analyze(byte[] bytecode) { ClassReader cr = new ClassReader(bytecode); - String className = cr.getClassName(); - if (className.startsWith("java/")) { - // the JVM disallows user classes in java.* packages, so don't even try backporting them - return; - } - Type clazz = classNameToType(className); + Type clazz = classNameToType(cr.getClassName()); if (Flags.hasFlag(cr.getAccess(), ACC_INTERFACE)) { interfaces.add(cr);