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