blob: 584fae3a5758913ce68a2a1eaee2dd44b8a62504 [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();
Jeff Hao848f70a2014-01-15 13:49:50 -080036 testNewStringObject();
Brian Carlstromce888532013-10-10 00:32:58 -070037 }
38
39 private static native void testFindClassOnAttachedNativeThread();
Brian Carlstrom67fe2b42013-10-15 18:51:42 -070040
Jeff Hao62509b62013-12-10 17:44:56 -080041 private static boolean testFindFieldOnAttachedNativeThreadField;
42
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +010043 private static native void testReflectFieldGetFromAttachedNativeThreadNative();
44
45 public static boolean testReflectFieldGetFromAttachedNativeThreadField;
46
Jeff Hao62509b62013-12-10 17:44:56 -080047 private static void testFindFieldOnAttachedNativeThread() {
48 testFindFieldOnAttachedNativeThreadNative();
49 if (!testFindFieldOnAttachedNativeThreadField) {
50 throw new AssertionError();
51 }
52 }
53
54 private static native void testFindFieldOnAttachedNativeThreadNative();
55
Brian Carlstrom67fe2b42013-10-15 18:51:42 -070056 private static void testCallStaticVoidMethodOnSubClass() {
57 testCallStaticVoidMethodOnSubClassNative();
58 if (!testCallStaticVoidMethodOnSubClass_SuperClass.executed) {
59 throw new AssertionError();
60 }
61 }
62
63 private static native void testCallStaticVoidMethodOnSubClassNative();
64
65 private static class testCallStaticVoidMethodOnSubClass_SuperClass {
66 private static boolean executed = false;
67 private static void execute() {
68 executed = true;
69 }
70 }
71
72 private static class testCallStaticVoidMethodOnSubClass_SubClass
73 extends testCallStaticVoidMethodOnSubClass_SuperClass {
74 }
Jeff Hao201803f2013-11-20 18:11:39 -080075
76 private static native Method testGetMirandaMethodNative();
77
78 private static void testGetMirandaMethod() {
79 Method m = testGetMirandaMethodNative();
80 if (m.getDeclaringClass() != testGetMirandaMethod_MirandaInterface.class) {
81 throw new AssertionError();
82 }
83 }
84
Narayan Kamathef809d02013-12-19 17:52:47 +000085 private static native void testZeroLengthByteBuffers();
86
Jeff Hao201803f2013-11-20 18:11:39 -080087 private static abstract class testGetMirandaMethod_MirandaAbstract implements testGetMirandaMethod_MirandaInterface {
88 public boolean inAbstract() {
89 return true;
90 }
91 }
92
93 private static interface testGetMirandaMethod_MirandaInterface {
94 public boolean inInterface();
95 }
Andreas Gamped1104322014-05-01 14:38:56 -070096
97 // Test sign-extension for values < 32b
98
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -070099 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 -0700100 byte b8, byte b9, byte b10);
101
102 private static void testByteMethod() {
103 byte returns[] = { 0, 1, 2, 127, -1, -2, -128 };
104 for (int i = 0; i < returns.length; i++) {
105 byte result = byteMethod((byte)i, (byte)2, (byte)(-3), (byte)4, (byte)(-5), (byte)6,
106 (byte)(-7), (byte)8, (byte)(-9), (byte)10);
107 if (returns[i] != result) {
108 System.out.println("Run " + i + " with " + returns[i] + " vs " + result);
109 throw new AssertionError();
110 }
111 }
112 }
113
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700114 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 -0700115 short s8, short s9, short s10);
116
117 private static void testShortMethod() {
118 short returns[] = { 0, 1, 2, 127, 32767, -1, -2, -128, -32768 };
119 for (int i = 0; i < returns.length; i++) {
120 short result = shortMethod((short)i, (short)2, (short)(-3), (short)4, (short)(-5), (short)6,
121 (short)(-7), (short)8, (short)(-9), (short)10);
122 if (returns[i] != result) {
123 System.out.println("Run " + i + " with " + returns[i] + " vs " + result);
124 throw new AssertionError();
125 }
126 }
127 }
128
129 // Test zero-extension for values < 32b
130
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700131 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 -0700132 boolean b8, boolean b9, boolean b10);
133
134 private static void testBooleanMethod() {
135 if (booleanMethod(false, true, false, true, false, true, false, true, false, true)) {
136 throw new AssertionError();
137 }
138
139 if (!booleanMethod(true, true, false, true, false, true, false, true, false, true)) {
140 throw new AssertionError();
141 }
142 }
143
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700144 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 -0700145 char c8, char c9, char c10);
146
147 private static void testCharMethod() {
148 char returns[] = { (char)0, (char)1, (char)2, (char)127, (char)255, (char)256, (char)15000,
149 (char)34000 };
150 for (int i = 0; i < returns.length; i++) {
151 char result = charMethod((char)i, 'a', 'b', 'c', '0', '1', '2', (char)1234, (char)2345,
152 (char)3456);
153 if (returns[i] != result) {
154 System.out.println("Run " + i + " with " + (int)returns[i] + " vs " + (int)result);
155 throw new AssertionError();
156 }
157 }
158 }
Narayan Kamath1268b742014-07-11 19:15:11 +0100159
160 // http://b/16531674
161 private static void testIsAssignableFromOnPrimitiveTypes() {
162 if (!nativeIsAssignableFrom(int.class, Integer.TYPE)) {
163 System.out.println("IsAssignableFrom(int.class, Integer.TYPE) returned false, expected true");
164 throw new AssertionError();
165 }
166
167 if (!nativeIsAssignableFrom(Integer.TYPE, int.class)) {
168 System.out.println("IsAssignableFrom(Integer.TYPE, int.class) returned false, expected true");
169 throw new AssertionError();
170 }
171 }
172
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700173 private static native boolean nativeIsAssignableFrom(Class<?> from, Class<?> to);
Andreas Gampe718ac652014-08-11 18:51:53 -0700174
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700175 private static void testShallowGetCallingClassLoader() {
Andreas Gampe718ac652014-08-11 18:51:53 -0700176 nativeTestShallowGetCallingClassLoader();
177 }
178
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700179 private native static void nativeTestShallowGetCallingClassLoader();
Andreas Gampe0d334ce2014-08-13 23:05:38 -0700180
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700181 private static void testShallowGetStackClass2() {
Andreas Gampe0d334ce2014-08-13 23:05:38 -0700182 nativeTestShallowGetStackClass2();
183 }
184
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700185 private static native void nativeTestShallowGetStackClass2();
186
187 private static native void testCallNonvirtual();
Jeff Hao848f70a2014-01-15 13:49:50 -0800188
189 private static native void testNewStringObject();
Brian Carlstrom58e5e5d2014-09-07 23:52:02 -0700190}
191
192class JniCallNonvirtualTest {
193 public boolean nonstaticMethodSuperCalled = false;
194 public boolean nonstaticMethodSubCalled = false;
195
196 private static native void testCallNonvirtual();
197
198 public JniCallNonvirtualTest() {
199 System.out.println("Super.<init>");
200 }
201
202 public static void staticMethod() {
203 System.out.println("Super.staticMethod");
204 }
205
206 public void nonstaticMethod() {
207 System.out.println("Super.nonstaticMethod");
208 nonstaticMethodSuperCalled = true;
209 }
210}
211
212class JniCallNonvirtualTestSubclass extends JniCallNonvirtualTest {
213
214 public JniCallNonvirtualTestSubclass() {
215 System.out.println("Subclass.<init>");
216 }
217
218 public static void staticMethod() {
219 System.out.println("Subclass.staticMethod");
220 }
221
222 public void nonstaticMethod() {
223 System.out.println("Subclass.nonstaticMethod");
224 nonstaticMethodSubCalled = true;
225 }
Brian Carlstromce888532013-10-10 00:32:58 -0700226}