1/*
2 * Copyright (c) 2013, 2016, 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 */
23
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 * @modules jdk.compiler
30 *          jdk.jdeps/com.sun.tools.javap
31 */
32
33import java.io.PrintWriter;
34import java.io.StringWriter;
35import java.nio.file.Paths;
36import java.util.*;
37
38public class MethodRefDupInConstantPoolTest {
39
40    private static final String methodToLookFor =
41            "java/util/Vector.iterator:()Ljava/util/Iterator;";
42
43    public static void main(String[] args) {
44        new MethodRefDupInConstantPoolTest().run();
45    }
46
47    void run() {
48        check("-v", Paths.get(System.getProperty("test.classes"),
49                this.getClass().getSimpleName() + "$TestHelper1.class").toString());
50        check("-v", Paths.get(System.getProperty("test.classes"),
51                this.getClass().getSimpleName() + "$TestHelper2.class").toString());
52        check("-v", Paths.get(System.getProperty("test.classes"),
53                this.getClass().getSimpleName() + "$TestHelper3.class").toString());
54        check("-v", Paths.get(System.getProperty("test.classes"),
55                this.getClass().getSimpleName() + "$TestHelper4.class").toString());
56    }
57
58    void check(String... params) {
59        StringWriter s;
60        String out;
61        try (PrintWriter pw = new PrintWriter(s = new StringWriter())) {
62            com.sun.tools.javap.Main.run(params, pw);
63            out = s.toString();
64        }
65        String constantPool = getConstantPool(out);
66        if (constantPool.indexOf(methodToLookFor) !=
67                constantPool.lastIndexOf(methodToLookFor)) {
68            throw new AssertionError("There is more than one entry for the method seek "  +
69                    methodToLookFor);
70        }
71    }
72
73    String getConstantPool(String out) {
74        int start = out.indexOf("Constant pool:");
75        int end = out.indexOf("{");
76        return out.substring(start, end);
77    }
78
79    class TestHelper1 {
80        void m() {
81            Vector v = new Vector();
82            Iterator iter = v.iterator();
83            while (iter.hasNext()) {
84                Object o = iter.next();
85                Object o2 = o;
86            }
87            for (Object o: v) {
88                Object o2 = o;
89            }
90        }
91    }
92
93    class TestHelper2<X extends Number & Iterable<String>> {
94        void test(X x) {
95            for (String s : x) { }
96        }
97    }
98
99    interface Data extends Iterable<String> {}
100
101    class TestHelper3<X extends Number & Iterable<? extends Data>> {
102        void test(X x) {
103            for (Data s : x) { }
104        }
105    }
106
107    class TestHelper4 {
108         void test(Iterable<? extends Data> t) {
109             for(Object a: t.iterator().next());
110         }
111    }
112}
113