blob: 9f4a8522e73c442017a53fce60a0a7ff63926dfb [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();
Brian Carlstromce888532013-10-10 00:32:58 -070043 }
44
Alex Light36121492016-02-22 13:43:29 -080045 private static native void testCallDefaultMethods();
46
Brian Carlstromce888532013-10-10 00:32:58 -070047 private static native void testFindClassOnAttachedNativeThread();
Brian Carlstrom67fe2b42013-10-15 18:51:42 -070048
Jeff Hao62509b62013-12-10 17:44:56 -080049 private static boolean testFindFieldOnAttachedNativeThreadField;
50
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +010051 private static native void testReflectFieldGetFromAttachedNativeThreadNative();
52
53 public static boolean testReflectFieldGetFromAttachedNativeThreadField;
54
Jeff Hao62509b62013-12-10 17:44:56 -080055 private static void testFindFieldOnAttachedNativeThread() {
56 testFindFieldOnAttachedNativeThreadNative();
57 if (!testFindFieldOnAttachedNativeThreadField) {
58 throw new AssertionError();
59 }
60 }
61
62 private static native void testFindFieldOnAttachedNativeThreadNative();
63
Brian Carlstrom67fe2b42013-10-15 18:51:42 -070064 private static void testCallStaticVoidMethodOnSubClass() {
65 testCallStaticVoidMethodOnSubClassNative();
66 if (!testCallStaticVoidMethodOnSubClass_SuperClass.executed) {
67 throw new AssertionError();
68 }
69 }
70
71 private static native void testCallStaticVoidMethodOnSubClassNative();
72
73 private static class testCallStaticVoidMethodOnSubClass_SuperClass {
74 private static boolean executed = false;
75 private static void execute() {
76 executed = true;
77 }
78 }
79
80 private static class testCallStaticVoidMethodOnSubClass_SubClass
81 extends testCallStaticVoidMethodOnSubClass_SuperClass {
82 }
Jeff Hao201803f2013-11-20 18:11:39 -080083
84 private static native Method testGetMirandaMethodNative();
85
86 private static void testGetMirandaMethod() {
87 Method m = testGetMirandaMethodNative();
88 if (m.getDeclaringClass() != testGetMirandaMethod_MirandaInterface.class) {
89 throw new AssertionError();
90 }
91 }
92
Narayan Kamathef809d02013-12-19 17:52:47 +000093 private static native void testZeroLengthByteBuffers();
94
Jeff Hao201803f2013-11-20 18:11:39 -080095 private static abstract class testGetMirandaMethod_MirandaAbstract implements testGetMirandaMethod_MirandaInterface {
96 public boolean inAbstract() {
97 return true;
98 }
99 }
100
101 private static interface testGetMirandaMethod_MirandaInterface {
102 public boolean inInterface();
103 }
Andreas Gamped1104322014-05-01 14:38:56 -0700104
105 // Test sign-extension for values < 32b
106
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700107 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 -0700108 byte b8, byte b9, byte b10);
109
110 private static void testByteMethod() {
111 byte returns[] = { 0, 1, 2, 127, -1, -2, -128 };
112 for (int i = 0; i < returns.length; i++) {
113 byte result = byteMethod((byte)i, (byte)2, (byte)(-3), (byte)4, (byte)(-5), (byte)6,
114 (byte)(-7), (byte)8, (byte)(-9), (byte)10);
115 if (returns[i] != result) {
116 System.out.println("Run " + i + " with " + returns[i] + " vs " + result);
117 throw new AssertionError();
118 }
119 }
120 }
121
Mathieu Chartierff6d8cf2015-06-02 13:40:12 -0700122 private static native void removeLocalObject(Object o);
123
124 private static void testRemoveLocalObject() {
125 removeLocalObject(new Object());
126 }
Alex Light36121492016-02-22 13:43:29 -0800127
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700128 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 -0700129 short s8, short s9, short s10);
130
131 private static void testShortMethod() {
132 short returns[] = { 0, 1, 2, 127, 32767, -1, -2, -128, -32768 };
133 for (int i = 0; i < returns.length; i++) {
134 short result = shortMethod((short)i, (short)2, (short)(-3), (short)4, (short)(-5), (short)6,
135 (short)(-7), (short)8, (short)(-9), (short)10);
136 if (returns[i] != result) {
137 System.out.println("Run " + i + " with " + returns[i] + " vs " + result);
138 throw new AssertionError();
139 }
140 }
141 }
142
143 // Test zero-extension for values < 32b
144
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700145 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 -0700146 boolean b8, boolean b9, boolean b10);
147
148 private static void testBooleanMethod() {
149 if (booleanMethod(false, true, false, true, false, true, false, true, false, true)) {
150 throw new AssertionError();
151 }
152
153 if (!booleanMethod(true, true, false, true, false, true, false, true, false, true)) {
154 throw new AssertionError();
155 }
156 }
157
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700158 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 -0700159 char c8, char c9, char c10);
160
161 private static void testCharMethod() {
162 char returns[] = { (char)0, (char)1, (char)2, (char)127, (char)255, (char)256, (char)15000,
163 (char)34000 };
164 for (int i = 0; i < returns.length; i++) {
165 char result = charMethod((char)i, 'a', 'b', 'c', '0', '1', '2', (char)1234, (char)2345,
166 (char)3456);
167 if (returns[i] != result) {
168 System.out.println("Run " + i + " with " + (int)returns[i] + " vs " + (int)result);
169 throw new AssertionError();
170 }
171 }
172 }
Narayan Kamath1268b742014-07-11 19:15:11 +0100173
174 // http://b/16531674
175 private static void testIsAssignableFromOnPrimitiveTypes() {
176 if (!nativeIsAssignableFrom(int.class, Integer.TYPE)) {
177 System.out.println("IsAssignableFrom(int.class, Integer.TYPE) returned false, expected true");
178 throw new AssertionError();
179 }
180
181 if (!nativeIsAssignableFrom(Integer.TYPE, int.class)) {
182 System.out.println("IsAssignableFrom(Integer.TYPE, int.class) returned false, expected true");
183 throw new AssertionError();
184 }
185 }
186
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700187 private static native boolean nativeIsAssignableFrom(Class<?> from, Class<?> to);
Andreas Gampe718ac652014-08-11 18:51:53 -0700188
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700189 private static void testShallowGetCallingClassLoader() {
Andreas Gampe718ac652014-08-11 18:51:53 -0700190 nativeTestShallowGetCallingClassLoader();
191 }
192
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700193 private native static void nativeTestShallowGetCallingClassLoader();
Andreas Gampe0d334ce2014-08-13 23:05:38 -0700194
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700195 private static void testShallowGetStackClass2() {
Andreas Gampe0d334ce2014-08-13 23:05:38 -0700196 nativeTestShallowGetStackClass2();
197 }
198
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700199 private static native void nativeTestShallowGetStackClass2();
200
201 private static native void testCallNonvirtual();
Jeff Hao848f70a2014-01-15 13:49:50 -0800202
203 private static native void testNewStringObject();
Mathieu Chartier72156e22015-07-10 18:26:41 -0700204
205 private interface SimpleInterface {
206 void a();
207 }
208
209 private static class DummyInvocationHandler implements InvocationHandler {
210 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
211 return null;
212 }
213 }
214
215 private static void testProxyGetMethodID() {
216 InvocationHandler handler = new DummyInvocationHandler();
217 SimpleInterface proxy =
218 (SimpleInterface) Proxy.newProxyInstance(SimpleInterface.class.getClassLoader(),
219 new Class[] {SimpleInterface.class}, handler);
220 if (testGetMethodID(SimpleInterface.class) == 0) {
221 throw new AssertionError();
222 }
223 if (testGetMethodID(proxy.getClass()) == 0) {
224 throw new AssertionError();
225 }
226 }
227
228 private static native long testGetMethodID(Class<?> c);
Hiroshi Yamauchi20a0be02016-02-19 15:44:06 -0800229
230 // Exercise GC and JNI critical sections in parallel.
231 private static void testJniCriticalSectionAndGc() {
232 Thread runGcThread = new Thread(new Runnable() {
233 @Override
234 public void run() {
235 for (int i = 0; i < 10; ++i) {
236 Runtime.getRuntime().gc();
237 }
238 }
239 });
240 Thread jniCriticalThread = new Thread(new Runnable() {
241 @Override
242 public void run() {
243 final int arraySize = 32;
244 byte[] array0 = new byte[arraySize];
245 byte[] array1 = new byte[arraySize];
246 enterJniCriticalSection(arraySize, array0, array1);
247 }
248 });
249 jniCriticalThread.start();
250 runGcThread.start();
251 try {
252 jniCriticalThread.join();
253 runGcThread.join();
254 } catch (InterruptedException ignored) {}
255 }
256
257 private static native void enterJniCriticalSection(int arraySize, byte[] array0, byte[] array);
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700258}
259
260class JniCallNonvirtualTest {
261 public boolean nonstaticMethodSuperCalled = false;
262 public boolean nonstaticMethodSubCalled = false;
263
264 private static native void testCallNonvirtual();
265
266 public JniCallNonvirtualTest() {
267 System.out.println("Super.<init>");
268 }
269
270 public static void staticMethod() {
271 System.out.println("Super.staticMethod");
272 }
273
274 public void nonstaticMethod() {
275 System.out.println("Super.nonstaticMethod");
276 nonstaticMethodSuperCalled = true;
277 }
278}
279
280class JniCallNonvirtualTestSubclass extends JniCallNonvirtualTest {
281
282 public JniCallNonvirtualTestSubclass() {
283 System.out.println("Subclass.<init>");
284 }
285
286 public static void staticMethod() {
287 System.out.println("Subclass.staticMethod");
288 }
289
290 public void nonstaticMethod() {
291 System.out.println("Subclass.nonstaticMethod");
292 nonstaticMethodSubCalled = true;
293 }
Brian Carlstromce888532013-10-10 00:32:58 -0700294}