diff --git a/end-to-end-tests/end-to-end-tests.iml b/end-to-end-tests/end-to-end-tests.iml index 06e4dc63..2143fa82 100644 --- a/end-to-end-tests/end-to-end-tests.iml +++ b/end-to-end-tests/end-to-end-tests.iml @@ -6,6 +6,7 @@ + diff --git a/end-to-end-tests/pom.xml b/end-to-end-tests/pom.xml index 1fae84d6..db80ab04 100644 --- a/end-to-end-tests/pom.xml +++ b/end-to-end-tests/pom.xml @@ -40,6 +40,23 @@ + + + src/test/resources + true + + *.properties + + + + src/test/resources + false + + *.properties + + + + diff --git a/end-to-end-tests/src/test/java/net/orfjackal/retrolambda/test/Java5BytecodeTest.java b/end-to-end-tests/src/test/java/net/orfjackal/retrolambda/test/Java5BytecodeTest.java new file mode 100644 index 00000000..482aa16f --- /dev/null +++ b/end-to-end-tests/src/test/java/net/orfjackal/retrolambda/test/Java5BytecodeTest.java @@ -0,0 +1,47 @@ +// 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.test; + +import com.google.common.io.CharStreams; +import org.apache.commons.lang.SystemUtils; +import org.junit.Test; + +import java.io.*; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + +public class Java5BytecodeTest { + + @Test + public void does_not_generate_stack_map_tables_for_Java_5() throws IOException { + String javapOutput = javap(Dummy.class); + + if (SystemUtils.isJavaVersionAtLeast(1.6f)) { + assertThat(javapOutput, containsString("StackMap")); + } else { + assertThat(javapOutput, not(containsString("StackMap"))); + } + } + + private static String javap(Class aClass) throws IOException { + Process process = new ProcessBuilder() + .directory(TestEnv.testClassesDir) + .command("javap", "-v", "-p", aClass.getName()) + .redirectErrorStream(true) + .start(); + return CharStreams.toString(new InputStreamReader(process.getInputStream())); + } + + + public static class Dummy { + public Dummy() { + // cause this method to have a stack map table + for (int i = 0; i < 3; i++) { + System.out.println(i); + } + } + } +} diff --git a/end-to-end-tests/src/test/java/net/orfjackal/retrolambda/test/TestEnv.java b/end-to-end-tests/src/test/java/net/orfjackal/retrolambda/test/TestEnv.java new file mode 100644 index 00000000..12496300 --- /dev/null +++ b/end-to-end-tests/src/test/java/net/orfjackal/retrolambda/test/TestEnv.java @@ -0,0 +1,23 @@ +// 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.test; + +import java.io.*; +import java.util.Properties; + +public class TestEnv { + + public static final File testClassesDir; + + static { + Properties p = new Properties(); + try (InputStream in = TestEnv.class.getResourceAsStream("/testing.properties")) { + p.load(in); + } catch (IOException e) { + throw new RuntimeException(e); + } + testClassesDir = new File(p.getProperty("testClassesDir")); + } +} diff --git a/end-to-end-tests/src/test/resources/testing.properties b/end-to-end-tests/src/test/resources/testing.properties new file mode 100644 index 00000000..71404202 --- /dev/null +++ b/end-to-end-tests/src/test/resources/testing.properties @@ -0,0 +1 @@ +testClassesDir=${project.build.testOutputDirectory} diff --git a/retrolambda/src/main/java/net/orfjackal/retrolambda/LowerBytecodeVersion.java b/retrolambda/src/main/java/net/orfjackal/retrolambda/LowerBytecodeVersion.java index 90543c1d..a9016630 100644 --- a/retrolambda/src/main/java/net/orfjackal/retrolambda/LowerBytecodeVersion.java +++ b/retrolambda/src/main/java/net/orfjackal/retrolambda/LowerBytecodeVersion.java @@ -1,10 +1,10 @@ -// Copyright © 2013-2014 Esko Luontola +// 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; -import org.objectweb.asm.ClassVisitor; +import org.objectweb.asm.*; import static org.objectweb.asm.Opcodes.ASM5; @@ -24,4 +24,26 @@ public void visit(int version, int access, String name, String signature, String } super.visit(version, access, name, signature, superName, interfaces); } + + @Override + public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { + MethodVisitor next = super.visitMethod(access, name, desc, signature, exceptions); + if (targetVersion <= Opcodes.V1_5) { + return new RemoveMethodFrames(next); + } else { + return next; + } + } + + private static class RemoveMethodFrames extends MethodVisitor { + + public RemoveMethodFrames(MethodVisitor next) { + super(Opcodes.ASM5, next); + } + + @Override + public void visitFrame(int type, int nLocal, Object[] local, int nStack, Object[] stack) { + // remove frame + } + } }