diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 36eec51a..cd0eae5f 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -18,22 +18,22 @@ - - + + - - + + - - + + diff --git a/README.md b/README.md index cb22bc5b..a1b6e463 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/end-to-end-tests/end-to-end-tests.iml b/end-to-end-tests/end-to-end-tests.iml index 2143fa82..a8c6d639 100644 --- a/end-to-end-tests/end-to-end-tests.iml +++ b/end-to-end-tests/end-to-end-tests.iml @@ -14,6 +14,7 @@ + diff --git a/end-to-end-tests/pom.xml b/end-to-end-tests/pom.xml index 96fa260c..fa3ddd1d 100644 --- a/end-to-end-tests/pom.xml +++ b/end-to-end-tests/pom.xml @@ -28,6 +28,12 @@ test + + org.ow2.asm + asm-debug-all + test + + net.orfjackal.retrolambda diff --git a/end-to-end-tests/src/test/java/net/orfjackal/retrolambda/test/LambdaTest.java b/end-to-end-tests/src/test/java/net/orfjackal/retrolambda/test/LambdaTest.java index a8527bbb..700ad4aa 100644 --- a/end-to-end-tests/src/test/java/net/orfjackal/retrolambda/test/LambdaTest.java +++ b/end-to-end-tests/src/test/java/net/orfjackal/retrolambda/test/LambdaTest.java @@ -4,8 +4,12 @@ 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; @@ -13,6 +17,7 @@ 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 { @@ -260,6 +265,19 @@ Callable 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 { diff --git a/parent/pom.xml b/parent/pom.xml index 36450a85..b4a5aae6 100644 --- a/parent/pom.xml +++ b/parent/pom.xml @@ -47,6 +47,8 @@ 1.7 false true + + true @@ -167,7 +169,7 @@ **/*Test.class - true + ${maven.test.redirectTestOutputToFile} @@ -177,7 +179,7 @@ **/*Test.class - true + ${maven.test.redirectTestOutputToFile} ${testJavaHome}/bin/java diff --git a/retrolambda/src/main/java/net/orfjackal/retrolambda/Transformers.java b/retrolambda/src/main/java/net/orfjackal/retrolambda/Transformers.java index 39bc51c9..577f8499 100644 --- a/retrolambda/src/main/java/net/orfjackal/retrolambda/Transformers.java +++ b/retrolambda/src/main/java/net/orfjackal/retrolambda/Transformers.java @@ -113,6 +113,7 @@ private byte[] transform(Consumer 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); diff --git a/retrolambda/src/main/java/net/orfjackal/retrolambda/lambdas/RemoveMethodHandlesLookupReferences.java b/retrolambda/src/main/java/net/orfjackal/retrolambda/lambdas/RemoveMethodHandlesLookupReferences.java new file mode 100644 index 00000000..b5a2f78a --- /dev/null +++ b/retrolambda/src/main/java/net/orfjackal/retrolambda/lambdas/RemoveMethodHandlesLookupReferences.java @@ -0,0 +1,27 @@ +// Copyright © 2013-2015 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 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); + } + } +}