blob: 8e9201056e265a568abd5b7c7e6179a1105a5185 [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
Jeff Hao201803f2013-11-20 18:11:39 -080017import java.lang.reflect.Method;
18
Andreas Gampe1c83cbc2014-07-22 18:52:29 -070019public class Main {
Brian Carlstromce888532013-10-10 00:32:58 -070020 public static void main(String[] args) {
21 System.loadLibrary("arttest");
22 testFindClassOnAttachedNativeThread();
Jeff Hao62509b62013-12-10 17:44:56 -080023 testFindFieldOnAttachedNativeThread();
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +010024 testReflectFieldGetFromAttachedNativeThreadNative();
Brian Carlstrom67fe2b42013-10-15 18:51:42 -070025 testCallStaticVoidMethodOnSubClass();
Jeff Hao201803f2013-11-20 18:11:39 -080026 testGetMirandaMethod();
Narayan Kamathef809d02013-12-19 17:52:47 +000027 testZeroLengthByteBuffers();
Andreas Gamped1104322014-05-01 14:38:56 -070028 testByteMethod();
29 testShortMethod();
30 testBooleanMethod();
31 testCharMethod();
Narayan Kamath1268b742014-07-11 19:15:11 +010032 testIsAssignableFromOnPrimitiveTypes();
Andreas Gampe718ac652014-08-11 18:51:53 -070033 testShallowGetCallingClassLoader();
Andreas Gampe0d334ce2014-08-13 23:05:38 -070034 testShallowGetStackClass2();
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -070035 testCallNonvirtual();
Brian Carlstromce888532013-10-10 00:32:58 -070036 }
37
38 private static native void testFindClassOnAttachedNativeThread();
Brian Carlstrom67fe2b42013-10-15 18:51:42 -070039
Jeff Hao62509b62013-12-10 17:44:56 -080040 private static boolean testFindFieldOnAttachedNativeThreadField;
41
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +010042 private static native void testReflectFieldGetFromAttachedNativeThreadNative();
43
44 public static boolean testReflectFieldGetFromAttachedNativeThreadField;
45
Jeff Hao62509b62013-12-10 17:44:56 -080046 private static void testFindFieldOnAttachedNativeThread() {
47 testFindFieldOnAttachedNativeThreadNative();
48 if (!testFindFieldOnAttachedNativeThreadField) {
49 throw new AssertionError();
50 }
51 }
52
53 private static native void testFindFieldOnAttachedNativeThreadNative();
54
Brian Carlstrom67fe2b42013-10-15 18:51:42 -070055 private static void testCallStaticVoidMethodOnSubClass() {
56 testCallStaticVoidMethodOnSubClassNative();
57 if (!testCallStaticVoidMethodOnSubClass_SuperClass.executed) {
58 throw new AssertionError();
59 }
60 }
61
62 private static native void testCallStaticVoidMethodOnSubClassNative();
63
64 private static class testCallStaticVoidMethodOnSubClass_SuperClass {
65 private static boolean executed = false;
66 private static void execute() {
67 executed = true;
68 }
69 }
70
71 private static class testCallStaticVoidMethodOnSubClass_SubClass
72 extends testCallStaticVoidMethodOnSubClass_SuperClass {
73 }
Jeff Hao201803f2013-11-20 18:11:39 -080074
75 private static native Method testGetMirandaMethodNative();
76
77 private static void testGetMirandaMethod() {
78 Method m = testGetMirandaMethodNative();
79 if (m.getDeclaringClass() != testGetMirandaMethod_MirandaInterface.class) {
80 throw new AssertionError();
81 }
82 }
83
Narayan Kamathef809d02013-12-19 17:52:47 +000084 private static native void testZeroLengthByteBuffers();
85
Jeff Hao201803f2013-11-20 18:11:39 -080086 private static abstract class testGetMirandaMethod_MirandaAbstract implements testGetMirandaMethod_MirandaInterface {
87 public boolean inAbstract() {
88 return true;
89 }
90 }
91
92 private static interface testGetMirandaMethod_MirandaInterface {
93 public boolean inInterface();
94 }
Andreas Gamped1104322014-05-01 14:38:56 -070095
96 // Test sign-extension for values < 32b
97
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -070098 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 -070099 byte b8, byte b9, byte b10);
100
101 private static void testByteMethod() {
102 byte returns[] = { 0, 1, 2, 127, -1, -2, -128 };
103 for (int i = 0; i < returns.length; i++) {
104 byte result = byteMethod((byte)i, (byte)2, (byte)(-3), (byte)4, (byte)(-5), (byte)6,
105 (byte)(-7), (byte)8, (byte)(-9), (byte)10);
106 if (returns[i] != result) {
107 System.out.println("Run " + i + " with " + returns[i] + " vs " + result);
108 throw new AssertionError();
109 }
110 }
111 }
112
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700113 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 -0700114 short s8, short s9, short s10);
115
116 private static void testShortMethod() {
117 short returns[] = { 0, 1, 2, 127, 32767, -1, -2, -128, -32768 };
118 for (int i = 0; i < returns.length; i++) {
119 short result = shortMethod((short)i, (short)2, (short)(-3), (short)4, (short)(-5), (short)6,
120 (short)(-7), (short)8, (short)(-9), (short)10);
121 if (returns[i] != result) {
122 System.out.println("Run " + i + " with " + returns[i] + " vs " + result);
123 throw new AssertionError();
124 }
125 }
126 }
127
128 // Test zero-extension for values < 32b
129
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700130 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 -0700131 boolean b8, boolean b9, boolean b10);
132
133 private static void testBooleanMethod() {
134 if (booleanMethod(false, true, false, true, false, true, false, true, false, true)) {
135 throw new AssertionError();
136 }
137
138 if (!booleanMethod(true, true, false, true, false, true, false, true, false, true)) {
139 throw new AssertionError();
140 }
141 }
142
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700143 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 -0700144 char c8, char c9, char c10);
145
146 private static void testCharMethod() {
147 char returns[] = { (char)0, (char)1, (char)2, (char)127, (char)255, (char)256, (char)15000,
148 (char)34000 };
149 for (int i = 0; i < returns.length; i++) {
150 char result = charMethod((char)i, 'a', 'b', 'c', '0', '1', '2', (char)1234, (char)2345,
151 (char)3456);
152 if (returns[i] != result) {
153 System.out.println("Run " + i + " with " + (int)returns[i] + " vs " + (int)result);
154 throw new AssertionError();
155 }
156 }
157 }
Narayan Kamath1268b742014-07-11 19:15:11 +0100158
159 // http://b/16531674
160 private static void testIsAssignableFromOnPrimitiveTypes() {
161 if (!nativeIsAssignableFrom(int.class, Integer.TYPE)) {
162 System.out.println("IsAssignableFrom(int.class, Integer.TYPE) returned false, expected true");
163 throw new AssertionError();
164 }
165
166 if (!nativeIsAssignableFrom(Integer.TYPE, int.class)) {
167 System.out.println("IsAssignableFrom(Integer.TYPE, int.class) returned false, expected true");
168 throw new AssertionError();
169 }
170 }
171
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700172 private static native boolean nativeIsAssignableFrom(Class<?> from, Class<?> to);
Andreas Gampe718ac652014-08-11 18:51:53 -0700173
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700174 private static void testShallowGetCallingClassLoader() {
Andreas Gampe718ac652014-08-11 18:51:53 -0700175 nativeTestShallowGetCallingClassLoader();
176 }
177
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700178 private native static void nativeTestShallowGetCallingClassLoader();
Andreas Gampe0d334ce2014-08-13 23:05:38 -0700179
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700180 private static void testShallowGetStackClass2() {
Andreas Gampe0d334ce2014-08-13 23:05:38 -0700181 nativeTestShallowGetStackClass2();
182 }
183
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700184 private static native void nativeTestShallowGetStackClass2();
185
186 private static native void testCallNonvirtual();
187}
188
189class JniCallNonvirtualTest {
190 public boolean nonstaticMethodSuperCalled = false;
191 public boolean nonstaticMethodSubCalled = false;
192
193 private static native void testCallNonvirtual();
194
195 public JniCallNonvirtualTest() {
196 System.out.println("Super.<init>");
197 }
198
199 public static void staticMethod() {
200 System.out.println("Super.staticMethod");
201 }
202
203 public void nonstaticMethod() {
204 System.out.println("Super.nonstaticMethod");
205 nonstaticMethodSuperCalled = true;
206 }
207}
208
209class JniCallNonvirtualTestSubclass extends JniCallNonvirtualTest {
210
211 public JniCallNonvirtualTestSubclass() {
212 System.out.println("Subclass.<init>");
213 }
214
215 public static void staticMethod() {
216 System.out.println("Subclass.staticMethod");
217 }
218
219 public void nonstaticMethod() {
220 System.out.println("Subclass.nonstaticMethod");
221 nonstaticMethodSubCalled = true;
222 }
Brian Carlstromce888532013-10-10 00:32:58 -0700223}