8050993: There is no record for condition in ternary operator in LineNumberTable.
authorjlahoda
Mon, 02 May 2016 06:43:44 +0200
changeset 37754 7b4f67ce5cb5
parent 37753 dbc87c966861
child 37755 b0918e7eb16b
8050993: There is no record for condition in ternary operator in LineNumberTable. Summary: Make sure there is an entry in the LineNumberTable for the condition of the ternary operator Reviewed-by: jjg Contributed-by: andrey.x.nazarov@oracle.com, jan.lahoda@oracle.com
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Gen.java
langtools/test/tools/javac/classfiles/attributes/LineNumberTable/LineNumberTestBase.java
langtools/test/tools/javac/classfiles/attributes/LineNumberTable/T8050993.java
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Gen.java	Mon May 02 10:11:24 2016 +0530
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Gen.java	Mon May 02 06:43:44 2016 +0200
@@ -1649,6 +1649,7 @@
 
     public void visitConditional(JCConditional tree) {
         Chain thenExit = null;
+        code.statBegin(tree.cond.pos);
         CondItem c = genCond(tree.cond, CRT_FLOW_CONTROLLER);
         Chain elseChain = c.jumpFalse();
         if (!c.isFalse()) {
--- a/langtools/test/tools/javac/classfiles/attributes/LineNumberTable/LineNumberTestBase.java	Mon May 02 10:11:24 2016 +0530
+++ b/langtools/test/tools/javac/classfiles/attributes/LineNumberTable/LineNumberTestBase.java	Mon May 02 06:43:44 2016 +0200
@@ -191,7 +191,7 @@
         CONDITION("int res = \n" +
                 "testField == 2 ?\n" +
                 "10\n" +
-                ":9;", 1, 3, 4), // see issue https://bugs.openjdk.java.net/browse/JDK-8050993
+                ":9;", 2, 3, 4),
         TRY("try{\n" +
                 "    --testField;\n" +
                 "}\n" +
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/classfiles/attributes/LineNumberTable/T8050993.java	Mon May 02 06:43:44 2016 +0200
@@ -0,0 +1,53 @@
+/*
+ * @test /nodynamiccopyright/
+ * @bug 8050993
+ * @summary Verify that the condition in the conditional lexpression gets a LineNumberTable entry
+ * @modules jdk.jdeps/com.sun.tools.classfile
+ * @compile -g T8050993.java
+ * @run main T8050993
+ */
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Objects;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import com.sun.tools.classfile.*;
+
+public class T8050993 {
+    public static void main(String[] args) throws IOException, ConstantPoolException {
+        ClassFile someTestIn = ClassFile.read(T8050993.class.getResourceAsStream("T8050993.class"));
+        Set<Integer> expectedLineNumbers = new HashSet<>(Arrays.asList(48, 49, 46, 47));
+        for (Method m : someTestIn.methods) {
+            if ("method".equals(m.getName(someTestIn.constant_pool))) {
+                Code_attribute code_attribute = (Code_attribute) m.attributes.get(Attribute.Code);
+                for (Attribute at : code_attribute.attributes) {
+                    if (Attribute.LineNumberTable.equals(at.getName(someTestIn.constant_pool))) {
+                        LineNumberTable_attribute att = (LineNumberTable_attribute) at;
+                        Set<Integer> actualLinesNumbers = Arrays.stream(att.line_number_table)
+                                                                .map(e -> e.line_number)
+                                                                .collect(Collectors.toSet());
+                        if (!Objects.equals(expectedLineNumbers, actualLinesNumbers)) {
+                            throw new AssertionError("Expected LineNumber entries not found;" +
+                                                     "actual=" + actualLinesNumbers + ";" +
+                                                     "expected=" + expectedLineNumbers);
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+    public static int field;
+
+    public static String method() {
+        String s =
+                field % 2 == 0 ?
+                "true" + field :
+                "false" + field;
+        return s;
+    }
+
+}