xref: /aosp_15_r20/art/test/2262-default-conflict-methods/src/Main.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 class Main implements Iface, Iface2 {
main(String[] args)17     public static void main(String[] args) {
18         System.loadLibrary(args[0]);
19         System.out.println("Create Main instance");
20         Main m = new Main();
21         System.out.println("Calling functions on concrete Main");
22         callMain(m);
23     }
24 
callMain(Main m)25     public static void callMain(Main m) {
26         System.out.println("Calling non-conflicting function on Main");
27         m.test();
28         long main_id = GetMethodId(false, Main.class, "test", "()V");
29         long iface_id = GetMethodId(false, Iface.class, "test", "()V");
30         try {
31             long iface2_id = GetMethodId(false, Main.class, "test", "()V");
32             System.out.println("Unexpected normal exit from GetMethodId");
33         } catch (NoSuchMethodError e) {
34             System.out.println("Expected NoSuchMethodError thrown on Iface2");
35         }
36         if (main_id != iface_id) {
37             throw new Error("Default methods have different method ids");
38         }
39         CallNonvirtual(m, Main.class, main_id);
40 
41         System.out.println("Calling conflicting function on Main");
42         try {
43             m.test_throws();
44         } catch (IncompatibleClassChangeError e) {
45             System.out.println("Expected ICCE on main");
46         }
47 
48         long main_throws_id = GetMethodId(false, Main.class, "test_throws", "()V");
49         long iface_throws_id = GetMethodId(false, Iface.class, "test_throws", "()V");
50         long iface2_throws_id = GetMethodId(false, Iface2.class, "test_throws", "()V");
51         if (main_throws_id == iface_throws_id || main_throws_id == iface2_throws_id) {
52             System.out.println(
53                     "Unexpected: method id of default conflicting matches one of the interface methods");
54         }
55 
56         try {
57             CallNonvirtual(m, Main.class, main_throws_id);
58         } catch (IncompatibleClassChangeError e) {
59             System.out.println("Expected ICCE on main");
60         }
61         return;
62     }
63 
GetMethodId(boolean is_static, Class k, String name, String sig)64     private static native long GetMethodId(boolean is_static, Class k, String name, String sig);
CallNonvirtual(Object obj, Class k, long methodid)65     private static native long CallNonvirtual(Object obj, Class k, long methodid);
66 }
67