1 /*
2  * Copyright (c) 2020, 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.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 /**
27  * @test
28  * @bug 8216324
29  * @summary GetClassMethods is confused by the presence of default methods in super interfaces
30  * @library /test/lib
31  * @compile OverpassMethods.java
32  * @run main/othervm/native -agentlib:OverpassMethods OverpassMethods
33  * @run main/othervm/native -agentlib:OverpassMethods=maintain_original_method_order OverpassMethods
34   */
35 
36 import java.lang.reflect.Method;
37 import java.util.Arrays;
38 
39 public class OverpassMethods {
40 
41     static {
42         try {
43             System.loadLibrary("OverpassMethods");
44         } catch (UnsatisfiedLinkError ex) {
45             System.err.println("Could not load OverpassMethods library");
46             System.err.println("java.library.path:" + System.getProperty("java.library.path"));
47             throw ex;
48         }
49     }
50 
log(Object msg)51     static private void log(Object msg) {
52         System.out.println(String.valueOf(msg));
53     }
54 
getJVMTIDeclaredMethods(Class<?> klass)55     static private native Method[] getJVMTIDeclaredMethods(Class<?> klass);
56 
57     public interface Parent {
def()58         default String def() { return "Parent.def"; }
method0()59         String method0();
method1()60         String method1();
61     }
62 
63     public interface Child extends Parent {
method2()64         String method2();
65     }
66 
67     public static class Impl implements Child {
method0()68         public String method0() { return "Impl.method0"; }
method1()69         public String method1() { return "Impl.method1"; }
method2()70         public String method2() { return "Impl.method2"; }
71     }
72 
main(String[] args)73     public static void main(String[] args) {
74         new Impl(); // To get classes initialized
75 
76         Method[] reflectMethods = Child.class.getDeclaredMethods();
77         Method[] jvmtiMethods = getJVMTIDeclaredMethods(Child.class);
78 
79         if (jvmtiMethods == null) {
80             throw new RuntimeException("getJVMTIDeclaredMethods failed");
81         }
82 
83         log("Reflection getDeclaredMethods returned: " + Arrays.toString(reflectMethods));
84         log("JVMTI GetClassMethods returned: " + Arrays.toString(jvmtiMethods));
85 
86         if (reflectMethods.length != jvmtiMethods.length) {
87             throw new RuntimeException("OverpassMethods failed: Unexpected method count from JVMTI GetClassMethods!");
88         }
89         if (!reflectMethods[0].equals(jvmtiMethods[0])) {
90             throw new RuntimeException("OverpassMethods failed: Unexpected method from JVMTI GetClassMethods!");
91         }
92         log("Test passed: Got expected output from JVMTI GetClassMethods!");
93     }
94 }
95