blob: dd88db0b7c4d8fc8f18d12ee86a96dd67fa04505 [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 Chartier22c1caa2015-06-02 13:40:12 -070039 testRemoveLocalObject();
Mathieu Chartier72156e22015-07-10 18:26:41 -070040 testProxyGetMethodID();
Brian Carlstromce888532013-10-10 00:32:58 -070041 }
42
43 private static native void testFindClassOnAttachedNativeThread();
Brian Carlstrom67fe2b42013-10-15 18:51:42 -070044
Jeff Hao62509b62013-12-10 17:44:56 -080045 private static boolean testFindFieldOnAttachedNativeThreadField;
46
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +010047 private static native void testReflectFieldGetFromAttachedNativeThreadNative();
48
49 public static boolean testReflectFieldGetFromAttachedNativeThreadField;
50
Jeff Hao62509b62013-12-10 17:44:56 -080051 private static void testFindFieldOnAttachedNativeThread() {
52 testFindFieldOnAttachedNativeThreadNative();
53 if (!testFindFieldOnAttachedNativeThreadField) {
54 throw new AssertionError();
55 }
56 }
57
58 private static native void testFindFieldOnAttachedNativeThreadNative();
59
Brian Carlstrom67fe2b42013-10-15 18:51:42 -070060 private static void testCallStaticVoidMethodOnSubClass() {
61 testCallStaticVoidMethodOnSubClassNative();
62 if (!testCallStaticVoidMethodOnSubClass_SuperClass.executed) {
63 throw new AssertionError();
64 }
65 }
66
67 private static native void testCallStaticVoidMethodOnSubClassNative();
68
69 private static class testCallStaticVoidMethodOnSubClass_SuperClass {
70 private static boolean executed = false;
71 private static void execute() {
72 executed = true;
73 }
74 }
75
76 private static class testCallStaticVoidMethodOnSubClass_SubClass
77 extends testCallStaticVoidMethodOnSubClass_SuperClass {
78 }
Jeff Hao201803f2013-11-20 18:11:39 -080079
80 private static native Method testGetMirandaMethodNative();
81
82 private static void testGetMirandaMethod() {
83 Method m = testGetMirandaMethodNative();
84 if (m.getDeclaringClass() != testGetMirandaMethod_MirandaInterface.class) {
85 throw new AssertionError();
86 }
87 }
88
Narayan Kamathef809d02013-12-19 17:52:47 +000089 private static native void testZeroLengthByteBuffers();
90
Jeff Hao201803f2013-11-20 18:11:39 -080091 private static abstract class testGetMirandaMethod_MirandaAbstract implements testGetMirandaMethod_MirandaInterface {
92 public boolean inAbstract() {
93 return true;
94 }
95 }
96
97 private static interface testGetMirandaMethod_MirandaInterface {
98 public boolean inInterface();
99 }
Andreas Gamped1104322014-05-01 14:38:56 -0700100
101 // Test sign-extension for values < 32b
102
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700103 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 -0700104 byte b8, byte b9, byte b10);
105
106 private static void testByteMethod() {
107 byte returns[] = { 0, 1, 2, 127, -1, -2, -128 };
108 for (int i = 0; i < returns.length; i++) {
109 byte result = byteMethod((byte)i, (byte)2, (byte)(-3), (byte)4, (byte)(-5), (byte)6,
110 (byte)(-7), (byte)8, (byte)(-9), (byte)10);
111 if (returns[i] != result) {
112 System.out.println("Run " + i + " with " + returns[i] + " vs " + result);
113 throw new AssertionError();
114 }
115 }
116 }
117
Mathieu Chartier22c1caa2015-06-02 13:40:12 -0700118 private static native void removeLocalObject(Object o);
119
120 private static void testRemoveLocalObject() {
121 removeLocalObject(new Object());
122 }
123
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700124 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 -0700125 short s8, short s9, short s10);
126
127 private static void testShortMethod() {
128 short returns[] = { 0, 1, 2, 127, 32767, -1, -2, -128, -32768 };
129 for (int i = 0; i < returns.length; i++) {
130 short result = shortMethod((short)i, (short)2, (short)(-3), (short)4, (short)(-5), (short)6,
131 (short)(-7), (short)8, (short)(-9), (short)10);
132 if (returns[i] != result) {
133 System.out.println("Run " + i + " with " + returns[i] + " vs " + result);
134 throw new AssertionError();
135 }
136 }
137 }
138
139 // Test zero-extension for values < 32b
140
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700141 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 -0700142 boolean b8, boolean b9, boolean b10);
143
144 private static void testBooleanMethod() {
145 if (booleanMethod(false, true, false, true, false, true, false, true, false, true)) {
146 throw new AssertionError();
147 }
148
149 if (!booleanMethod(true, true, false, true, false, true, false, true, false, true)) {
150 throw new AssertionError();
151 }
152 }
153
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700154 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 -0700155 char c8, char c9, char c10);
156
157 private static void testCharMethod() {
158 char returns[] = { (char)0, (char)1, (char)2, (char)127, (char)255, (char)256, (char)15000,
159 (char)34000 };
160 for (int i = 0; i < returns.length; i++) {
161 char result = charMethod((char)i, 'a', 'b', 'c', '0', '1', '2', (char)1234, (char)2345,
162 (char)3456);
163 if (returns[i] != result) {
164 System.out.println("Run " + i + " with " + (int)returns[i] + " vs " + (int)result);
165 throw new AssertionError();
166 }
167 }
168 }
Narayan Kamath1268b742014-07-11 19:15:11 +0100169
170 // http://b/16531674
171 private static void testIsAssignableFromOnPrimitiveTypes() {
172 if (!nativeIsAssignableFrom(int.class, Integer.TYPE)) {
173 System.out.println("IsAssignableFrom(int.class, Integer.TYPE) returned false, expected true");
174 throw new AssertionError();
175 }
176
177 if (!nativeIsAssignableFrom(Integer.TYPE, int.class)) {
178 System.out.println("IsAssignableFrom(Integer.TYPE, int.class) returned false, expected true");
179 throw new AssertionError();
180 }
181 }
182
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700183 private static native boolean nativeIsAssignableFrom(Class<?> from, Class<?> to);
Andreas Gampe718ac652014-08-11 18:51:53 -0700184
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700185 private static void testShallowGetCallingClassLoader() {
Andreas Gampe718ac652014-08-11 18:51:53 -0700186 nativeTestShallowGetCallingClassLoader();
187 }
188
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700189 private native static void nativeTestShallowGetCallingClassLoader();
Andreas Gampe0d334ce2014-08-13 23:05:38 -0700190
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700191 private static void testShallowGetStackClass2() {
Andreas Gampe0d334ce2014-08-13 23:05:38 -0700192 nativeTestShallowGetStackClass2();
193 }
194
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700195 private static native void nativeTestShallowGetStackClass2();
196
197 private static native void testCallNonvirtual();
Jeff Hao848f70a2014-01-15 13:49:50 -0800198
199 private static native void testNewStringObject();
Mathieu Chartier72156e22015-07-10 18:26:41 -0700200
201 private interface SimpleInterface {
202 void a();
203 }
204
205 private static class DummyInvocationHandler implements InvocationHandler {
206 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
207 return null;
208 }
209 }
210
211 private static void testProxyGetMethodID() {
212 InvocationHandler handler = new DummyInvocationHandler();
213 SimpleInterface proxy =
214 (SimpleInterface) Proxy.newProxyInstance(SimpleInterface.class.getClassLoader(),
215 new Class[] {SimpleInterface.class}, handler);
216 if (testGetMethodID(SimpleInterface.class) == 0) {
217 throw new AssertionError();
218 }
219 if (testGetMethodID(proxy.getClass()) == 0) {
220 throw new AssertionError();
221 }
222 }
223
224 private static native long testGetMethodID(Class<?> c);
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700225}
226
227class JniCallNonvirtualTest {
228 public boolean nonstaticMethodSuperCalled = false;
229 public boolean nonstaticMethodSubCalled = false;
230
231 private static native void testCallNonvirtual();
232
233 public JniCallNonvirtualTest() {
234 System.out.println("Super.<init>");
235 }
236
237 public static void staticMethod() {
238 System.out.println("Super.staticMethod");
239 }
240
241 public void nonstaticMethod() {
242 System.out.println("Super.nonstaticMethod");
243 nonstaticMethodSuperCalled = true;
244 }
245}
246
247class JniCallNonvirtualTestSubclass extends JniCallNonvirtualTest {
248
249 public JniCallNonvirtualTestSubclass() {
250 System.out.println("Subclass.<init>");
251 }
252
253 public static void staticMethod() {
254 System.out.println("Subclass.staticMethod");
255 }
256
257 public void nonstaticMethod() {
258 System.out.println("Subclass.nonstaticMethod");
259 nonstaticMethodSubCalled = true;
260 }
Brian Carlstromce888532013-10-10 00:32:58 -0700261}