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