TestNoBridgeOnDefaults.java revision 2942:08092deced3f
1/*
2 * Copyright (c) 2011, 2015, 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 7192246
27 * @summary  check that javac does not generate bridge methods for defaults
28 * @modules jdk.jdeps/com.sun.tools.classfile
29 */
30
31import com.sun.tools.classfile.ClassFile;
32import com.sun.tools.classfile.ConstantPool.*;
33import com.sun.tools.classfile.Method;
34
35import java.io.*;
36
37public class TestNoBridgeOnDefaults {
38
39    interface A<X> {
40        default <Y> A<X> m(X x, Y y) { return Impl.<X,Y>m1(this, x, y); }
41    }
42
43    static abstract class B<X> implements A<X> { }
44
45    interface C<X> extends A<X> {
46        default <Y> C<X> m(X x, Y y) { return Impl.<X,Y>m2(this, x, y); }
47    }
48
49    static abstract class D<X> extends B<X> implements C<X> { }
50
51    static class Impl {
52       static <X, Y> A<X> m1(A<X> rec, X x, Y y) { return null; }
53       static <X, Y> C<X> m2(C<X> rec, X x, Y y) { return null; }
54    }
55
56    static final String[] SUBTEST_NAMES = { B.class.getName() + ".class", D.class.getName() + ".class" };
57    static final String TEST_METHOD_NAME = "m";
58
59    public static void main(String... args) throws Exception {
60        new TestNoBridgeOnDefaults().run();
61    }
62
63    public void run() throws Exception {
64        String workDir = System.getProperty("test.classes");
65        for (int i = 0 ; i < SUBTEST_NAMES.length ; i ++) {
66            File compiledTest = new File(workDir, SUBTEST_NAMES[i]);
67            checkNoBridgeOnDefaults(compiledTest);
68        }
69    }
70
71    void checkNoBridgeOnDefaults(File f) {
72        System.err.println("check: " + f);
73        try {
74            ClassFile cf = ClassFile.read(f);
75            for (Method m : cf.methods) {
76                String mname = m.getName(cf.constant_pool);
77                if (mname.equals(TEST_METHOD_NAME)) {
78                    throw new Error("unexpected bridge method found " + m);
79                }
80            }
81        } catch (Exception e) {
82            e.printStackTrace();
83            throw new Error("error reading " + f +": " + e);
84        }
85    }
86}
87