blob: 573afdbd1356e8de62e8637f3dc6d2a55556c88e [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
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070021import dalvik.annotation.optimization.FastNative;
22
Andreas Gampe1c83cbc2014-07-22 18:52:29 -070023public class Main {
Brian Carlstromce888532013-10-10 00:32:58 -070024 public static void main(String[] args) {
Mathieu Chartier031768a2015-08-27 10:25:02 -070025 System.loadLibrary(args[0]);
Brian Carlstromce888532013-10-10 00:32:58 -070026 testFindClassOnAttachedNativeThread();
Jeff Hao62509b62013-12-10 17:44:56 -080027 testFindFieldOnAttachedNativeThread();
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +010028 testReflectFieldGetFromAttachedNativeThreadNative();
Brian Carlstrom67fe2b42013-10-15 18:51:42 -070029 testCallStaticVoidMethodOnSubClass();
Jeff Hao201803f2013-11-20 18:11:39 -080030 testGetMirandaMethod();
Narayan Kamathef809d02013-12-19 17:52:47 +000031 testZeroLengthByteBuffers();
Andreas Gamped1104322014-05-01 14:38:56 -070032 testByteMethod();
33 testShortMethod();
34 testBooleanMethod();
35 testCharMethod();
Narayan Kamath1268b742014-07-11 19:15:11 +010036 testIsAssignableFromOnPrimitiveTypes();
Andreas Gampe718ac652014-08-11 18:51:53 -070037 testShallowGetCallingClassLoader();
Andreas Gampe0d334ce2014-08-13 23:05:38 -070038 testShallowGetStackClass2();
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -070039 testCallNonvirtual();
Jeff Hao848f70a2014-01-15 13:49:50 -080040 testNewStringObject();
Mathieu Chartierff6d8cf2015-06-02 13:40:12 -070041 testRemoveLocalObject();
Mathieu Chartier72156e22015-07-10 18:26:41 -070042 testProxyGetMethodID();
Hiroshi Yamauchi20a0be02016-02-19 15:44:06 -080043 testJniCriticalSectionAndGc();
Alex Light36121492016-02-22 13:43:29 -080044 testCallDefaultMethods();
Alex Lighte9d2ca22016-02-25 16:13:54 -080045 String lambda = "λ";
46 testInvokeLambdaMethod(() -> { System.out.println("hi-lambda: " + lambda); });
47 String def = "δ";
48 testInvokeLambdaDefaultMethod(() -> { System.out.println("hi-default " + def + lambda); });
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070049
50 registerNativesJniTest();
51 testFastNativeMethods();
Brian Carlstromce888532013-10-10 00:32:58 -070052 }
53
Igor Murashkin9d4b6da2016-07-29 09:51:58 -070054 private static native boolean registerNativesJniTest();
55
Alex Light36121492016-02-22 13:43:29 -080056 private static native void testCallDefaultMethods();
57
Brian Carlstromce888532013-10-10 00:32:58 -070058 private static native void testFindClassOnAttachedNativeThread();
Brian Carlstrom67fe2b42013-10-15 18:51:42 -070059
Jeff Hao62509b62013-12-10 17:44:56 -080060 private static boolean testFindFieldOnAttachedNativeThreadField;
61
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +010062 private static native void testReflectFieldGetFromAttachedNativeThreadNative();
63
64 public static boolean testReflectFieldGetFromAttachedNativeThreadField;
65
Jeff Hao62509b62013-12-10 17:44:56 -080066 private static void testFindFieldOnAttachedNativeThread() {
67 testFindFieldOnAttachedNativeThreadNative();
68 if (!testFindFieldOnAttachedNativeThreadField) {
69 throw new AssertionError();
70 }
71 }
72
73 private static native void testFindFieldOnAttachedNativeThreadNative();
74
Brian Carlstrom67fe2b42013-10-15 18:51:42 -070075 private static void testCallStaticVoidMethodOnSubClass() {
76 testCallStaticVoidMethodOnSubClassNative();
77 if (!testCallStaticVoidMethodOnSubClass_SuperClass.executed) {
78 throw new AssertionError();
79 }
80 }
81
82 private static native void testCallStaticVoidMethodOnSubClassNative();
83
84 private static class testCallStaticVoidMethodOnSubClass_SuperClass {
85 private static boolean executed = false;
86 private static void execute() {
87 executed = true;
88 }
89 }
90
91 private static class testCallStaticVoidMethodOnSubClass_SubClass
92 extends testCallStaticVoidMethodOnSubClass_SuperClass {
93 }
Jeff Hao201803f2013-11-20 18:11:39 -080094
95 private static native Method testGetMirandaMethodNative();
96
97 private static void testGetMirandaMethod() {
98 Method m = testGetMirandaMethodNative();
99 if (m.getDeclaringClass() != testGetMirandaMethod_MirandaInterface.class) {
100 throw new AssertionError();
101 }
102 }
103
Narayan Kamathef809d02013-12-19 17:52:47 +0000104 private static native void testZeroLengthByteBuffers();
105
Jeff Hao201803f2013-11-20 18:11:39 -0800106 private static abstract class testGetMirandaMethod_MirandaAbstract implements testGetMirandaMethod_MirandaInterface {
107 public boolean inAbstract() {
108 return true;
109 }
110 }
111
112 private static interface testGetMirandaMethod_MirandaInterface {
113 public boolean inInterface();
114 }
Andreas Gamped1104322014-05-01 14:38:56 -0700115
116 // Test sign-extension for values < 32b
117
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700118 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 -0700119 byte b8, byte b9, byte b10);
120
121 private static void testByteMethod() {
122 byte returns[] = { 0, 1, 2, 127, -1, -2, -128 };
123 for (int i = 0; i < returns.length; i++) {
124 byte result = byteMethod((byte)i, (byte)2, (byte)(-3), (byte)4, (byte)(-5), (byte)6,
125 (byte)(-7), (byte)8, (byte)(-9), (byte)10);
126 if (returns[i] != result) {
127 System.out.println("Run " + i + " with " + returns[i] + " vs " + result);
128 throw new AssertionError();
129 }
130 }
131 }
132
Mathieu Chartierff6d8cf2015-06-02 13:40:12 -0700133 private static native void removeLocalObject(Object o);
134
135 private static void testRemoveLocalObject() {
136 removeLocalObject(new Object());
137 }
Alex Light36121492016-02-22 13:43:29 -0800138
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700139 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 -0700140 short s8, short s9, short s10);
141
142 private static void testShortMethod() {
143 short returns[] = { 0, 1, 2, 127, 32767, -1, -2, -128, -32768 };
144 for (int i = 0; i < returns.length; i++) {
145 short result = shortMethod((short)i, (short)2, (short)(-3), (short)4, (short)(-5), (short)6,
146 (short)(-7), (short)8, (short)(-9), (short)10);
147 if (returns[i] != result) {
148 System.out.println("Run " + i + " with " + returns[i] + " vs " + result);
149 throw new AssertionError();
150 }
151 }
152 }
153
154 // Test zero-extension for values < 32b
155
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700156 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 -0700157 boolean b8, boolean b9, boolean b10);
158
159 private static void testBooleanMethod() {
160 if (booleanMethod(false, true, false, true, false, true, false, true, false, true)) {
161 throw new AssertionError();
162 }
163
164 if (!booleanMethod(true, true, false, true, false, true, false, true, false, true)) {
165 throw new AssertionError();
166 }
167 }
168
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700169 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 -0700170 char c8, char c9, char c10);
171
172 private static void testCharMethod() {
173 char returns[] = { (char)0, (char)1, (char)2, (char)127, (char)255, (char)256, (char)15000,
174 (char)34000 };
175 for (int i = 0; i < returns.length; i++) {
176 char result = charMethod((char)i, 'a', 'b', 'c', '0', '1', '2', (char)1234, (char)2345,
177 (char)3456);
178 if (returns[i] != result) {
179 System.out.println("Run " + i + " with " + (int)returns[i] + " vs " + (int)result);
180 throw new AssertionError();
181 }
182 }
183 }
Narayan Kamath1268b742014-07-11 19:15:11 +0100184
185 // http://b/16531674
186 private static void testIsAssignableFromOnPrimitiveTypes() {
187 if (!nativeIsAssignableFrom(int.class, Integer.TYPE)) {
188 System.out.println("IsAssignableFrom(int.class, Integer.TYPE) returned false, expected true");
189 throw new AssertionError();
190 }
191
192 if (!nativeIsAssignableFrom(Integer.TYPE, int.class)) {
193 System.out.println("IsAssignableFrom(Integer.TYPE, int.class) returned false, expected true");
194 throw new AssertionError();
195 }
196 }
197
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700198 private static native boolean nativeIsAssignableFrom(Class<?> from, Class<?> to);
Andreas Gampe718ac652014-08-11 18:51:53 -0700199
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700200 private static void testShallowGetCallingClassLoader() {
Andreas Gampe718ac652014-08-11 18:51:53 -0700201 nativeTestShallowGetCallingClassLoader();
202 }
203
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700204 private native static void nativeTestShallowGetCallingClassLoader();
Andreas Gampe0d334ce2014-08-13 23:05:38 -0700205
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700206 private static void testShallowGetStackClass2() {
Andreas Gampe0d334ce2014-08-13 23:05:38 -0700207 nativeTestShallowGetStackClass2();
208 }
209
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700210 private static native void nativeTestShallowGetStackClass2();
211
212 private static native void testCallNonvirtual();
Jeff Hao848f70a2014-01-15 13:49:50 -0800213
214 private static native void testNewStringObject();
Mathieu Chartier72156e22015-07-10 18:26:41 -0700215
216 private interface SimpleInterface {
217 void a();
218 }
219
220 private static class DummyInvocationHandler implements InvocationHandler {
221 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
222 return null;
223 }
224 }
225
226 private static void testProxyGetMethodID() {
227 InvocationHandler handler = new DummyInvocationHandler();
228 SimpleInterface proxy =
229 (SimpleInterface) Proxy.newProxyInstance(SimpleInterface.class.getClassLoader(),
Andreas Gampe166aaee2016-07-18 08:27:23 -0700230 new Class<?>[] {SimpleInterface.class}, handler);
Mathieu Chartier72156e22015-07-10 18:26:41 -0700231 if (testGetMethodID(SimpleInterface.class) == 0) {
232 throw new AssertionError();
233 }
234 if (testGetMethodID(proxy.getClass()) == 0) {
235 throw new AssertionError();
236 }
237 }
238
239 private static native long testGetMethodID(Class<?> c);
Hiroshi Yamauchi20a0be02016-02-19 15:44:06 -0800240
241 // Exercise GC and JNI critical sections in parallel.
242 private static void testJniCriticalSectionAndGc() {
243 Thread runGcThread = new Thread(new Runnable() {
244 @Override
245 public void run() {
246 for (int i = 0; i < 10; ++i) {
247 Runtime.getRuntime().gc();
248 }
249 }
250 });
251 Thread jniCriticalThread = new Thread(new Runnable() {
252 @Override
253 public void run() {
254 final int arraySize = 32;
255 byte[] array0 = new byte[arraySize];
256 byte[] array1 = new byte[arraySize];
257 enterJniCriticalSection(arraySize, array0, array1);
258 }
259 });
260 jniCriticalThread.start();
261 runGcThread.start();
262 try {
263 jniCriticalThread.join();
264 runGcThread.join();
265 } catch (InterruptedException ignored) {}
266 }
267
268 private static native void enterJniCriticalSection(int arraySize, byte[] array0, byte[] array);
Alex Lighte9d2ca22016-02-25 16:13:54 -0800269
270 private static native void testInvokeLambdaMethod(LambdaInterface iface);
271
272 private static native void testInvokeLambdaDefaultMethod(LambdaInterface iface);
Igor Murashkin9d4b6da2016-07-29 09:51:58 -0700273
274 // Test invoking @FastNative methods works correctly.
275
276 // Return sum of a+b+c.
277 @FastNative
278 static native int intFastNativeMethod(int a, int b, int c);
279
280 private static void testFastNativeMethods() {
281 int returns[] = { 0, 3, 6, 9, 12 };
282 for (int i = 0; i < returns.length; i++) {
283 int result = intFastNativeMethod(i, i, i);
284 if (returns[i] != result) {
285 System.out.println("FastNative Int Run " + i + " with " + returns[i] + " vs " + result);
286 throw new AssertionError();
287 }
288 }
289 }
290
291
Alex Lighte9d2ca22016-02-25 16:13:54 -0800292}
293
294@FunctionalInterface
295interface LambdaInterface {
296 public void sayHi();
297 public default void sayHiTwice() {
298 sayHi();
299 sayHi();
300 }
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700301}
302
303class JniCallNonvirtualTest {
304 public boolean nonstaticMethodSuperCalled = false;
305 public boolean nonstaticMethodSubCalled = false;
306
307 private static native void testCallNonvirtual();
308
309 public JniCallNonvirtualTest() {
310 System.out.println("Super.<init>");
311 }
312
313 public static void staticMethod() {
314 System.out.println("Super.staticMethod");
315 }
316
317 public void nonstaticMethod() {
318 System.out.println("Super.nonstaticMethod");
319 nonstaticMethodSuperCalled = true;
320 }
321}
322
323class JniCallNonvirtualTestSubclass extends JniCallNonvirtualTest {
324
325 public JniCallNonvirtualTestSubclass() {
326 System.out.println("Subclass.<init>");
327 }
328
329 public static void staticMethod() {
330 System.out.println("Subclass.staticMethod");
331 }
332
333 public void nonstaticMethod() {
334 System.out.println("Subclass.nonstaticMethod");
335 nonstaticMethodSubCalled = true;
336 }
Brian Carlstromce888532013-10-10 00:32:58 -0700337}