test/tools/javac/T5053846/MethodRefDupInConstantPoolTest.java

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 1687
a4be2c2fe0a1
parent 0
959103a6100f
permissions
-rw-r--r--

merge

     1 /*
     2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  */
    24 /*
    25  * @test
    26  * @bug 5053846 8011432
    27  * @summary javac: MethodRef entries are duplicated in the constant pool
    28  * @summary javac, compiler regression iterable + captured type
    29  */
    31 import java.io.PrintWriter;
    32 import java.io.StringWriter;
    33 import java.nio.file.Paths;
    34 import java.util.*;
    36 public class MethodRefDupInConstantPoolTest {
    38     private static final String methodToLookFor =
    39             "java/util/Vector.iterator:()Ljava/util/Iterator;";
    41     public static void main(String[] args) {
    42         new MethodRefDupInConstantPoolTest().run();
    43     }
    45     void run() {
    46         check("-v", Paths.get(System.getProperty("test.classes"),
    47                 this.getClass().getSimpleName() + "$TestHelper1.class").toString());
    48         check("-v", Paths.get(System.getProperty("test.classes"),
    49                 this.getClass().getSimpleName() + "$TestHelper2.class").toString());
    50         check("-v", Paths.get(System.getProperty("test.classes"),
    51                 this.getClass().getSimpleName() + "$TestHelper3.class").toString());
    52         check("-v", Paths.get(System.getProperty("test.classes"),
    53                 this.getClass().getSimpleName() + "$TestHelper4.class").toString());
    54     }
    56     void check(String... params) {
    57         StringWriter s;
    58         String out;
    59         try (PrintWriter pw = new PrintWriter(s = new StringWriter())) {
    60             com.sun.tools.javap.Main.run(params, pw);
    61             out = s.toString();
    62         }
    63         String constantPool = getConstantPool(out);
    64         if (constantPool.indexOf(methodToLookFor) !=
    65                 constantPool.lastIndexOf(methodToLookFor)) {
    66             throw new AssertionError("There is more than one entry for the method seek "  +
    67                     methodToLookFor);
    68         }
    69     }
    71     String getConstantPool(String out) {
    72         int start = out.indexOf("Constant pool:");
    73         int end = out.indexOf("{");
    74         return out.substring(start, end);
    75     }
    77     class TestHelper1 {
    78         void m() {
    79             Vector v = new Vector();
    80             Iterator iter = v.iterator();
    81             while (iter.hasNext()) {
    82                 Object o = iter.next();
    83                 Object o2 = o;
    84             }
    85             for (Object o: v) {
    86                 Object o2 = o;
    87             }
    88         }
    89     }
    91     class TestHelper2<X extends Number & Iterable<String>> {
    92         void test(X x) {
    93             for (String s : x) { }
    94         }
    95     }
    97     interface Data extends Iterable<String> {}
    99     class TestHelper3<X extends Number & Iterable<? extends Data>> {
   100         void test(X x) {
   101             for (Data s : x) { }
   102         }
   103     }
   105     class TestHelper4 {
   106          void test(Iterable<? extends Data> t) {
   107              for(Object a: t.iterator().next());
   108          }
   109     }
   110 }

mercurial