7005371: Multicatch: assertion error while generating LocalVariableTypeTable attribute
authormcimadamore
Thu, 09 Dec 2010 15:50:57 +0000
changeset 7629 4662e86bff2e
parent 7628 e7baeb97d164
child 7630 850c97ee59f2
7005371: Multicatch: assertion error while generating LocalVariableTypeTable attribute Summary: compiler crashes with assertion error if '-g' option is passed and source contains multicatch Reviewed-by: jjg
langtools/src/share/classes/com/sun/tools/javac/jvm/ClassWriter.java
langtools/test/tools/javac/multicatch/7005371/SubTest.java
langtools/test/tools/javac/multicatch/7005371/T7005371.java
--- a/langtools/src/share/classes/com/sun/tools/javac/jvm/ClassWriter.java	Thu Dec 09 15:50:34 2010 +0000
+++ b/langtools/src/share/classes/com/sun/tools/javac/jvm/ClassWriter.java	Thu Dec 09 15:50:57 2010 +0000
@@ -1168,7 +1168,7 @@
                 VarSymbol sym = var.sym;
                 databuf.appendChar(pool.put(sym.name));
                 Type vartype = sym.erasure(types);
-                if (!types.isSameType(sym.type, vartype))
+                if (needsLocalVariableTypeEntry(sym.type))
                     nGenericVars++;
                 databuf.appendChar(pool.put(typeSig(vartype)));
                 databuf.appendChar(var.reg);
@@ -1185,7 +1185,7 @@
             for (int i=0; i<code.varBufferSize; i++) {
                 Code.LocalVar var = code.varBuffer[i];
                 VarSymbol sym = var.sym;
-                if (types.isSameType(sym.type, sym.erasure(types)))
+                if (!needsLocalVariableTypeEntry(sym.type))
                     continue;
                 count++;
                 // write variable info
@@ -1209,6 +1209,14 @@
         }
         endAttrs(acountIdx, acount);
     }
+    //where
+    private boolean needsLocalVariableTypeEntry(Type t) {
+        //a local variable needs a type-entry if its type T is generic
+        //(i.e. |T| != T) and if it's not an intersection type (not supported
+        //in signature attribute grammar)
+        return (!types.isSameType(t, types.erasure(t)) &&
+                !t.isCompound());
+    }
 
     void writeStackMap(Code code) {
         int nframes = code.stackMapBufferSize;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/multicatch/7005371/SubTest.java	Thu Dec 09 15:50:57 2010 +0000
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2010, 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.
+ */
+
+import java.util.*;
+
+class SubTest {
+
+    interface Foo {}
+    static class X1 extends Exception implements Foo {}
+    static class X2 extends Exception implements Foo {}
+
+    void test(boolean cond, List<String> ls) {
+        try {
+            if (cond)
+                throw new X1();
+            else
+                throw new X2();
+        }
+        catch (X1 | X2 ex) {}
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/multicatch/7005371/T7005371.java	Thu Dec 09 15:50:57 2010 +0000
@@ -0,0 +1,100 @@
+/*
+ * Copyright (c) 2010, 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 7005371
+ * @summary  Multicatch: assertion error while generating LocalVariableTypeTable attribute
+ * @compile -g SubTest.java
+ * @run main T7005371
+ */
+
+import com.sun.tools.classfile.Attribute;
+import com.sun.tools.classfile.ClassFile;
+import com.sun.tools.classfile.Code_attribute;
+import com.sun.tools.classfile.LocalVariableTypeTable_attribute;
+import com.sun.tools.classfile.Method;
+
+import java.io.*;
+
+public class T7005371 {
+
+    static final String SUBTEST_NAME = SubTest.class.getName() + ".class";
+    static final String TEST_METHOD_NAME = "test";
+    static final int LVT_LENGTH = 1;
+    static final String LVT_SIG_TYPE = "Ljava/util/List<Ljava/lang/String;>;";
+
+
+    public static void main(String... args) throws Exception {
+        new T7005371().run();
+    }
+
+    public void run() throws Exception {
+        String workDir = System.getProperty("test.classes");
+        System.out.println(workDir);
+        File compiledTest = new File(workDir, SUBTEST_NAME);
+        verifyLocalVariableTypeTableAttr(compiledTest);
+    }
+
+    void verifyLocalVariableTypeTableAttr(File f) {
+        System.err.println("verify: " + f);
+        try {
+            ClassFile cf = ClassFile.read(f);
+            Method testMethod = null;
+            for (Method m : cf.methods) {
+                if (m.getName(cf.constant_pool).equals(TEST_METHOD_NAME)) {
+                    testMethod = m;
+                    break;
+                }
+            }
+            if (testMethod == null) {
+                throw new Error("Missing method: " + TEST_METHOD_NAME);
+            }
+            Code_attribute code = (Code_attribute)testMethod.attributes.get(Attribute.Code);
+            if (code == null) {
+                throw new Error("Missing Code attribute for method: " + TEST_METHOD_NAME);
+            }
+            LocalVariableTypeTable_attribute lvt_table =
+                    (LocalVariableTypeTable_attribute)code.attributes.get(Attribute.LocalVariableTypeTable);
+            if (lvt_table == null) {
+                throw new Error("Missing LocalVariableTypeTable attribute for method: " + TEST_METHOD_NAME);
+            }
+            if (lvt_table.local_variable_table_length != LVT_LENGTH) {
+                throw new Error("LocalVariableTypeTable has wrong size" +
+                        "\nfound: " + lvt_table.local_variable_table_length +
+                        "\nrequired: " + LVT_LENGTH);
+            }
+            String sig =
+                    cf.constant_pool.getUTF8Value(lvt_table.local_variable_table[0].signature_index);
+
+            if (sig == null || !sig.equals(LVT_SIG_TYPE)) {
+                throw new Error("LocalVariableTypeTable has wrong signature" +
+                        "\nfound: " + sig +
+                        "\nrequired: " + LVT_SIG_TYPE);
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new Error("error reading " + f +": " + e);
+        }
+    }
+}