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 extends Impl implements Iface { 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 callMain(m); 22 } 23 callMain(Main m)24 public static void callMain(Main m) { 25 System.out.println("Test method with concrete implementation"); 26 m.test_correct(); 27 System.out.println("Test method with concrete implementation via JNI call"); 28 long iface_id = GetMethodId(false, Iface.class, "test_correct", "()V"); 29 long main_id = GetMethodId(false, Main.class, "test_correct", "()V"); 30 long impl_id = GetMethodId(false, Impl.class, "test_correct", "()V"); 31 if (iface_id != main_id) { 32 System.out.println("Abstract interface and Main have different method ids"); 33 } else { 34 System.out.println("Unexpected: Abstract interface and Main have same method ids"); 35 } 36 CallNonvirtual(m, Main.class, main_id); 37 38 System.out.println("Test method with no concrete implementation"); 39 try { 40 m.test_throws(); 41 } catch (AbstractMethodError e) { 42 System.out.println("Expected AME Thrown on Main"); 43 } 44 long iface_throws_id = GetMethodId(false, Iface.class, "test_throws", "()V"); 45 long main_throws_id = GetMethodId(false, Main.class, "test_throws", "()V"); 46 try { 47 long id = GetMethodId(false, Impl.class, "test_throws", "()V"); 48 } catch (NoSuchMethodError e) { 49 System.out.println("Expected NoSuchMethodError on Main"); 50 } 51 if (iface_throws_id == main_throws_id) { 52 System.out.println("Abstract interface and Main have same method ids"); 53 } else { 54 System.out.println("Unexpected: Abstract interface and Main have different method ids"); 55 } 56 57 try { 58 CallNonvirtual(m, Main.class, main_throws_id); 59 } catch (AbstractMethodError e) { 60 System.out.println("Expected AME Thrown on Main via JNI call"); 61 } 62 return; 63 } 64 GetMethodId(boolean is_static, Class k, String name, String sig)65 private static native long GetMethodId(boolean is_static, Class k, String name, String sig); CallNonvirtual(Object obj, Class k, long methodid)66 private static native long CallNonvirtual(Object obj, Class k, long methodid); 67 } 68