1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/T5053846/MethodRefDupInConstantPoolTest.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,110 @@ 1.4 +/* 1.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 5053846 8011432 1.30 + * @summary javac: MethodRef entries are duplicated in the constant pool 1.31 + * @summary javac, compiler regression iterable + captured type 1.32 + */ 1.33 + 1.34 +import java.io.PrintWriter; 1.35 +import java.io.StringWriter; 1.36 +import java.nio.file.Paths; 1.37 +import java.util.*; 1.38 + 1.39 +public class MethodRefDupInConstantPoolTest { 1.40 + 1.41 + private static final String methodToLookFor = 1.42 + "java/util/Vector.iterator:()Ljava/util/Iterator;"; 1.43 + 1.44 + public static void main(String[] args) { 1.45 + new MethodRefDupInConstantPoolTest().run(); 1.46 + } 1.47 + 1.48 + void run() { 1.49 + check("-v", Paths.get(System.getProperty("test.classes"), 1.50 + this.getClass().getSimpleName() + "$TestHelper1.class").toString()); 1.51 + check("-v", Paths.get(System.getProperty("test.classes"), 1.52 + this.getClass().getSimpleName() + "$TestHelper2.class").toString()); 1.53 + check("-v", Paths.get(System.getProperty("test.classes"), 1.54 + this.getClass().getSimpleName() + "$TestHelper3.class").toString()); 1.55 + check("-v", Paths.get(System.getProperty("test.classes"), 1.56 + this.getClass().getSimpleName() + "$TestHelper4.class").toString()); 1.57 + } 1.58 + 1.59 + void check(String... params) { 1.60 + StringWriter s; 1.61 + String out; 1.62 + try (PrintWriter pw = new PrintWriter(s = new StringWriter())) { 1.63 + com.sun.tools.javap.Main.run(params, pw); 1.64 + out = s.toString(); 1.65 + } 1.66 + String constantPool = getConstantPool(out); 1.67 + if (constantPool.indexOf(methodToLookFor) != 1.68 + constantPool.lastIndexOf(methodToLookFor)) { 1.69 + throw new AssertionError("There is more than one entry for the method seek " + 1.70 + methodToLookFor); 1.71 + } 1.72 + } 1.73 + 1.74 + String getConstantPool(String out) { 1.75 + int start = out.indexOf("Constant pool:"); 1.76 + int end = out.indexOf("{"); 1.77 + return out.substring(start, end); 1.78 + } 1.79 + 1.80 + class TestHelper1 { 1.81 + void m() { 1.82 + Vector v = new Vector(); 1.83 + Iterator iter = v.iterator(); 1.84 + while (iter.hasNext()) { 1.85 + Object o = iter.next(); 1.86 + Object o2 = o; 1.87 + } 1.88 + for (Object o: v) { 1.89 + Object o2 = o; 1.90 + } 1.91 + } 1.92 + } 1.93 + 1.94 + class TestHelper2<X extends Number & Iterable<String>> { 1.95 + void test(X x) { 1.96 + for (String s : x) { } 1.97 + } 1.98 + } 1.99 + 1.100 + interface Data extends Iterable<String> {} 1.101 + 1.102 + class TestHelper3<X extends Number & Iterable<? extends Data>> { 1.103 + void test(X x) { 1.104 + for (Data s : x) { } 1.105 + } 1.106 + } 1.107 + 1.108 + class TestHelper4 { 1.109 + void test(Iterable<? extends Data> t) { 1.110 + for(Object a: t.iterator().next()); 1.111 + } 1.112 + } 1.113 +}