blob: 7872a926aa890cea9c285d62bee632229aa52c51 [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 Hughes0f4c41d2011-09-04 14:58:03 -070020class SystemMethods {
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
33 f = SystemMethods.class.getDeclaredField("z");
34 System.out.println(f.getBoolean(null));
35 f = SystemMethods.class.getDeclaredField("b");
36 System.out.println(f.getByte(null));
37 f = SystemMethods.class.getDeclaredField("c");
38 System.out.println(f.getChar(null));
39 f = SystemMethods.class.getDeclaredField("d");
40 System.out.println(f.getDouble(null));
41 f = SystemMethods.class.getDeclaredField("f");
42 System.out.println(f.getFloat(null));
43 f = SystemMethods.class.getDeclaredField("i");
44 System.out.println(f.getInt(null));
45 f = SystemMethods.class.getDeclaredField("j");
46 System.out.println(f.getLong(null));
47 f = SystemMethods.class.getDeclaredField("s");
48 System.out.println(f.getShort(null));
49
50 f = SystemMethods.class.getDeclaredField("z");
51 f.setBoolean(null, false);
52 f = SystemMethods.class.getDeclaredField("b");
53 f.setByte(null, (byte) 7);
54 f = SystemMethods.class.getDeclaredField("c");
55 f.setChar(null, 'y');
56 f = SystemMethods.class.getDeclaredField("d");
57 f.setDouble(null, 2.7);
58 f = SystemMethods.class.getDeclaredField("f");
59 f.setFloat(null, 2.7f);
60 f = SystemMethods.class.getDeclaredField("i");
61 f.setInt(null, 31);
62 f = SystemMethods.class.getDeclaredField("j");
63 f.setLong(null, 63);
64 f = SystemMethods.class.getDeclaredField("s");
65 f.setShort(null, (short) 15);
66
67 f = SystemMethods.class.getDeclaredField("z");
68 System.out.println(f.getBoolean(null));
69 f = SystemMethods.class.getDeclaredField("b");
70 System.out.println(f.getByte(null));
71 f = SystemMethods.class.getDeclaredField("c");
72 System.out.println(f.getChar(null));
73 f = SystemMethods.class.getDeclaredField("d");
74 System.out.println(f.getDouble(null));
75 f = SystemMethods.class.getDeclaredField("f");
76 System.out.println(f.getFloat(null));
77 f = SystemMethods.class.getDeclaredField("i");
78 System.out.println(f.getInt(null));
79 f = SystemMethods.class.getDeclaredField("j");
80 System.out.println(f.getLong(null));
81 f = SystemMethods.class.getDeclaredField("s");
82 System.out.println(f.getShort(null));
83
84 f = SystemMethods.class.getDeclaredField("z");
85 f.set(null, Boolean.valueOf(true));
86 f = SystemMethods.class.getDeclaredField("b");
87 f.set(null, Byte.valueOf((byte) 6));
88 f = SystemMethods.class.getDeclaredField("c");
89 f.set(null, Character.valueOf('z'));
90 f = SystemMethods.class.getDeclaredField("d");
91 f.set(null, Double.valueOf(1.3));
92 f = SystemMethods.class.getDeclaredField("f");
93 f.set(null, Float.valueOf(1.3f));
94 f = SystemMethods.class.getDeclaredField("i");
95 f.set(null, Integer.valueOf(30));
96 f = SystemMethods.class.getDeclaredField("j");
97 f.set(null, Long.valueOf(62));
98 f.set(null, Integer.valueOf(62));
99 f = SystemMethods.class.getDeclaredField("s");
100 f.set(null, Short.valueOf((short) 14));
101
102 f = SystemMethods.class.getDeclaredField("z");
103 System.out.println(f.getBoolean(null));
104 f = SystemMethods.class.getDeclaredField("b");
105 System.out.println(f.getByte(null));
106 f = SystemMethods.class.getDeclaredField("c");
107 System.out.println(f.getChar(null));
108 f = SystemMethods.class.getDeclaredField("d");
109 System.out.println(f.getDouble(null));
110 f = SystemMethods.class.getDeclaredField("f");
111 System.out.println(f.getFloat(null));
112 f = SystemMethods.class.getDeclaredField("i");
113 System.out.println(f.getInt(null));
114 f = SystemMethods.class.getDeclaredField("j");
115 System.out.println(f.getLong(null));
116 f = SystemMethods.class.getDeclaredField("s");
117 System.out.println(f.getShort(null));
118
119 try {
120 f = SystemMethods.class.getDeclaredField("s");
121 f.set(null, Integer.valueOf(14));
122 } catch (Exception ex) {
123 ex.printStackTrace();
124 }
125
126 f = SystemMethods.class.getDeclaredField("z");
127 show(f.get(null));
128 f = SystemMethods.class.getDeclaredField("b");
129 show(f.get(null));
130 f = SystemMethods.class.getDeclaredField("c");
131 show(f.get(null));
132 f = SystemMethods.class.getDeclaredField("d");
133 show(f.get(null));
134 f = SystemMethods.class.getDeclaredField("f");
135 show(f.get(null));
136 f = SystemMethods.class.getDeclaredField("i");
137 show(f.get(null));
138 f = SystemMethods.class.getDeclaredField("j");
139 show(f.get(null));
140 f = SystemMethods.class.getDeclaredField("s");
141 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
164 System.out.println(Arrays.toString(SystemMethods.class.getInterfaces()));
165 System.out.println(Arrays.toString(String.class.getInterfaces()));
166
Elliott Hughesd61ae752011-10-02 17:47:42 -0700167 System.out.println(SystemMethods.class.getModifiers());
168 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
178 m = SystemMethods.class.getDeclaredMethod("IV", int.class);
179 System.out.println(Arrays.toString(m.getParameterTypes()));
180 show(m.invoke(null, 4444));
181 System.out.println(Arrays.toString(m.getParameterTypes()));
182
183 m = SystemMethods.class.getDeclaredMethod("IIV", int.class, int.class);
184 System.out.println(Arrays.toString(m.getParameterTypes()));
185 show(m.invoke(null, 1111, 2222));
186
187 m = SystemMethods.class.getDeclaredMethod("III", int.class, int.class);
188 System.out.println(Arrays.toString(m.getParameterTypes()));
189 show(m.invoke(null, 1111, 2222));
190
191 m = SystemMethods.class.getDeclaredMethod("sumArray", int[].class);
192 System.out.println(Arrays.toString(m.getParameterTypes()));
193 show(m.invoke(null, new int[] { 1, 2, 3, 4 }));
194
195 m = SystemMethods.class.getDeclaredMethod("concat", String[].class);
196 System.out.println(Arrays.toString(m.getParameterTypes()));
197 show(m.invoke(null, (Object) new String[] { "h", "e", "l", "l", "o" }));
198
199 m = SystemMethods.class.getDeclaredMethod("ZBCDFIJSV", boolean.class, byte.class, char.class, double.class, float.class, int.class, long.class, short.class);
200 System.out.println(Arrays.toString(m.getParameterTypes()));
201 show(m.invoke(null, true, (byte) 0, '1', 2, 3, 4, 5, (short) 6));
202
203 m = SystemMethods.class.getDeclaredMethod("ZBCDLFIJSV", boolean.class, byte.class, char.class, double.class, String.class, float.class, int.class, long.class, short.class);
204 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 {
208 m = SystemMethods.class.getDeclaredMethod("thrower");
209 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}