Fri, 15 Mar 2013 09:02:26 +0000
5053846: javac: MethodRef entries are duplicated in the constant pool
Reviewed-by: mcimadamore
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
27 * @summary javac: MethodRef entries are duplicated in the constant pool
28 */
30 import java.io.PrintWriter;
31 import java.io.StringWriter;
32 import java.nio.file.Paths;
33 import java.util.*;
35 public class MethodRefDupInConstantPoolTest {
37 private static final String methodToLookFor =
38 "java/util/Vector.iterator:()Ljava/util/Iterator;";
40 public static void main(String[] args) {
41 new MethodRefDupInConstantPoolTest().run();
42 }
44 void run() {
45 check("-v", Paths.get(System.getProperty("test.classes"),
46 "TestHelper1.class").toString());
47 check("-v", Paths.get(System.getProperty("test.classes"),
48 "TestHelper2.class").toString());
49 }
51 void check(String... params) {
52 StringWriter s;
53 String out;
54 try (PrintWriter pw = new PrintWriter(s = new StringWriter())) {
55 com.sun.tools.javap.Main.run(params, pw);
56 out = s.toString();
57 }
58 String constantPool = getConstantPool(out);
59 if (constantPool.indexOf(methodToLookFor) !=
60 constantPool.lastIndexOf(methodToLookFor)) {
61 throw new AssertionError("There is more than one entry for the method seek " +
62 methodToLookFor);
63 }
64 }
66 String getConstantPool(String out) {
67 int start = out.indexOf("Constant pool:");
68 int end = out.indexOf("{");
69 return out.substring(start, end);
70 }
71 }
73 class TestHelper1 {
74 void m() {
75 Vector v = new Vector();
76 Iterator iter = v.iterator();
77 while (iter.hasNext()) {
78 Object o = iter.next();
79 Object o2 = o;
80 }
81 for (Object o: v) {
82 Object o2 = o;
83 }
84 }
85 }
87 class TestHelper2<X extends Number & Iterable<String>> {
88 void test(X x) {
89 for (String s : x) { }
90 }
91 }