diff options
author | Liam Miller-Cushon <cushon@google.com> | 2016-11-12 01:13:28 +0000 |
---|---|---|
committer | Kristina Chodorow <kchodorow@google.com> | 2016-11-14 14:56:05 +0000 |
commit | 383b1e4bc0cd2a533508d500483f29a40a12fe1a (patch) | |
tree | 839fc0c6d1d527fe3f9e9763eac5871a09eb89ce /src/java_tools/buildjar/javatests/com | |
parent | 93d63abe44d317644ae36be9078ebfee14e16a46 (diff) |
Don't break lambda bodies during tree pruning
Rewriting block-bodied lambda bodies into empty blocks breaks
breaks overload resolution.
--
MOS_MIGRATED_REVID=138936301
Diffstat (limited to 'src/java_tools/buildjar/javatests/com')
2 files changed, 70 insertions, 23 deletions
diff --git a/src/java_tools/buildjar/javatests/com/google/devtools/build/java/turbine/javac/JavacTurbineTest.java b/src/java_tools/buildjar/javatests/com/google/devtools/build/java/turbine/javac/JavacTurbineTest.java index 39fe109e7c..acc9d595f3 100644 --- a/src/java_tools/buildjar/javatests/com/google/devtools/build/java/turbine/javac/JavacTurbineTest.java +++ b/src/java_tools/buildjar/javatests/com/google/devtools/build/java/turbine/javac/JavacTurbineTest.java @@ -1341,4 +1341,46 @@ public class JavacTurbineTest { }; assertThat(text).isEqualTo(Joiner.on('\n').join(expected)); } + + @Test + public void lambdaBody() throws Exception { + addSourceLines( + "P.java", + "import java.util.function.Predicate;", + "enum P {", + " INSTANCE(x -> {", + " return false;", + " });", + " P(Predicate<String> p) {}", + "}"); + + compile(); + + Map<String, byte[]> outputs = collectOutputs(); + + String text = textify(outputs.get("P.class")); + String[] expected = { + "// class version 52.0 (52)", + "// access flags 0x4030", + "// signature Ljava/lang/Enum<LP;>;", + "// declaration: P extends java.lang.Enum<P>", + "final enum P extends java/lang/Enum {", + "", + " // access flags 0x19", + " public final static INNERCLASS java/lang/invoke/MethodHandles$Lookup" + + " java/lang/invoke/MethodHandles Lookup", + "", + " // access flags 0x4019", + " public final static enum LP; INSTANCE", + "", + " // access flags 0x9", + " public static values()[LP;", + "", + " // access flags 0x9", + " public static valueOf(Ljava/lang/String;)LP;", + "}", + "" + }; + assertThat(text).isEqualTo(Joiner.on('\n').join(expected)); + } } diff --git a/src/java_tools/buildjar/javatests/com/google/devtools/build/java/turbine/javac/TreePrunerTest.java b/src/java_tools/buildjar/javatests/com/google/devtools/build/java/turbine/javac/TreePrunerTest.java index 5f3962b9ad..bebb093e14 100644 --- a/src/java_tools/buildjar/javatests/com/google/devtools/build/java/turbine/javac/TreePrunerTest.java +++ b/src/java_tools/buildjar/javatests/com/google/devtools/build/java/turbine/javac/TreePrunerTest.java @@ -19,7 +19,6 @@ import static com.google.common.truth.Truth.assertThat; import static java.nio.charset.StandardCharsets.UTF_8; import com.google.common.base.Joiner; - import com.sun.tools.javac.file.JavacFileManager; import com.sun.tools.javac.parser.JavacParser; import com.sun.tools.javac.parser.ParserFactory; @@ -27,7 +26,7 @@ import com.sun.tools.javac.tree.JCTree.JCClassDecl; import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; import com.sun.tools.javac.tree.JCTree.JCVariableDecl; import com.sun.tools.javac.util.Context; - +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -36,7 +35,16 @@ import org.junit.runners.JUnit4; @RunWith(JUnit4.class) public class TreePrunerTest { - static JCCompilationUnit parseLines(String... lines) { + Context context; + + @Before + public void setUp() { + this.context = new Context(); + // registers itself in the context as a side effect + new JavacFileManager(context, true, UTF_8); + } + + JCCompilationUnit parseLines(String... lines) { Context context = new Context(); try (JavacFileManager fm = new JavacFileManager(context, true, UTF_8)) { ParserFactory parserFactory = ParserFactory.instance(context); @@ -48,33 +56,31 @@ public class TreePrunerTest { } } - static JCVariableDecl parseField(String line) { + JCVariableDecl parseField(String line) { JCCompilationUnit unit = parseLines("class T {", line, "}"); JCClassDecl classDecl = (JCClassDecl) getOnlyElement(unit.defs); return (JCVariableDecl) getOnlyElement(classDecl.defs); } - static String printPruned(String line) { + String printPruned(String line) { JCVariableDecl tree = parseField(line); - TreePruner.prune(tree); + TreePruner.prune(context, tree); return tree.toString(); } @Test public void hello() { String[] lines = { - "class Test {", - // TODO(cushon): fix google-java-format's handling of lists of string literals + "class Test {", // " void f() {", " System.err.println(\"hello\");", " }", "}", }; JCCompilationUnit tree = parseLines(lines); - TreePruner.prune(tree); + TreePruner.prune(context, tree); String[] expected = { - "class Test {", - // TODO(cushon): fix google-java-format's handling of lists of string literals + "class Test {", // " ", " void f() {", " }", @@ -96,13 +102,14 @@ public class TreePrunerTest { "}", }; JCCompilationUnit tree = parseLines(lines); - TreePruner.prune(tree); + TreePruner.prune(context, tree); String[] expected = { - "class Test {", - // TODO(cushon): fix google-java-format's handling of lists of string literals + "class Test {", // " {", + " throw new AssertionError();", " }", " static {", + " throw new AssertionError();", " }", "}", }; @@ -112,8 +119,7 @@ public class TreePrunerTest { @Test public void nonConstantFieldInitializers() { String[] lines = { - "class Test {", - // TODO(cushon): fix google-java-format's handling of lists of string literals + "class Test {", // " Object a = null;", " int[] b = {1};", " int c = g();", @@ -121,10 +127,9 @@ public class TreePrunerTest { "}", }; JCCompilationUnit tree = parseLines(lines); - TreePruner.prune(tree); + TreePruner.prune(context, tree); String[] expected = { - "class Test {", - // TODO(cushon): fix google-java-format's handling of lists of string literals + "class Test {", // " Object a;", " int[] b;", " int c;", @@ -238,7 +243,7 @@ public class TreePrunerTest { "}", }; JCCompilationUnit tree = parseLines(lines); - TreePruner.prune(tree); + TreePruner.prune(context, tree); String[] expected = { "class Test {", " ", @@ -271,7 +276,7 @@ public class TreePrunerTest { "}", }; JCCompilationUnit tree = parseLines(lines); - TreePruner.prune(tree); + TreePruner.prune(context, tree); String[] expected = { "@interface Anno {", " ", @@ -292,7 +297,7 @@ public class TreePrunerTest { "}", }; JCCompilationUnit tree = parseLines(lines); - TreePruner.prune(tree); + TreePruner.prune(context, tree); String[] expected = { "interface Intf {", " int CONST = 42;", @@ -318,7 +323,7 @@ public class TreePrunerTest { "}", }; JCCompilationUnit tree = parseLines(lines); - TreePruner.prune(tree); + TreePruner.prune(context, tree); String[] expected = { "class Test {", " ", |