8134329: TeeOpTest.java fails across platforms after fix for JDK-8129547
authormcimadamore
Tue, 25 Aug 2015 15:10:25 +0100
changeset 32336 9e9acf7f26a4
parent 32335 7df616378cf3
child 32337 c9d3ab9f601c
8134329: TeeOpTest.java fails across platforms after fix for JDK-8129547 Summary: Wrong indexes associated to CONSTANT_InvokeDynamic_info entries. Reviewed-by: sundar Contributed-by: aleksey.shipilev@oracle.com
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassWriter.java
langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Pool.java
langtools/test/tools/javac/lambda/8134329/T8134329.java
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassWriter.java	Tue Aug 25 15:14:41 2015 +0200
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/ClassWriter.java	Tue Aug 25 15:10:25 2015 +0100
@@ -138,7 +138,7 @@
     /** The bootstrap methods to be written in the corresponding class attribute
      *  (one for each invokedynamic)
      */
-    Map<DynamicMethod.BootstrapMethodsKey, MethodHandle> bootstrapMethods;
+    Map<DynamicMethod.BootstrapMethodsKey, DynamicMethod.BootstrapMethodsValue> bootstrapMethods;
 
     /** The log to use for verbose output.
      */
@@ -402,7 +402,15 @@
                     DynamicMethodSymbol dynSym = (DynamicMethodSymbol)m;
                     MethodHandle handle = new MethodHandle(dynSym.bsmKind, dynSym.bsm, types);
                     DynamicMethod.BootstrapMethodsKey key = new DynamicMethod.BootstrapMethodsKey(dynSym, types);
-                    bootstrapMethods.put(key, handle);
+
+                    // Figure out the index for existing BSM; create a new BSM if no key
+                    DynamicMethod.BootstrapMethodsValue val = bootstrapMethods.get(key);
+                    if (val == null) {
+                        int index = bootstrapMethods.size();
+                        val = new DynamicMethod.BootstrapMethodsValue(handle, index);
+                        bootstrapMethods.put(key, val);
+                    }
+
                     //init cp entries
                     pool.put(names.BootstrapMethods);
                     pool.put(handle);
@@ -410,7 +418,7 @@
                         pool.put(staticArg);
                     }
                     poolbuf.appendByte(CONSTANT_InvokeDynamic);
-                    poolbuf.appendChar(bootstrapMethods.size() - 1);
+                    poolbuf.appendChar(val.index);
                     poolbuf.appendChar(pool.put(nameType(dynSym)));
                 }
             } else if (value instanceof VarSymbol) {
@@ -1024,10 +1032,10 @@
     void writeBootstrapMethods() {
         int alenIdx = writeAttr(names.BootstrapMethods);
         databuf.appendChar(bootstrapMethods.size());
-        for (Map.Entry<DynamicMethod.BootstrapMethodsKey, MethodHandle> entry : bootstrapMethods.entrySet()) {
+        for (Map.Entry<DynamicMethod.BootstrapMethodsKey, DynamicMethod.BootstrapMethodsValue> entry : bootstrapMethods.entrySet()) {
             DynamicMethod.BootstrapMethodsKey bsmKey = entry.getKey();
             //write BSM handle
-            databuf.appendChar(pool.get(entry.getValue()));
+            databuf.appendChar(pool.get(entry.getValue().mh));
             Object[] uniqueArgs = bsmKey.getUniqueArgs();
             //write static args length
             databuf.appendChar(uniqueArgs.length);
--- a/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Pool.java	Tue Aug 25 15:14:41 2015 +0200
+++ b/langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Pool.java	Tue Aug 25 15:10:25 2015 +0100
@@ -251,6 +251,16 @@
                 return uniqueStaticArgs;
             }
         }
+
+        static class BootstrapMethodsValue {
+            final MethodHandle mh;
+            final int index;
+
+            public BootstrapMethodsValue(MethodHandle mh, int index) {
+                this.mh = mh;
+                this.index = index;
+            }
+        }
     }
 
     static class Variable extends DelegatedSymbol<VarSymbol> {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/lambda/8134329/T8134329.java	Tue Aug 25 15:10:25 2015 +0100
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2015, 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.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * 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 8134329
+ * @summary TeeOpTest.java fails across platforms after fix for JDK-8129547
+ */
+import java.util.function.Supplier;
+
+public class T8134329 {
+
+    static void assertEquals(Object o1, Object o2) {
+        if (!o1.equals(o2)) {
+            throw new AssertionError(String.format("Expected %s - found %s", o2, o1));
+        }
+    }
+
+    public static void main(String[] args) {
+        Supplier<String> s1 = new T8134329() { }::m;
+        assertEquals(s1.get(), "m");
+        Supplier<String> s2 = new T8134329() { }::g;
+        assertEquals(s2.get(), "g");
+        Supplier<String> s3 = new T8134329() { }::m;
+        assertEquals(s3.get(), "m");
+    }
+
+    String m() { return "m"; }
+    String g() { return "g"; }
+}