blob: 5c39ede68778ef71445ba56092a61462ad78cb3f [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();
Brian Carlstromce888532013-10-10 00:32:58 -070042 }
43
44 private static native void testFindClassOnAttachedNativeThread();
Brian Carlstrom67fe2b42013-10-15 18:51:42 -070045
Jeff Hao62509b62013-12-10 17:44:56 -080046 private static boolean testFindFieldOnAttachedNativeThreadField;
47
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +010048 private static native void testReflectFieldGetFromAttachedNativeThreadNative();
49
50 public static boolean testReflectFieldGetFromAttachedNativeThreadField;
51
Jeff Hao62509b62013-12-10 17:44:56 -080052 private static void testFindFieldOnAttachedNativeThread() {
53 testFindFieldOnAttachedNativeThreadNative();
54 if (!testFindFieldOnAttachedNativeThreadField) {
55 throw new AssertionError();
56 }
57 }
58
59 private static native void testFindFieldOnAttachedNativeThreadNative();
60
Brian Carlstrom67fe2b42013-10-15 18:51:42 -070061 private static void testCallStaticVoidMethodOnSubClass() {
62 testCallStaticVoidMethodOnSubClassNative();
63 if (!testCallStaticVoidMethodOnSubClass_SuperClass.executed) {
64 throw new AssertionError();
65 }
66 }
67
68 private static native void testCallStaticVoidMethodOnSubClassNative();
69
70 private static class testCallStaticVoidMethodOnSubClass_SuperClass {
71 private static boolean executed = false;
72 private static void execute() {
73 executed = true;
74 }
75 }
76
77 private static class testCallStaticVoidMethodOnSubClass_SubClass
78 extends testCallStaticVoidMethodOnSubClass_SuperClass {
79 }
Jeff Hao201803f2013-11-20 18:11:39 -080080
81 private static native Method testGetMirandaMethodNative();
82
83 private static void testGetMirandaMethod() {
84 Method m = testGetMirandaMethodNative();
85 if (m.getDeclaringClass() != testGetMirandaMethod_MirandaInterface.class) {
86 throw new AssertionError();
87 }
88 }
89
Narayan Kamathef809d02013-12-19 17:52:47 +000090 private static native void testZeroLengthByteBuffers();
91
Jeff Hao201803f2013-11-20 18:11:39 -080092 private static abstract class testGetMirandaMethod_MirandaAbstract implements testGetMirandaMethod_MirandaInterface {
93 public boolean inAbstract() {
94 return true;
95 }
96 }
97
98 private static interface testGetMirandaMethod_MirandaInterface {
99 public boolean inInterface();
100 }
Andreas Gamped1104322014-05-01 14:38:56 -0700101
102 // Test sign-extension for values < 32b
103
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700104 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 -0700105 byte b8, byte b9, byte b10);
106
107 private static void testByteMethod() {
108 byte returns[] = { 0, 1, 2, 127, -1, -2, -128 };
109 for (int i = 0; i < returns.length; i++) {
110 byte result = byteMethod((byte)i, (byte)2, (byte)(-3), (byte)4, (byte)(-5), (byte)6,
111 (byte)(-7), (byte)8, (byte)(-9), (byte)10);
112 if (returns[i] != result) {
113 System.out.println("Run " + i + " with " + returns[i] + " vs " + result);
114 throw new AssertionError();
115 }
116 }
117 }
118
Mathieu Chartierff6d8cf2015-06-02 13:40:12 -0700119 private static native void removeLocalObject(Object o);
120
121 private static void testRemoveLocalObject() {
122 removeLocalObject(new Object());
123 }
124
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700125 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 -0700126 short s8, short s9, short s10);
127
128 private static void testShortMethod() {
129 short returns[] = { 0, 1, 2, 127, 32767, -1, -2, -128, -32768 };
130 for (int i = 0; i < returns.length; i++) {
131 short result = shortMethod((short)i, (short)2, (short)(-3), (short)4, (short)(-5), (short)6,
132 (short)(-7), (short)8, (short)(-9), (short)10);
133 if (returns[i] != result) {
134 System.out.println("Run " + i + " with " + returns[i] + " vs " + result);
135 throw new AssertionError();
136 }
137 }
138 }
139
140 // Test zero-extension for values < 32b
141
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700142 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 -0700143 boolean b8, boolean b9, boolean b10);
144
145 private static void testBooleanMethod() {
146 if (booleanMethod(false, true, false, true, false, true, false, true, false, true)) {
147 throw new AssertionError();
148 }
149
150 if (!booleanMethod(true, true, false, true, false, true, false, true, false, true)) {
151 throw new AssertionError();
152 }
153 }
154
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700155 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 -0700156 char c8, char c9, char c10);
157
158 private static void testCharMethod() {
159 char returns[] = { (char)0, (char)1, (char)2, (char)127, (char)255, (char)256, (char)15000,
160 (char)34000 };
161 for (int i = 0; i < returns.length; i++) {
162 char result = charMethod((char)i, 'a', 'b', 'c', '0', '1', '2', (char)1234, (char)2345,
163 (char)3456);
164 if (returns[i] != result) {
165 System.out.println("Run " + i + " with " + (int)returns[i] + " vs " + (int)result);
166 throw new AssertionError();
167 }
168 }
169 }
Narayan Kamath1268b742014-07-11 19:15:11 +0100170
171 // http://b/16531674
172 private static void testIsAssignableFromOnPrimitiveTypes() {
173 if (!nativeIsAssignableFrom(int.class, Integer.TYPE)) {
174 System.out.println("IsAssignableFrom(int.class, Integer.TYPE) returned false, expected true");
175 throw new AssertionError();
176 }
177
178 if (!nativeIsAssignableFrom(Integer.TYPE, int.class)) {
179 System.out.println("IsAssignableFrom(Integer.TYPE, int.class) returned false, expected true");
180 throw new AssertionError();
181 }
182 }
183
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700184 private static native boolean nativeIsAssignableFrom(Class<?> from, Class<?> to);
Andreas Gampe718ac652014-08-11 18:51:53 -0700185
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700186 private static void testShallowGetCallingClassLoader() {
Andreas Gampe718ac652014-08-11 18:51:53 -0700187 nativeTestShallowGetCallingClassLoader();
188 }
189
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700190 private native static void nativeTestShallowGetCallingClassLoader();
Andreas Gampe0d334ce2014-08-13 23:05:38 -0700191
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700192 private static void testShallowGetStackClass2() {
Andreas Gampe0d334ce2014-08-13 23:05:38 -0700193 nativeTestShallowGetStackClass2();
194 }
195
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700196 private static native void nativeTestShallowGetStackClass2();
197
198 private static native void testCallNonvirtual();
Jeff Hao848f70a2014-01-15 13:49:50 -0800199
200 private static native void testNewStringObject();
Mathieu Chartier72156e22015-07-10 18:26:41 -0700201
202 private interface SimpleInterface {
203 void a();
204 }
205
206 private static class DummyInvocationHandler implements InvocationHandler {
207 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
208 return null;
209 }
210 }
211
212 private static void testProxyGetMethodID() {
213 InvocationHandler handler = new DummyInvocationHandler();
214 SimpleInterface proxy =
215 (SimpleInterface) Proxy.newProxyInstance(SimpleInterface.class.getClassLoader(),
216 new Class[] {SimpleInterface.class}, handler);
217 if (testGetMethodID(SimpleInterface.class) == 0) {
218 throw new AssertionError();
219 }
220 if (testGetMethodID(proxy.getClass()) == 0) {
221 throw new AssertionError();
222 }
223 }
224
225 private static native long testGetMethodID(Class<?> c);
Hiroshi Yamauchi20a0be02016-02-19 15:44:06 -0800226
227 // Exercise GC and JNI critical sections in parallel.
228 private static void testJniCriticalSectionAndGc() {
229 Thread runGcThread = new Thread(new Runnable() {
230 @Override
231 public void run() {
232 for (int i = 0; i < 10; ++i) {
233 Runtime.getRuntime().gc();
234 }
235 }
236 });
237 Thread jniCriticalThread = new Thread(new Runnable() {
238 @Override
239 public void run() {
240 final int arraySize = 32;
241 byte[] array0 = new byte[arraySize];
242 byte[] array1 = new byte[arraySize];
243 enterJniCriticalSection(arraySize, array0, array1);
244 }
245 });
246 jniCriticalThread.start();
247 runGcThread.start();
248 try {
249 jniCriticalThread.join();
250 runGcThread.join();
251 } catch (InterruptedException ignored) {}
252 }
253
254 private static native void enterJniCriticalSection(int arraySize, byte[] array0, byte[] array);
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700255}
256
257class JniCallNonvirtualTest {
258 public boolean nonstaticMethodSuperCalled = false;
259 public boolean nonstaticMethodSubCalled = false;
260
261 private static native void testCallNonvirtual();
262
263 public JniCallNonvirtualTest() {
264 System.out.println("Super.<init>");
265 }
266
267 public static void staticMethod() {
268 System.out.println("Super.staticMethod");
269 }
270
271 public void nonstaticMethod() {
272 System.out.println("Super.nonstaticMethod");
273 nonstaticMethodSuperCalled = true;
274 }
275}
276
277class JniCallNonvirtualTestSubclass extends JniCallNonvirtualTest {
278
279 public JniCallNonvirtualTestSubclass() {
280 System.out.println("Subclass.<init>");
281 }
282
283 public static void staticMethod() {
284 System.out.println("Subclass.staticMethod");
285 }
286
287 public void nonstaticMethod() {
288 System.out.println("Subclass.nonstaticMethod");
289 nonstaticMethodSubCalled = true;
290 }
Brian Carlstromce888532013-10-10 00:32:58 -0700291}