Skip to content

Commit

Permalink
Do not generate stack map tables for Java 5 bytecode
Browse files Browse the repository at this point in the history
Fixes #55
  • Loading branch information
luontola committed Jun 7, 2015
1 parent f5b5593 commit dc594da
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 2 deletions.
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 @@ -6,6 +6,7 @@
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
Expand Down
17 changes: 17 additions & 0 deletions end-to-end-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@
</dependencies>

<build>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
<includes>
<include>*.properties</include>
</includes>
</testResource>
<testResource>
<directory>src/test/resources</directory>
<filtering>false</filtering>
<excludes>
<exclude>*.properties</exclude>
</excludes>
</testResource>
</testResources>

<plugins>

<!-- Don't deploy the tests to Maven Central -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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.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);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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.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"));
}
}
1 change: 1 addition & 0 deletions end-to-end-tests/src/test/resources/testing.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
testClassesDir=${project.build.testOutputDirectory}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright © 2013-2014 Esko Luontola <www.orfjackal.net>
// 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;

import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.*;

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

Expand All @@ -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
}
}
}

0 comments on commit dc594da

Please sign in to comment.