xref: /aosp_15_r20/art/test/1972-jni-id-swap-indices/src/Main.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2019 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 import java.util.function.Consumer;
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker public class Main {
20*795d594fSAndroid Build Coastguard Worker   public static final boolean PRINT = false;
21*795d594fSAndroid Build Coastguard Worker 
22*795d594fSAndroid Build Coastguard Worker   public static class PtrCls {
doNothingPtr()23*795d594fSAndroid Build Coastguard Worker     public static void doNothingPtr() {}
24*795d594fSAndroid Build Coastguard Worker   }
25*795d594fSAndroid Build Coastguard Worker 
26*795d594fSAndroid Build Coastguard Worker   public static class IdxCls {
doNothingIdx()27*795d594fSAndroid Build Coastguard Worker     public static void doNothingIdx() {}
28*795d594fSAndroid Build Coastguard Worker   }
29*795d594fSAndroid Build Coastguard Worker 
DbgPrint(String str)30*795d594fSAndroid Build Coastguard Worker   public static void DbgPrint(String str) {
31*795d594fSAndroid Build Coastguard Worker     if (PRINT) {
32*795d594fSAndroid Build Coastguard Worker       System.out.println(str);
33*795d594fSAndroid Build Coastguard Worker     }
34*795d594fSAndroid Build Coastguard Worker   }
35*795d594fSAndroid Build Coastguard Worker 
GetId(Class<?> k, String name)36*795d594fSAndroid Build Coastguard Worker   public static long GetId(Class<?> k, String name) {
37*795d594fSAndroid Build Coastguard Worker     return GetMethodId(true, k, name, "()V");
38*795d594fSAndroid Build Coastguard Worker   }
39*795d594fSAndroid Build Coastguard Worker 
main(String[] args)40*795d594fSAndroid Build Coastguard Worker   public static void main(String[] args) {
41*795d594fSAndroid Build Coastguard Worker     System.loadLibrary(args[0]);
42*795d594fSAndroid Build Coastguard Worker     System.out.println("JNI Type is: " + GetJniType());
43*795d594fSAndroid Build Coastguard Worker     long expect_ptr_id = GetId(PtrCls.class, "doNothingPtr");
44*795d594fSAndroid Build Coastguard Worker     DbgPrint(String.format("expected_ptr_id is 0x%x", expect_ptr_id));
45*795d594fSAndroid Build Coastguard Worker     if (expect_ptr_id % 4 != 0) {
46*795d594fSAndroid Build Coastguard Worker       throw new Error("ID " + expect_ptr_id + " is not aligned!");
47*795d594fSAndroid Build Coastguard Worker     } else {
48*795d594fSAndroid Build Coastguard Worker       System.out.println("pointer ID looks like a pointer!");
49*795d594fSAndroid Build Coastguard Worker     }
50*795d594fSAndroid Build Coastguard Worker     SetToIndexIds();
51*795d594fSAndroid Build Coastguard Worker     System.out.println("JNI Type is: " + GetJniType());
52*795d594fSAndroid Build Coastguard Worker     long expect_idx_id = GetId(IdxCls.class, "doNothingIdx");
53*795d594fSAndroid Build Coastguard Worker     DbgPrint(String.format("expected_idx_id is 0x%x", expect_idx_id));
54*795d594fSAndroid Build Coastguard Worker     if (expect_idx_id % 2 != 1) {
55*795d594fSAndroid Build Coastguard Worker       throw new Error("ID " + expect_ptr_id + " is not odd!");
56*795d594fSAndroid Build Coastguard Worker     } else {
57*795d594fSAndroid Build Coastguard Worker       System.out.println("index ID looks like an index!");
58*795d594fSAndroid Build Coastguard Worker     }
59*795d594fSAndroid Build Coastguard Worker     long again_ptr_id = GetId(PtrCls.class, "doNothingPtr");
60*795d594fSAndroid Build Coastguard Worker     if (expect_ptr_id != again_ptr_id) {
61*795d594fSAndroid Build Coastguard Worker       throw new Error(
62*795d594fSAndroid Build Coastguard Worker           "Got different id values for same method. " + expect_ptr_id + " vs " + again_ptr_id);
63*795d594fSAndroid Build Coastguard Worker     } else {
64*795d594fSAndroid Build Coastguard Worker       System.out.println("pointer ID remains a pointer!");
65*795d594fSAndroid Build Coastguard Worker     }
66*795d594fSAndroid Build Coastguard Worker     long well_known_id = GetMethodId(false, Consumer.class, "accept", "(Ljava/lang/Object;)V");
67*795d594fSAndroid Build Coastguard Worker     DbgPrint(String.format("well_known_id is 0x%x", well_known_id));
68*795d594fSAndroid Build Coastguard Worker     if (well_known_id % 2 != 1) {
69*795d594fSAndroid Build Coastguard Worker       throw new Error("WKC ID " + well_known_id + " is not odd!");
70*795d594fSAndroid Build Coastguard Worker     } else {
71*795d594fSAndroid Build Coastguard Worker       System.out.println("index WKC ID looks like an index!");
72*795d594fSAndroid Build Coastguard Worker     }
73*795d594fSAndroid Build Coastguard Worker   }
74*795d594fSAndroid Build Coastguard Worker 
GetJniType()75*795d594fSAndroid Build Coastguard Worker   private static native String GetJniType();
SetToIndexIds()76*795d594fSAndroid Build Coastguard Worker   private static native void SetToIndexIds();
GetMethodId(boolean is_static, Class k, String name, String sig)77*795d594fSAndroid Build Coastguard Worker   private static native long GetMethodId(boolean is_static, Class k, String name, String sig);
78*795d594fSAndroid Build Coastguard Worker }
79