Skip to content

Commit

Permalink
Use a non-delegating class loader to support transforming classes wit…
Browse files Browse the repository at this point in the history
…h same name as JRE's built-in classes

Fixes #29
  • Loading branch information
luontola committed Aug 24, 2014
1 parent 8d5b86d commit 0ec45c6
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ package-private.
Version History
---------------

### Upcoming

- Fixed a crash when trying backport classes that are part of the JRE, but
the JRE's class differs from the class being backported
([Issue #29](https://github.com/orfjackal/retrolambda/issues/29))

### Retrolambda 1.6.0 (2014-08-20)

- Does not anymore require the use of a Java agent
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright © 2013-2014 Esko Luontola <www.orfjackal.net>
// This software is released under the Apache License 2.0.
// The license text is at http://www.apache.org/licenses/LICENSE-2.0

package com.sun.javafx.application;

import net.orfjackal.retrolambda.test.ClasspathTest;

/**
* @see ClasspathTest#prefers_classes_in_explicit_classpath_over_classes_in_the_JRE
*/
@SuppressWarnings("UnusedDeclaration")
public class LauncherImpl {

public Runnable foo() {
return () -> {
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertNotNull;

public class ClasspathTest {

Expand Down Expand Up @@ -45,4 +46,15 @@ public Runnable foo() {
}
new RequiresMainClassesInTestClasspath().foo();
}

/**
* This is to reproduce a bug where trying to backport a development
* version of JavaFX classes fails because the same classes also exist in
* the JRE's extension directory and Retrolambda accidentally loads the
* old built-in class instead of the new class that is being transformed.
*/
@Test
public void prefers_classes_in_explicit_classpath_over_classes_in_the_JRE() {
assertNotNull(getClass().getResource("/com/sun/javafx/application/LauncherImpl$$Lambda$1.class"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright © 2013-2014 Esko Luontola <www.orfjackal.net>
// This software is released under the Apache License 2.0.
// The license text is at http://www.apache.org/licenses/LICENSE-2.0

package net.orfjackal.retrolambda;

import java.net.*;

public class NonDelegatingClassLoader extends URLClassLoader {

public NonDelegatingClassLoader(URL[] urls) {
super(urls);
}

@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
Class<?> c = findLoadedClass(name);
if (c != null) {
return c;
}
try {
return findClass(name);
} catch (ClassNotFoundException e) {
return super.loadClass(name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static void run(Config config) throws Throwable {
return;
}

Thread.currentThread().setContextClassLoader(new URLClassLoader(asUrls(classpath)));
Thread.currentThread().setContextClassLoader(new NonDelegatingClassLoader(asUrls(classpath)));

try (LambdaClassDumper dumper = new LambdaClassDumper(new LambdaClassSaver(outputDir, bytecodeVersion))) {
if (!PreMain.isAgentLoaded()) {
Expand Down

0 comments on commit 0ec45c6

Please sign in to comment.