blob: 94c02aab1c3a36af9c4e8d26d7f3f655bdab33c3 [file] [log] [blame]
Elliott Hughes0f4c41d2011-09-04 14:58:03 -07001/*
2 * Copyright (C) 2011 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
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070017import java.lang.reflect.*;
18import java.util.*;
19
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -080020class ReflectionTest {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070021 private static boolean z = true;
22 private static byte b = 8;
23 private static char c = 'x';
Elliott Hughes1240dad2011-09-09 16:24:50 -070024 private static double d = Math.PI;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070025 private static float f = 3.14f;
26 private static int i = 32;
27 private static long j = 0x0123456789abcdefL;
28 private static short s = 16;
Elliott Hughes1240dad2011-09-09 16:24:50 -070029
Elliott Hughesd61ae752011-10-02 17:47:42 -070030 public static void testFieldReflection() throws Exception {
31 Field f;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070032
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -080033 f = ReflectionTest.class.getDeclaredField("z");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070034 System.out.println(f.getBoolean(null));
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -080035 f = ReflectionTest.class.getDeclaredField("b");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070036 System.out.println(f.getByte(null));
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -080037 f = ReflectionTest.class.getDeclaredField("c");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070038 System.out.println(f.getChar(null));
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -080039 f = ReflectionTest.class.getDeclaredField("d");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070040 System.out.println(f.getDouble(null));
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -080041 f = ReflectionTest.class.getDeclaredField("f");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070042 System.out.println(f.getFloat(null));
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -080043 f = ReflectionTest.class.getDeclaredField("i");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070044 System.out.println(f.getInt(null));
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -080045 f = ReflectionTest.class.getDeclaredField("j");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070046 System.out.println(f.getLong(null));
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -080047 f = ReflectionTest.class.getDeclaredField("s");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070048 System.out.println(f.getShort(null));
49
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -080050 f = ReflectionTest.class.getDeclaredField("z");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070051 f.setBoolean(null, false);
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -080052 f = ReflectionTest.class.getDeclaredField("b");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070053 f.setByte(null, (byte) 7);
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -080054 f = ReflectionTest.class.getDeclaredField("c");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070055 f.setChar(null, 'y');
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -080056 f = ReflectionTest.class.getDeclaredField("d");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070057 f.setDouble(null, 2.7);
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -080058 f = ReflectionTest.class.getDeclaredField("f");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070059 f.setFloat(null, 2.7f);
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -080060 f = ReflectionTest.class.getDeclaredField("i");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070061 f.setInt(null, 31);
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -080062 f = ReflectionTest.class.getDeclaredField("j");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070063 f.setLong(null, 63);
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -080064 f = ReflectionTest.class.getDeclaredField("s");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070065 f.setShort(null, (short) 15);
66
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -080067 f = ReflectionTest.class.getDeclaredField("z");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070068 System.out.println(f.getBoolean(null));
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -080069 f = ReflectionTest.class.getDeclaredField("b");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070070 System.out.println(f.getByte(null));
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -080071 f = ReflectionTest.class.getDeclaredField("c");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070072 System.out.println(f.getChar(null));
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -080073 f = ReflectionTest.class.getDeclaredField("d");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070074 System.out.println(f.getDouble(null));
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -080075 f = ReflectionTest.class.getDeclaredField("f");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070076 System.out.println(f.getFloat(null));
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -080077 f = ReflectionTest.class.getDeclaredField("i");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070078 System.out.println(f.getInt(null));
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -080079 f = ReflectionTest.class.getDeclaredField("j");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070080 System.out.println(f.getLong(null));
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -080081 f = ReflectionTest.class.getDeclaredField("s");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070082 System.out.println(f.getShort(null));
83
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -080084 f = ReflectionTest.class.getDeclaredField("z");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070085 f.set(null, Boolean.valueOf(true));
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -080086 f = ReflectionTest.class.getDeclaredField("b");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070087 f.set(null, Byte.valueOf((byte) 6));
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -080088 f = ReflectionTest.class.getDeclaredField("c");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070089 f.set(null, Character.valueOf('z'));
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -080090 f = ReflectionTest.class.getDeclaredField("d");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070091 f.set(null, Double.valueOf(1.3));
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -080092 f = ReflectionTest.class.getDeclaredField("f");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070093 f.set(null, Float.valueOf(1.3f));
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -080094 f = ReflectionTest.class.getDeclaredField("i");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070095 f.set(null, Integer.valueOf(30));
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -080096 f = ReflectionTest.class.getDeclaredField("j");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -070097 f.set(null, Long.valueOf(62));
98 f.set(null, Integer.valueOf(62));
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -080099 f = ReflectionTest.class.getDeclaredField("s");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700100 f.set(null, Short.valueOf((short) 14));
101
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -0800102 f = ReflectionTest.class.getDeclaredField("z");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700103 System.out.println(f.getBoolean(null));
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -0800104 f = ReflectionTest.class.getDeclaredField("b");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700105 System.out.println(f.getByte(null));
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -0800106 f = ReflectionTest.class.getDeclaredField("c");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700107 System.out.println(f.getChar(null));
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -0800108 f = ReflectionTest.class.getDeclaredField("d");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700109 System.out.println(f.getDouble(null));
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -0800110 f = ReflectionTest.class.getDeclaredField("f");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700111 System.out.println(f.getFloat(null));
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -0800112 f = ReflectionTest.class.getDeclaredField("i");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700113 System.out.println(f.getInt(null));
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -0800114 f = ReflectionTest.class.getDeclaredField("j");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700115 System.out.println(f.getLong(null));
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -0800116 f = ReflectionTest.class.getDeclaredField("s");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700117 System.out.println(f.getShort(null));
118
119 try {
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -0800120 f = ReflectionTest.class.getDeclaredField("s");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700121 f.set(null, Integer.valueOf(14));
122 } catch (Exception ex) {
123 ex.printStackTrace();
124 }
125
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -0800126 f = ReflectionTest.class.getDeclaredField("z");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700127 show(f.get(null));
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -0800128 f = ReflectionTest.class.getDeclaredField("b");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700129 show(f.get(null));
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -0800130 f = ReflectionTest.class.getDeclaredField("c");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700131 show(f.get(null));
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -0800132 f = ReflectionTest.class.getDeclaredField("d");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700133 show(f.get(null));
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -0800134 f = ReflectionTest.class.getDeclaredField("f");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700135 show(f.get(null));
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -0800136 f = ReflectionTest.class.getDeclaredField("i");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700137 show(f.get(null));
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -0800138 f = ReflectionTest.class.getDeclaredField("j");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700139 show(f.get(null));
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -0800140 f = ReflectionTest.class.getDeclaredField("s");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700141 show(f.get(null));
142
143 /*
144 private static boolean z = true;
145 private static byte b = 8;
146 private static char c = 'x';
147 private static double d = Math.PI;
148 private static float f = 3.14f;
149 private static int i = 32;
150 private static long j = 0x0123456789abcdefL;
151 private static short s = 16;
152 */
153 }
154
155 private static void show(Object o) {
156 System.out.println(o + " (" + (o != null ? o.getClass() : "null") + ")");
157 }
158
Elliott Hughesd61ae752011-10-02 17:47:42 -0700159 public static void testMethodReflection() throws Exception {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700160 System.out.println(Arrays.toString(String.class.getDeclaredConstructors()));
161 System.out.println(Arrays.toString(String.class.getDeclaredFields()));
162 System.out.println(Arrays.toString(String.class.getDeclaredMethods()));
163
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -0800164 System.out.println(Arrays.toString(ReflectionTest.class.getInterfaces()));
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700165 System.out.println(Arrays.toString(String.class.getInterfaces()));
166
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -0800167 System.out.println(ReflectionTest.class.getModifiers());
Elliott Hughesd61ae752011-10-02 17:47:42 -0700168 System.out.println(String.class.getModifiers());
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700169
170 System.out.println(String.class.isAssignableFrom(Object.class));
171 System.out.println(Object.class.isAssignableFrom(String.class));
172
173 System.out.println(String.class.isInstance("hello"));
174 System.out.println(String.class.isInstance(123));
175
Elliott Hughesd61ae752011-10-02 17:47:42 -0700176 Method m;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700177
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -0800178 m = ReflectionTest.class.getDeclaredMethod("IV", int.class);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700179 System.out.println(Arrays.toString(m.getParameterTypes()));
180 show(m.invoke(null, 4444));
181 System.out.println(Arrays.toString(m.getParameterTypes()));
182
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -0800183 m = ReflectionTest.class.getDeclaredMethod("IIV", int.class, int.class);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700184 System.out.println(Arrays.toString(m.getParameterTypes()));
185 show(m.invoke(null, 1111, 2222));
186
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -0800187 m = ReflectionTest.class.getDeclaredMethod("III", int.class, int.class);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700188 System.out.println(Arrays.toString(m.getParameterTypes()));
189 show(m.invoke(null, 1111, 2222));
190
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -0800191 m = ReflectionTest.class.getDeclaredMethod("sumArray", int[].class);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700192 System.out.println(Arrays.toString(m.getParameterTypes()));
193 show(m.invoke(null, new int[] { 1, 2, 3, 4 }));
194
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -0800195 m = ReflectionTest.class.getDeclaredMethod("concat", String[].class);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700196 System.out.println(Arrays.toString(m.getParameterTypes()));
197 show(m.invoke(null, (Object) new String[] { "h", "e", "l", "l", "o" }));
198
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -0800199 m = ReflectionTest.class.getDeclaredMethod("ZBCDFIJSV", boolean.class, byte.class, char.class, double.class, float.class, int.class, long.class, short.class);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700200 System.out.println(Arrays.toString(m.getParameterTypes()));
201 show(m.invoke(null, true, (byte) 0, '1', 2, 3, 4, 5, (short) 6));
202
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -0800203 m = ReflectionTest.class.getDeclaredMethod("ZBCDLFIJSV", boolean.class, byte.class, char.class, double.class, String.class, float.class, int.class, long.class, short.class);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700204 System.out.println(Arrays.toString(m.getParameterTypes()));
205 show(m.invoke(null, true, (byte) 0, '1', 2, "hello world", 3, 4, 5, (short) 6));
206
207 try {
Elliott Hughesfe9cf7a2012-01-09 18:18:25 -0800208 m = ReflectionTest.class.getDeclaredMethod("thrower");
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700209 System.out.println(Arrays.toString(m.getParameterTypes()));
210 show(m.invoke(null));
211 System.out.println("************* should have thrown!");
212 } catch (Exception ex) {
213 ex.printStackTrace();
214 }
215 }
216
217 private static void thrower() {
218 throw new ArithmeticException("surprise!");
219 }
220
Elliott Hughesd61ae752011-10-02 17:47:42 -0700221 private static int sumArray(int[] xs) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700222 int result = 0;
223 for (int x : xs) {
224 result += x;
225 }
226 return result;
227 }
228
Elliott Hughesd61ae752011-10-02 17:47:42 -0700229 private static String concat(String[] strings) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700230 String result = "";
231 for (String s : strings) {
232 result += s;
233 }
234 return result;
235 }
236
Elliott Hughesd61ae752011-10-02 17:47:42 -0700237 private static void IV(int i) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700238 System.out.println(i);
239 }
240
Elliott Hughesd61ae752011-10-02 17:47:42 -0700241 private static void IIV(int i, int j) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700242 System.out.println(i + " " + j);
243 }
244
Elliott Hughesd61ae752011-10-02 17:47:42 -0700245 private static int III(int i, int j) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700246 System.out.println(i + " " + j);
247 return i + j;
248 }
249
Elliott Hughesd61ae752011-10-02 17:47:42 -0700250 private static void ZBCDFIJSV(boolean z, byte b, char c, double d, float f, int i, long l, short s) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700251 System.out.println(z + " " + b + " " + c + " " + d + " " + f + " " + i + " " + l + " " + s);
252 }
253
Elliott Hughesd61ae752011-10-02 17:47:42 -0700254 private static void ZBCDLFIJSV(boolean z, byte b, char c, double d, String string, float f, int i, long l, short s) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700255 System.out.println(z + " " + b + " " + c + " " + d + " " + " " + string + " " + f + " " + i + " " + l + " " + s);
256 }
257
Elliott Hughesd61ae752011-10-02 17:47:42 -0700258 public static void testConstructorReflection() throws Exception {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700259 Constructor<?> ctor;
260
261 ctor = String.class.getConstructor(new Class[0]);
262 show(ctor.newInstance((Object[]) null));
263
264 ctor = String.class.getConstructor(char[].class, int.class, int.class);
265 show(ctor.newInstance(new char[] { 'x', 'y', 'z', '!' }, 1, 2));
266 }
267
Elliott Hughes29f27422011-09-18 16:02:18 -0700268 public static void main(String[] args) throws Exception {
Elliott Hughesd61ae752011-10-02 17:47:42 -0700269 testFieldReflection();
270 testMethodReflection();
271 testConstructorReflection();
Elliott Hughes29f27422011-09-18 16:02:18 -0700272 }
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700273}