8201440: javac should create unique DynamicMethodSymbols at LambdaToMethod
authorvromero
Wed, 11 Apr 2018 14:52:32 -0400
changeset 49577 faf02d65df7d
parent 49576 535498e7602f
child 49578 0c3e252cea44
8201440: javac should create unique DynamicMethodSymbols at LambdaToMethod Reviewed-by: mcimadamore Contributed-by: bsrbnd@gmail.com
src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java
src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Pool.java
test/langtools/tools/javac/lambda/deduplication/Deduplication.java
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java	Wed Apr 11 11:11:13 2018 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java	Wed Apr 11 14:52:32 2018 -0400
@@ -67,6 +67,7 @@
 import static com.sun.tools.javac.code.Kinds.Kind.*;
 import static com.sun.tools.javac.code.TypeTag.*;
 import static com.sun.tools.javac.tree.JCTree.Tag.*;
+import static com.sun.tools.javac.jvm.Pool.DynamicMethod;
 
 import javax.lang.model.element.ElementKind;
 import javax.lang.model.type.TypeKind;
@@ -208,6 +209,8 @@
 
         private Map<DedupedLambda, DedupedLambda> dedupedLambdas;
 
+        private Map<DynamicMethod, DynamicMethodSymbol> dynMethSyms = new HashMap<>();
+
         /**
          * list of deserialization cases
          */
@@ -1200,9 +1203,10 @@
                                             (MethodSymbol)bsm,
                                             indyType,
                                             staticArgs.toArray());
-
             JCFieldAccess qualifier = make.Select(make.QualIdent(site.tsym), bsmName);
-            qualifier.sym = dynSym;
+            DynamicMethodSymbol existing = kInfo.dynMethSyms.putIfAbsent(
+                    new DynamicMethod(dynSym, types), dynSym);
+            qualifier.sym = existing != null ? existing : dynSym;
             qualifier.type = indyType.getReturnType();
 
             JCMethodInvocation proxyCall = make.Apply(List.nil(), qualifier, indyArgs);
--- a/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Pool.java	Wed Apr 11 11:11:13 2018 -0700
+++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/jvm/Pool.java	Wed Apr 11 14:52:32 2018 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2018, 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
@@ -180,10 +180,10 @@
         }
     }
 
-    static class DynamicMethod extends Method {
+    public static class DynamicMethod extends Method {
         public Object[] uniqueStaticArgs;
 
-        DynamicMethod(DynamicMethodSymbol m, Types types) {
+        public DynamicMethod(DynamicMethodSymbol m, Types types) {
             super(m, types);
             uniqueStaticArgs = getUniqueTypeArray(m.staticArgs, types);
         }
--- a/test/langtools/tools/javac/lambda/deduplication/Deduplication.java	Wed Apr 11 11:11:13 2018 -0700
+++ b/test/langtools/tools/javac/lambda/deduplication/Deduplication.java	Wed Apr 11 14:52:32 2018 -0400
@@ -32,6 +32,12 @@
     void group(Object... xs) {}
 
     void test() {
+
+        group(
+                (Runnable) () -> { ( (Runnable) () -> {} ).run(); },
+                (Runnable) () -> { ( (Runnable) () -> {} ).run(); }
+        );
+
         group((Function<String, Integer>) x -> x.hashCode());
         group((Function<Object, Integer>) x -> x.hashCode());