Skip to content

Commit

Permalink
Removes bytecode references to `java.lang.invoke.MethodHandles.Lookup…
Browse files Browse the repository at this point in the history
…` on Java 6 and older

Fixes #61
  • Loading branch information
luontola committed Jul 8, 2015
1 parent a13bc98 commit 6f49e17
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 8 deletions.
12 changes: 6 additions & 6 deletions .idea/compiler.xml

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

3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ Version History
([Issue #56](https://github.com/orfjackal/retrolambda/issues/56))
- Fixed method references to constructors of the current class
([Issue #60](https://github.com/orfjackal/retrolambda/issues/60))
- Removes bytecode references to `java.lang.invoke.MethodHandles.Lookup` on
Java 6 and older
([Issue #61](https://github.com/orfjackal/retrolambda/issues/61))

### Retrolambda 2.0.3 (2015-06-07)

Expand Down
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 @@ -14,6 +14,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" scope="TEST" name="Maven: org.ow2.asm:asm-debug-all:5.0.4" level="project" />
<orderEntry type="module-library">
<library name="Maven: net.orfjackal.retrolambda:java-lang-dummies:1">
<CLASSES>
Expand Down
6 changes: 6 additions & 0 deletions end-to-end-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-debug-all</artifactId>
<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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@

package net.orfjackal.retrolambda.test;

import org.apache.commons.lang.SystemUtils;
import org.junit.Test;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.tree.*;

import java.io.IOException;
import java.lang.reflect.*;
import java.util.*;
import java.util.concurrent.Callable;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeThat;

public class LambdaTest extends SuperClass {

Expand Down Expand Up @@ -260,6 +265,19 @@ Callable<String> childRef() {
assertThat(((Parent) child).privateMethod(), is("parent version"));
assertThat(child.parentRef().call(), is("parent version"));
}

@Test
public void bytecode_will_not_contain_dangling_references_to_MethodHandles() throws IOException {
assumeThat(SystemUtils.JAVA_VERSION_FLOAT, is(lessThan(1.7f)));

ClassReader cr = new ClassReader(getClass().getName().replace('.', '/'));
ClassNode cn = new ClassNode();
cr.accept(cn, ClassReader.SKIP_CODE);

for (InnerClassNode innerClass : cn.innerClasses) {
assertThat(innerClass.name, not(startsWith("java/lang/invoke")));
}
}
}

class SuperClass {
Expand Down
6 changes: 4 additions & 2 deletions parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
<testBytecodeTarget>1.7</testBytecodeTarget>
<testFork>false</testFork>
<testDefaultMethods>true</testDefaultMethods>
<!-- Override the default value of this property, but allow changing it on the command line -->
<maven.test.redirectTestOutputToFile>true</maven.test.redirectTestOutputToFile>
</properties>

<prerequisites>
Expand Down Expand Up @@ -167,7 +169,7 @@
<includes>
<include>**/*Test.class</include>
</includes>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
<redirectTestOutputToFile>${maven.test.redirectTestOutputToFile}</redirectTestOutputToFile>
</configuration>
</plugin>

Expand All @@ -177,7 +179,7 @@
<includes>
<include>**/*Test.class</include>
</includes>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
<redirectTestOutputToFile>${maven.test.redirectTestOutputToFile}</redirectTestOutputToFile>
<jvm>${testJavaHome}/bin/java</jvm>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ private byte[] transform(Consumer<ClassVisitor> reader, ClassVisitorChain chain)
next = new LowerBytecodeVersion(next, targetVersion);
if (targetVersion < Opcodes.V1_7) {
next = new SwallowSuppressedExceptions(next);
next = new RemoveMethodHandlesLookupReferences(next);
}
next = new FixInvokeStaticOnInterfaceMethod(next);
next = chain.wrap(next);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright © 2013-2015 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.lambdas;

import org.objectweb.asm.*;

import java.lang.invoke.MethodHandles;

import static org.objectweb.asm.Opcodes.ASM5;

public class RemoveMethodHandlesLookupReferences extends ClassVisitor {

private static final String METHOD_HANDLES_LOOKUP = Type.getType(MethodHandles.Lookup.class).getInternalName();

public RemoveMethodHandlesLookupReferences(ClassVisitor next) {
super(ASM5, next);
}

@Override
public void visitInnerClass(String name, String outerName, String innerName, int access) {
if (!name.equals(METHOD_HANDLES_LOOKUP)) {
super.visitInnerClass(name, outerName, innerName, access);
}
}
}

0 comments on commit 6f49e17

Please sign in to comment.