xref: /aosp_15_r20/art/test/139-register-natives/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 
17*795d594fSAndroid Build Coastguard Worker public class Main {
main(String[] args)18*795d594fSAndroid Build Coastguard Worker   public static void main(String[] args) {
19*795d594fSAndroid Build Coastguard Worker     System.loadLibrary(args[0]);
20*795d594fSAndroid Build Coastguard Worker     testRegistration1();
21*795d594fSAndroid Build Coastguard Worker     testRegistration2();
22*795d594fSAndroid Build Coastguard Worker     testRegistration3();
23*795d594fSAndroid Build Coastguard Worker   }
24*795d594fSAndroid Build Coastguard Worker 
25*795d594fSAndroid Build Coastguard Worker   // Test that a subclass' method is registered instead of a superclass' method.
testRegistration1()26*795d594fSAndroid Build Coastguard Worker   private static void testRegistration1() {
27*795d594fSAndroid Build Coastguard Worker     registerNatives(TestSub.class);
28*795d594fSAndroid Build Coastguard Worker 
29*795d594fSAndroid Build Coastguard Worker     expectNotThrows(new TestSub());
30*795d594fSAndroid Build Coastguard Worker     expectThrows(new TestSuper());
31*795d594fSAndroid Build Coastguard Worker   }
32*795d594fSAndroid Build Coastguard Worker 
33*795d594fSAndroid Build Coastguard Worker   // Test that a superclass' method is registered if the subclass doesn't have a matching method.
testRegistration2()34*795d594fSAndroid Build Coastguard Worker   private static void testRegistration2() {
35*795d594fSAndroid Build Coastguard Worker     registerNatives(TestSub2.class);
36*795d594fSAndroid Build Coastguard Worker 
37*795d594fSAndroid Build Coastguard Worker     expectNotThrows(new TestSub2());
38*795d594fSAndroid Build Coastguard Worker     expectNotThrows(new TestSuper2());
39*795d594fSAndroid Build Coastguard Worker   }
40*795d594fSAndroid Build Coastguard Worker 
41*795d594fSAndroid Build Coastguard Worker   // Test that registration fails if the subclass has a matching non-native method.
testRegistration3()42*795d594fSAndroid Build Coastguard Worker   private static void testRegistration3() {
43*795d594fSAndroid Build Coastguard Worker     try {
44*795d594fSAndroid Build Coastguard Worker       registerNatives(TestSub3.class);
45*795d594fSAndroid Build Coastguard Worker       System.out.println("Expected exception for registerNatives(TestSub3.class)");
46*795d594fSAndroid Build Coastguard Worker     } catch (NoSuchMethodError ignored) {
47*795d594fSAndroid Build Coastguard Worker     }
48*795d594fSAndroid Build Coastguard Worker   }
49*795d594fSAndroid Build Coastguard Worker 
registerNatives(Class<?> c)50*795d594fSAndroid Build Coastguard Worker   private native static int registerNatives(Class<?> c);
51*795d594fSAndroid Build Coastguard Worker 
expectThrows(Base b)52*795d594fSAndroid Build Coastguard Worker   private static void expectThrows(Base b) {
53*795d594fSAndroid Build Coastguard Worker     try {
54*795d594fSAndroid Build Coastguard Worker       b.callMyFoo();
55*795d594fSAndroid Build Coastguard Worker       System.out.println("Expected exception for " + b.getClass().getName());
56*795d594fSAndroid Build Coastguard Worker     } catch (Throwable ignored) {
57*795d594fSAndroid Build Coastguard Worker     }
58*795d594fSAndroid Build Coastguard Worker   }
59*795d594fSAndroid Build Coastguard Worker 
expectNotThrows(Base b)60*795d594fSAndroid Build Coastguard Worker   private static void expectNotThrows(Base b) {
61*795d594fSAndroid Build Coastguard Worker     try {
62*795d594fSAndroid Build Coastguard Worker       b.callMyFoo();
63*795d594fSAndroid Build Coastguard Worker     } catch (Throwable t) {
64*795d594fSAndroid Build Coastguard Worker       System.out.println("Did not expect an exception for " + b.getClass().getName());
65*795d594fSAndroid Build Coastguard Worker       t.printStackTrace(System.out);
66*795d594fSAndroid Build Coastguard Worker     }
67*795d594fSAndroid Build Coastguard Worker   }
68*795d594fSAndroid Build Coastguard Worker }
69*795d594fSAndroid Build Coastguard Worker 
70*795d594fSAndroid Build Coastguard Worker abstract class Base {
callMyFoo()71*795d594fSAndroid Build Coastguard Worker   public abstract void callMyFoo();
72*795d594fSAndroid Build Coastguard Worker }
73*795d594fSAndroid Build Coastguard Worker 
74*795d594fSAndroid Build Coastguard Worker class TestSuper extends Base {
foo()75*795d594fSAndroid Build Coastguard Worker   private native void foo();
76*795d594fSAndroid Build Coastguard Worker 
77*795d594fSAndroid Build Coastguard Worker   @Override
callMyFoo()78*795d594fSAndroid Build Coastguard Worker   public void callMyFoo() {
79*795d594fSAndroid Build Coastguard Worker     foo();
80*795d594fSAndroid Build Coastguard Worker   }
81*795d594fSAndroid Build Coastguard Worker }
82*795d594fSAndroid Build Coastguard Worker 
83*795d594fSAndroid Build Coastguard Worker class TestSub extends TestSuper {
foo()84*795d594fSAndroid Build Coastguard Worker   public native void foo();
85*795d594fSAndroid Build Coastguard Worker 
86*795d594fSAndroid Build Coastguard Worker   @Override
callMyFoo()87*795d594fSAndroid Build Coastguard Worker   public void callMyFoo() {
88*795d594fSAndroid Build Coastguard Worker     foo();
89*795d594fSAndroid Build Coastguard Worker   }
90*795d594fSAndroid Build Coastguard Worker }
91*795d594fSAndroid Build Coastguard Worker 
92*795d594fSAndroid Build Coastguard Worker class TestSuper2 extends Base{
foo()93*795d594fSAndroid Build Coastguard Worker   public native void foo();
94*795d594fSAndroid Build Coastguard Worker 
95*795d594fSAndroid Build Coastguard Worker   @Override
callMyFoo()96*795d594fSAndroid Build Coastguard Worker   public void callMyFoo() {
97*795d594fSAndroid Build Coastguard Worker     foo();
98*795d594fSAndroid Build Coastguard Worker   }
99*795d594fSAndroid Build Coastguard Worker }
100*795d594fSAndroid Build Coastguard Worker 
101*795d594fSAndroid Build Coastguard Worker class TestSub2 extends TestSuper2 {
102*795d594fSAndroid Build Coastguard Worker }
103*795d594fSAndroid Build Coastguard Worker 
104*795d594fSAndroid Build Coastguard Worker class TestSuper3 extends Base {
foo()105*795d594fSAndroid Build Coastguard Worker   public native void foo();
106*795d594fSAndroid Build Coastguard Worker 
107*795d594fSAndroid Build Coastguard Worker   @Override
callMyFoo()108*795d594fSAndroid Build Coastguard Worker   public void callMyFoo() {
109*795d594fSAndroid Build Coastguard Worker     foo();
110*795d594fSAndroid Build Coastguard Worker   }
111*795d594fSAndroid Build Coastguard Worker }
112*795d594fSAndroid Build Coastguard Worker 
113*795d594fSAndroid Build Coastguard Worker class TestSub3 extends TestSuper3 {
foo()114*795d594fSAndroid Build Coastguard Worker   public void foo() {
115*795d594fSAndroid Build Coastguard Worker     System.out.println("TestSub3.foo()");
116*795d594fSAndroid Build Coastguard Worker   }
117*795d594fSAndroid Build Coastguard Worker }
118