blob: 0221900113f4bee33a0293b5b84bec683a8cad9a [file] [log] [blame]
Brian Carlstromce888532013-10-10 00:32:58 -07001/*
2 * Copyright (C) 2013 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
Mathieu Chartier72156e22015-07-10 18:26:41 -070017import java.lang.reflect.InvocationHandler;
Jeff Hao201803f2013-11-20 18:11:39 -080018import java.lang.reflect.Method;
Mathieu Chartier72156e22015-07-10 18:26:41 -070019import java.lang.reflect.Proxy;
Jeff Hao201803f2013-11-20 18:11:39 -080020
Andreas Gampe1c83cbc2014-07-22 18:52:29 -070021public class Main {
Brian Carlstromce888532013-10-10 00:32:58 -070022 public static void main(String[] args) {
Mathieu Chartier031768a2015-08-27 10:25:02 -070023 System.loadLibrary(args[0]);
Brian Carlstromce888532013-10-10 00:32:58 -070024 testFindClassOnAttachedNativeThread();
Jeff Hao62509b62013-12-10 17:44:56 -080025 testFindFieldOnAttachedNativeThread();
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +010026 testReflectFieldGetFromAttachedNativeThreadNative();
Brian Carlstrom67fe2b42013-10-15 18:51:42 -070027 testCallStaticVoidMethodOnSubClass();
Jeff Hao201803f2013-11-20 18:11:39 -080028 testGetMirandaMethod();
Narayan Kamathef809d02013-12-19 17:52:47 +000029 testZeroLengthByteBuffers();
Andreas Gamped1104322014-05-01 14:38:56 -070030 testByteMethod();
31 testShortMethod();
32 testBooleanMethod();
33 testCharMethod();
Narayan Kamath1268b742014-07-11 19:15:11 +010034 testIsAssignableFromOnPrimitiveTypes();
Andreas Gampe718ac652014-08-11 18:51:53 -070035 testShallowGetCallingClassLoader();
Andreas Gampe0d334ce2014-08-13 23:05:38 -070036 testShallowGetStackClass2();
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -070037 testCallNonvirtual();
Jeff Hao848f70a2014-01-15 13:49:50 -080038 testNewStringObject();
Mathieu Chartierff6d8cf2015-06-02 13:40:12 -070039 testRemoveLocalObject();
Mathieu Chartier72156e22015-07-10 18:26:41 -070040 testProxyGetMethodID();
Hiroshi Yamauchi20a0be02016-02-19 15:44:06 -080041 testJniCriticalSectionAndGc();
Alex Light36121492016-02-22 13:43:29 -080042 testCallDefaultMethods();
Alex Lighte9d2ca22016-02-25 16:13:54 -080043 String lambda = "λ";
44 testInvokeLambdaMethod(() -> { System.out.println("hi-lambda: " + lambda); });
45 String def = "δ";
46 testInvokeLambdaDefaultMethod(() -> { System.out.println("hi-default " + def + lambda); });
Brian Carlstromce888532013-10-10 00:32:58 -070047 }
48
Alex Light36121492016-02-22 13:43:29 -080049 private static native void testCallDefaultMethods();
50
Brian Carlstromce888532013-10-10 00:32:58 -070051 private static native void testFindClassOnAttachedNativeThread();
Brian Carlstrom67fe2b42013-10-15 18:51:42 -070052
Jeff Hao62509b62013-12-10 17:44:56 -080053 private static boolean testFindFieldOnAttachedNativeThreadField;
54
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +010055 private static native void testReflectFieldGetFromAttachedNativeThreadNative();
56
57 public static boolean testReflectFieldGetFromAttachedNativeThreadField;
58
Jeff Hao62509b62013-12-10 17:44:56 -080059 private static void testFindFieldOnAttachedNativeThread() {
60 testFindFieldOnAttachedNativeThreadNative();
61 if (!testFindFieldOnAttachedNativeThreadField) {
62 throw new AssertionError();
63 }
64 }
65
66 private static native void testFindFieldOnAttachedNativeThreadNative();
67
Brian Carlstrom67fe2b42013-10-15 18:51:42 -070068 private static void testCallStaticVoidMethodOnSubClass() {
69 testCallStaticVoidMethodOnSubClassNative();
70 if (!testCallStaticVoidMethodOnSubClass_SuperClass.executed) {
71 throw new AssertionError();
72 }
73 }
74
75 private static native void testCallStaticVoidMethodOnSubClassNative();
76
77 private static class testCallStaticVoidMethodOnSubClass_SuperClass {
78 private static boolean executed = false;
79 private static void execute() {
80 executed = true;
81 }
82 }
83
84 private static class testCallStaticVoidMethodOnSubClass_SubClass
85 extends testCallStaticVoidMethodOnSubClass_SuperClass {
86 }
Jeff Hao201803f2013-11-20 18:11:39 -080087
88 private static native Method testGetMirandaMethodNative();
89
90 private static void testGetMirandaMethod() {
91 Method m = testGetMirandaMethodNative();
92 if (m.getDeclaringClass() != testGetMirandaMethod_MirandaInterface.class) {
93 throw new AssertionError();
94 }
95 }
96
Narayan Kamathef809d02013-12-19 17:52:47 +000097 private static native void testZeroLengthByteBuffers();
98
Jeff Hao201803f2013-11-20 18:11:39 -080099 private static abstract class testGetMirandaMethod_MirandaAbstract implements testGetMirandaMethod_MirandaInterface {
100 public boolean inAbstract() {
101 return true;
102 }
103 }
104
105 private static interface testGetMirandaMethod_MirandaInterface {
106 public boolean inInterface();
107 }
Andreas Gamped1104322014-05-01 14:38:56 -0700108
109 // Test sign-extension for values < 32b
110
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700111 static native byte byteMethod(byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7,
Andreas Gamped1104322014-05-01 14:38:56 -0700112 byte b8, byte b9, byte b10);
113
114 private static void testByteMethod() {
115 byte returns[] = { 0, 1, 2, 127, -1, -2, -128 };
116 for (int i = 0; i < returns.length; i++) {
117 byte result = byteMethod((byte)i, (byte)2, (byte)(-3), (byte)4, (byte)(-5), (byte)6,
118 (byte)(-7), (byte)8, (byte)(-9), (byte)10);
119 if (returns[i] != result) {
120 System.out.println("Run " + i + " with " + returns[i] + " vs " + result);
121 throw new AssertionError();
122 }
123 }
124 }
125
Mathieu Chartierff6d8cf2015-06-02 13:40:12 -0700126 private static native void removeLocalObject(Object o);
127
128 private static void testRemoveLocalObject() {
129 removeLocalObject(new Object());
130 }
Alex Light36121492016-02-22 13:43:29 -0800131
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700132 private static native short shortMethod(short s1, short s2, short s3, short s4, short s5, short s6, short s7,
Andreas Gamped1104322014-05-01 14:38:56 -0700133 short s8, short s9, short s10);
134
135 private static void testShortMethod() {
136 short returns[] = { 0, 1, 2, 127, 32767, -1, -2, -128, -32768 };
137 for (int i = 0; i < returns.length; i++) {
138 short result = shortMethod((short)i, (short)2, (short)(-3), (short)4, (short)(-5), (short)6,
139 (short)(-7), (short)8, (short)(-9), (short)10);
140 if (returns[i] != result) {
141 System.out.println("Run " + i + " with " + returns[i] + " vs " + result);
142 throw new AssertionError();
143 }
144 }
145 }
146
147 // Test zero-extension for values < 32b
148
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700149 private static native boolean booleanMethod(boolean b1, boolean b2, boolean b3, boolean b4, boolean b5, boolean b6, boolean b7,
Andreas Gamped1104322014-05-01 14:38:56 -0700150 boolean b8, boolean b9, boolean b10);
151
152 private static void testBooleanMethod() {
153 if (booleanMethod(false, true, false, true, false, true, false, true, false, true)) {
154 throw new AssertionError();
155 }
156
157 if (!booleanMethod(true, true, false, true, false, true, false, true, false, true)) {
158 throw new AssertionError();
159 }
160 }
161
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700162 private static native char charMethod(char c1, char c2, char c3, char c4, char c5, char c6, char c7,
Andreas Gamped1104322014-05-01 14:38:56 -0700163 char c8, char c9, char c10);
164
165 private static void testCharMethod() {
166 char returns[] = { (char)0, (char)1, (char)2, (char)127, (char)255, (char)256, (char)15000,
167 (char)34000 };
168 for (int i = 0; i < returns.length; i++) {
169 char result = charMethod((char)i, 'a', 'b', 'c', '0', '1', '2', (char)1234, (char)2345,
170 (char)3456);
171 if (returns[i] != result) {
172 System.out.println("Run " + i + " with " + (int)returns[i] + " vs " + (int)result);
173 throw new AssertionError();
174 }
175 }
176 }
Narayan Kamath1268b742014-07-11 19:15:11 +0100177
178 // http://b/16531674
179 private static void testIsAssignableFromOnPrimitiveTypes() {
180 if (!nativeIsAssignableFrom(int.class, Integer.TYPE)) {
181 System.out.println("IsAssignableFrom(int.class, Integer.TYPE) returned false, expected true");
182 throw new AssertionError();
183 }
184
185 if (!nativeIsAssignableFrom(Integer.TYPE, int.class)) {
186 System.out.println("IsAssignableFrom(Integer.TYPE, int.class) returned false, expected true");
187 throw new AssertionError();
188 }
189 }
190
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700191 private static native boolean nativeIsAssignableFrom(Class<?> from, Class<?> to);
Andreas Gampe718ac652014-08-11 18:51:53 -0700192
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700193 private static void testShallowGetCallingClassLoader() {
Andreas Gampe718ac652014-08-11 18:51:53 -0700194 nativeTestShallowGetCallingClassLoader();
195 }
196
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700197 private native static void nativeTestShallowGetCallingClassLoader();
Andreas Gampe0d334ce2014-08-13 23:05:38 -0700198
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700199 private static void testShallowGetStackClass2() {
Andreas Gampe0d334ce2014-08-13 23:05:38 -0700200 nativeTestShallowGetStackClass2();
201 }
202
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700203 private static native void nativeTestShallowGetStackClass2();
204
205 private static native void testCallNonvirtual();
Jeff Hao848f70a2014-01-15 13:49:50 -0800206
207 private static native void testNewStringObject();
Mathieu Chartier72156e22015-07-10 18:26:41 -0700208
209 private interface SimpleInterface {
210 void a();
211 }
212
213 private static class DummyInvocationHandler implements InvocationHandler {
214 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
215 return null;
216 }
217 }
218
219 private static void testProxyGetMethodID() {
220 InvocationHandler handler = new DummyInvocationHandler();
221 SimpleInterface proxy =
222 (SimpleInterface) Proxy.newProxyInstance(SimpleInterface.class.getClassLoader(),
Andreas Gampe166aaee2016-07-18 08:27:23 -0700223 new Class<?>[] {SimpleInterface.class}, handler);
Mathieu Chartier72156e22015-07-10 18:26:41 -0700224 if (testGetMethodID(SimpleInterface.class) == 0) {
225 throw new AssertionError();
226 }
227 if (testGetMethodID(proxy.getClass()) == 0) {
228 throw new AssertionError();
229 }
230 }
231
232 private static native long testGetMethodID(Class<?> c);
Hiroshi Yamauchi20a0be02016-02-19 15:44:06 -0800233
234 // Exercise GC and JNI critical sections in parallel.
235 private static void testJniCriticalSectionAndGc() {
236 Thread runGcThread = new Thread(new Runnable() {
237 @Override
238 public void run() {
239 for (int i = 0; i < 10; ++i) {
240 Runtime.getRuntime().gc();
241 }
242 }
243 });
244 Thread jniCriticalThread = new Thread(new Runnable() {
245 @Override
246 public void run() {
247 final int arraySize = 32;
248 byte[] array0 = new byte[arraySize];
249 byte[] array1 = new byte[arraySize];
250 enterJniCriticalSection(arraySize, array0, array1);
251 }
252 });
253 jniCriticalThread.start();
254 runGcThread.start();
255 try {
256 jniCriticalThread.join();
257 runGcThread.join();
258 } catch (InterruptedException ignored) {}
259 }
260
261 private static native void enterJniCriticalSection(int arraySize, byte[] array0, byte[] array);
Alex Lighte9d2ca22016-02-25 16:13:54 -0800262
263 private static native void testInvokeLambdaMethod(LambdaInterface iface);
264
265 private static native void testInvokeLambdaDefaultMethod(LambdaInterface iface);
266}
267
268@FunctionalInterface
269interface LambdaInterface {
270 public void sayHi();
271 public default void sayHiTwice() {
272 sayHi();
273 sayHi();
274 }
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700275}
276
277class JniCallNonvirtualTest {
278 public boolean nonstaticMethodSuperCalled = false;
279 public boolean nonstaticMethodSubCalled = false;
280
281 private static native void testCallNonvirtual();
282
283 public JniCallNonvirtualTest() {
284 System.out.println("Super.<init>");
285 }
286
287 public static void staticMethod() {
288 System.out.println("Super.staticMethod");
289 }
290
291 public void nonstaticMethod() {
292 System.out.println("Super.nonstaticMethod");
293 nonstaticMethodSuperCalled = true;
294 }
295}
296
297class JniCallNonvirtualTestSubclass extends JniCallNonvirtualTest {
298
299 public JniCallNonvirtualTestSubclass() {
300 System.out.println("Subclass.<init>");
301 }
302
303 public static void staticMethod() {
304 System.out.println("Subclass.staticMethod");
305 }
306
307 public void nonstaticMethod() {
308 System.out.println("Subclass.nonstaticMethod");
309 nonstaticMethodSubCalled = true;
310 }
Brian Carlstromce888532013-10-10 00:32:58 -0700311}