Skip to content

Commit

Permalink
Always load java.* classes by delegating to the bootstrap class loader
Browse files Browse the repository at this point in the history
Fixes #34 for real
  • Loading branch information
luontola committed Oct 3, 2014
1 parent 281ef93 commit 6f1891b
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 30 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions end-to-end-tests/end-to-end-tests.iml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<orderEntry type="library" name="Maven: com.google.guava:guava:11.0.2" level="project" />
<orderEntry type="library" name="Maven: com.google.code.findbugs:jsr305:1.3.9" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: commons-lang:commons-lang:2.6" level="project" />
<orderEntry type="library" name="Maven: net.orfjackal.retrolambda:java-lang-dummies:1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.11" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-library:1.3" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
Expand Down
9 changes: 9 additions & 0 deletions end-to-end-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@
<scope>test</scope>
</dependency>

<!-- See net.orfjackal.retrolambda.test.ClasspathTest#ignores_classes_in_explicit_classpath_that_are_under_the_java_package -->
<dependency>
<groupId>net.orfjackal.retrolambda</groupId>
<artifactId>java-lang-dummies</artifactId>
<version>1</version>
<scope>system</scope>
<systemPath>${basedir}/src/test/lib/java-lang-dummies.jar</systemPath>
</dependency>

</dependencies>

<build>
Expand Down
20 changes: 0 additions & 20 deletions end-to-end-tests/src/test/java/java/lang/Math.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<URL> resources = Collections.list(getClass().getClassLoader().getResources("java/lang/Object.class"));
assertThat(resources, (Matcher) hasItem(hasToString(containsString("java-lang-dummies.jar"))));
}
}
Binary file added end-to-end-tests/src/test/lib/java-lang-dummies.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 6f1891b

Please sign in to comment.