1 /*
2  * Copyright (C) 2019 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 {
18   public static final boolean PRINT = false;
19 
20   public static class PtrCls {
doNothingPtr()21     public static void doNothingPtr() {}
22   }
23 
24   public static class IdxCls {
doNothingIdx()25     public static void doNothingIdx() {}
26   }
27 
DbgPrint(String str)28   public static void DbgPrint(String str) {
29     if (PRINT) {
30       System.out.println(str);
31     }
32   }
33 
GetId(Class<?> c, String name)34   public static long GetId(Class<?> c, String name) {
35     return GetMethodId(true, c, name, "()V");
36   }
37 
main(String[] args)38   public static void main(String[] args) {
39     System.loadLibrary(args[0]);
40     System.out.println("JNI Type is: " + GetJniType());
41     long expect_ptr_id = GetId(PtrCls.class, "doNothingPtr");
42     DbgPrint(String.format("expected_ptr_id is 0x%x", expect_ptr_id));
43     if (expect_ptr_id % 4 != 0) {
44       throw new Error("ID " + expect_ptr_id + " is not aligned!");
45     } else {
46       System.out.println("pointer ID looks like a pointer!");
47     }
48     SetToPointerIds();
49     System.out.println("JNI Type is: " + GetJniType());
50     long expect_ptr_id2 = GetId(IdxCls.class, "doNothingIdx");
51     DbgPrint(String.format("expected_ptr_id2 is 0x%x", expect_ptr_id2));
52     if (expect_ptr_id2 % 4 != 0) {
53       throw new Error("ID " + expect_ptr_id + " is not aligned!");
54     } else {
55       System.out.println("pointer2 ID looks like a pointer!");
56     }
57     long again_ptr_id = GetId(PtrCls.class, "doNothingPtr");
58     if (expect_ptr_id != again_ptr_id) {
59       throw new Error(
60           "Got different id values for same method. " + expect_ptr_id + " vs " + again_ptr_id);
61     } else {
62       System.out.println("pointer ID remains a pointer!");
63     }
64   }
65 
GetJniType()66   private static native String GetJniType();
SetToPointerIds()67   private static native void SetToPointerIds();
GetMethodId(boolean is_static, Class k, String name, String sig)68   private static native long GetMethodId(boolean is_static, Class k, String name, String sig);
69 }
70