blob: 6d7d647d070c2c9cc231e3a3ad9f9b7c1dac6a11 [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();
Brian Carlstromce888532013-10-10 00:32:58 -070034 }
35
36 private static native void testFindClassOnAttachedNativeThread();
Brian Carlstrom67fe2b42013-10-15 18:51:42 -070037
Jeff Hao62509b62013-12-10 17:44:56 -080038 private static boolean testFindFieldOnAttachedNativeThreadField;
39
Vladimir Marko3bd7a6c2014-06-12 15:22:31 +010040 private static native void testReflectFieldGetFromAttachedNativeThreadNative();
41
42 public static boolean testReflectFieldGetFromAttachedNativeThreadField;
43
Jeff Hao62509b62013-12-10 17:44:56 -080044 private static void testFindFieldOnAttachedNativeThread() {
45 testFindFieldOnAttachedNativeThreadNative();
46 if (!testFindFieldOnAttachedNativeThreadField) {
47 throw new AssertionError();
48 }
49 }
50
51 private static native void testFindFieldOnAttachedNativeThreadNative();
52
Brian Carlstrom67fe2b42013-10-15 18:51:42 -070053 private static void testCallStaticVoidMethodOnSubClass() {
54 testCallStaticVoidMethodOnSubClassNative();
55 if (!testCallStaticVoidMethodOnSubClass_SuperClass.executed) {
56 throw new AssertionError();
57 }
58 }
59
60 private static native void testCallStaticVoidMethodOnSubClassNative();
61
62 private static class testCallStaticVoidMethodOnSubClass_SuperClass {
63 private static boolean executed = false;
64 private static void execute() {
65 executed = true;
66 }
67 }
68
69 private static class testCallStaticVoidMethodOnSubClass_SubClass
70 extends testCallStaticVoidMethodOnSubClass_SuperClass {
71 }
Jeff Hao201803f2013-11-20 18:11:39 -080072
73 private static native Method testGetMirandaMethodNative();
74
75 private static void testGetMirandaMethod() {
76 Method m = testGetMirandaMethodNative();
77 if (m.getDeclaringClass() != testGetMirandaMethod_MirandaInterface.class) {
78 throw new AssertionError();
79 }
80 }
81
Narayan Kamathef809d02013-12-19 17:52:47 +000082 private static native void testZeroLengthByteBuffers();
83
Jeff Hao201803f2013-11-20 18:11:39 -080084 private static abstract class testGetMirandaMethod_MirandaAbstract implements testGetMirandaMethod_MirandaInterface {
85 public boolean inAbstract() {
86 return true;
87 }
88 }
89
90 private static interface testGetMirandaMethod_MirandaInterface {
91 public boolean inInterface();
92 }
Andreas Gamped1104322014-05-01 14:38:56 -070093
94 // Test sign-extension for values < 32b
95
96 native static byte byteMethod(byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7,
97 byte b8, byte b9, byte b10);
98
99 private static void testByteMethod() {
100 byte returns[] = { 0, 1, 2, 127, -1, -2, -128 };
101 for (int i = 0; i < returns.length; i++) {
102 byte result = byteMethod((byte)i, (byte)2, (byte)(-3), (byte)4, (byte)(-5), (byte)6,
103 (byte)(-7), (byte)8, (byte)(-9), (byte)10);
104 if (returns[i] != result) {
105 System.out.println("Run " + i + " with " + returns[i] + " vs " + result);
106 throw new AssertionError();
107 }
108 }
109 }
110
111 native static short shortMethod(short s1, short s2, short s3, short s4, short s5, short s6, short s7,
112 short s8, short s9, short s10);
113
114 private static void testShortMethod() {
115 short returns[] = { 0, 1, 2, 127, 32767, -1, -2, -128, -32768 };
116 for (int i = 0; i < returns.length; i++) {
117 short result = shortMethod((short)i, (short)2, (short)(-3), (short)4, (short)(-5), (short)6,
118 (short)(-7), (short)8, (short)(-9), (short)10);
119 if (returns[i] != result) {
120 System.out.println("Run " + i + " with " + returns[i] + " vs " + result);
121 throw new AssertionError();
122 }
123 }
124 }
125
126 // Test zero-extension for values < 32b
127
128 native static boolean booleanMethod(boolean b1, boolean b2, boolean b3, boolean b4, boolean b5, boolean b6, boolean b7,
129 boolean b8, boolean b9, boolean b10);
130
131 private static void testBooleanMethod() {
132 if (booleanMethod(false, true, false, true, false, true, false, true, false, true)) {
133 throw new AssertionError();
134 }
135
136 if (!booleanMethod(true, true, false, true, false, true, false, true, false, true)) {
137 throw new AssertionError();
138 }
139 }
140
141 native static char charMethod(char c1, char c2, char c3, char c4, char c5, char c6, char c7,
142 char c8, char c9, char c10);
143
144 private static void testCharMethod() {
145 char returns[] = { (char)0, (char)1, (char)2, (char)127, (char)255, (char)256, (char)15000,
146 (char)34000 };
147 for (int i = 0; i < returns.length; i++) {
148 char result = charMethod((char)i, 'a', 'b', 'c', '0', '1', '2', (char)1234, (char)2345,
149 (char)3456);
150 if (returns[i] != result) {
151 System.out.println("Run " + i + " with " + (int)returns[i] + " vs " + (int)result);
152 throw new AssertionError();
153 }
154 }
155 }
Narayan Kamath1268b742014-07-11 19:15:11 +0100156
157 // http://b/16531674
158 private static void testIsAssignableFromOnPrimitiveTypes() {
159 if (!nativeIsAssignableFrom(int.class, Integer.TYPE)) {
160 System.out.println("IsAssignableFrom(int.class, Integer.TYPE) returned false, expected true");
161 throw new AssertionError();
162 }
163
164 if (!nativeIsAssignableFrom(Integer.TYPE, int.class)) {
165 System.out.println("IsAssignableFrom(Integer.TYPE, int.class) returned false, expected true");
166 throw new AssertionError();
167 }
168 }
169
170 native static boolean nativeIsAssignableFrom(Class<?> from, Class<?> to);
Andreas Gampe718ac652014-08-11 18:51:53 -0700171
172 static void testShallowGetCallingClassLoader() {
173 nativeTestShallowGetCallingClassLoader();
174 }
175
176 native static void nativeTestShallowGetCallingClassLoader();
Brian Carlstromce888532013-10-10 00:32:58 -0700177}