8144168: No type annotations generated for nested lambdas
authorsadayapalam
Thu, 28 Jan 2016 09:09:34 +0530
changeset 35418 8681d57fc3f8
parent 35417 ec74f22cf4c6
child 35421 145c5b260ea1
8144168: No type annotations generated for nested lambdas Reviewed-by: jlahoda
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Gen.java
langtools/test/tools/javac/MethodParameters/LambdaTest.out
langtools/test/tools/javac/annotations/typeAnnotations/classfile/NestedLambdasCastedTest.java
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java	Tue Jan 26 12:11:55 2016 +0000
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java	Thu Jan 28 09:09:34 2016 +0530
@@ -1812,7 +1812,7 @@
 
             TranslationContext(T tree) {
                 this.tree = tree;
-                this.owner = owner();
+                this.owner = owner(true);
                 this.depth = frameStack.size() - 1;
                 this.prev = context();
                 ClassSymbol csym =
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Gen.java	Tue Jan 26 12:11:55 2016 +0000
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Gen.java	Thu Jan 28 09:09:34 2016 +0530
@@ -2173,8 +2173,8 @@
         }
 
     public void visitTypeCast(JCTypeCast tree) {
+        result = genExpr(tree.expr, tree.clazz.type).load();
         setTypeAnnotationPositions(tree.pos);
-        result = genExpr(tree.expr, tree.clazz.type).load();
         // Additional code is only needed if we cast to a reference type
         // which is not statically a supertype of the expression's type.
         // For basic types, the coerce(...) in genExpr(...) will do
@@ -2191,8 +2191,8 @@
     }
 
     public void visitTypeTest(JCInstanceOf tree) {
+        genExpr(tree.expr, tree.expr.type).load();
         setTypeAnnotationPositions(tree.pos);
-        genExpr(tree.expr, tree.expr.type).load();
         code.emitop2(instanceof_, makeRef(tree.pos(), tree.clazz.type));
         result = items.makeStackItem(syms.booleanType);
     }
--- a/langtools/test/tools/javac/MethodParameters/LambdaTest.out	Tue Jan 26 12:11:55 2016 +0000
+++ b/langtools/test/tools/javac/MethodParameters/LambdaTest.out	Thu Jan 28 09:09:34 2016 +0530
@@ -2,6 +2,6 @@
 LambdaTest.<init>()
 LambdaTest.foo(i)
 LambdaTest.lambda$static$1(arg0)/*synthetic*/
-LambdaTest.lambda$null$0(arg0, arg1)/*synthetic*/
+LambdaTest.lambda$static$0(arg0, arg1)/*synthetic*/
 static interface LambdaTest$I -- inner
 LambdaTest$I.m(x)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/annotations/typeAnnotations/classfile/NestedLambdasCastedTest.java	Thu Jan 28 09:09:34 2016 +0530
@@ -0,0 +1,85 @@
+/*
+ * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8144168
+ * @summary No type annotations generated for nested lambdas
+ * @library /tools/lib
+ * @modules jdk.compiler/com.sun.tools.javac.api
+ *          jdk.compiler/com.sun.tools.javac.file
+ *          jdk.compiler/com.sun.tools.javac.main
+ * @build ToolBox
+ * @run compile NestedLambdasCastedTest.java
+ * @run main NestedLambdasCastedTest
+ */
+
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Target;
+
+public class NestedLambdasCastedTest {
+
+    // Expected output can't be directly encoded into NestedLambdasCastedTest !!!
+    static class ExpectedOutputHolder {
+        public String [] outputs = {
+                      "public static strictfp void main(java.lang.String[])",
+                      "private static strictfp void lambda$main$3();",
+                      "private static strictfp void lambda$main$2();",
+                      "private static strictfp void lambda$main$1();",
+                      "private static strictfp void lambda$main$0();",
+                      "0: #63(#64=s#65): CAST, offset=5, type_index=0",
+                      "0: #63(#64=s#70): CAST, offset=5, type_index=0",
+                      "0: #63(#64=s#73): CAST, offset=5, type_index=0",
+                      "0: #63(#64=s#76): CAST, offset=5, type_index=0"
+        };
+    }
+
+    @Target(ElementType.TYPE_USE)
+    public @interface TA {
+        String value() default "";
+    };
+
+    public static strictfp void main(String args[]) throws Exception {
+        Runnable one = (@TA("1") Runnable) () -> {
+            Runnable two = (@TA("2") Runnable) () -> {
+                Runnable three = (@TA("3") Runnable) () -> {
+                    Runnable four = (@TA("4") Runnable) () -> {
+                    };
+                };
+            };
+        };
+        ToolBox tb = new ToolBox();
+        Path classPath = Paths.get(ToolBox.testClasses, "NestedLambdasCastedTest.class");
+        String javapOut = tb.new JavapTask()
+                .options("-v", "-p")
+                .classes(classPath.toString())
+                .run()
+                .getOutput(ToolBox.OutputKind.DIRECT);
+        ExpectedOutputHolder holder = new ExpectedOutputHolder();
+        for (String s : holder.outputs)
+        if (!javapOut.contains(s))
+            throw new AssertionError("Expected type annotation on LOCAL_VARIABLE missing");
+    }
+}