1 /*
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 
17 import java.lang.reflect.*;
18 import java.util.*;
19 
20 class Main {
21   private static boolean z = true;
22   private static byte b = 8;
23   private static char c = 'x';
24   private static double d = Math.PI;
25   private static float f = 3.14f;
26   private static int i = 32;
27   private static long j = 0x0123456789abcdefL;
28   private static short s = 16;
29 
testFieldReflection()30   public static void testFieldReflection() throws Exception {
31     Field f;
32 
33     f = Main.class.getDeclaredField("z");
34     System.out.println(f.getBoolean(null));
35     f = Main.class.getDeclaredField("b");
36     System.out.println(f.getByte(null));
37     f = Main.class.getDeclaredField("c");
38     System.out.println(f.getChar(null));
39     f = Main.class.getDeclaredField("d");
40     System.out.println(f.getDouble(null));
41     f = Main.class.getDeclaredField("f");
42     System.out.println(f.getFloat(null));
43     f = Main.class.getDeclaredField("i");
44     System.out.println(f.getInt(null));
45     f = Main.class.getDeclaredField("j");
46     System.out.println(f.getLong(null));
47     f = Main.class.getDeclaredField("s");
48     System.out.println(f.getShort(null));
49 
50     f = Main.class.getDeclaredField("z");
51     f.setBoolean(null, false);
52     f = Main.class.getDeclaredField("b");
53     f.setByte(null, (byte) 7);
54     f = Main.class.getDeclaredField("c");
55     f.setChar(null, 'y');
56     f = Main.class.getDeclaredField("d");
57     f.setDouble(null, 2.7);
58     f = Main.class.getDeclaredField("f");
59     f.setFloat(null, 2.7f);
60     f = Main.class.getDeclaredField("i");
61     f.setInt(null, 31);
62     f = Main.class.getDeclaredField("j");
63     f.setLong(null, 63);
64     f = Main.class.getDeclaredField("s");
65     f.setShort(null, (short) 15);
66 
67     f = Main.class.getDeclaredField("z");
68     System.out.println(f.getBoolean(null));
69     f = Main.class.getDeclaredField("b");
70     System.out.println(f.getByte(null));
71     f = Main.class.getDeclaredField("c");
72     System.out.println(f.getChar(null));
73     f = Main.class.getDeclaredField("d");
74     System.out.println(f.getDouble(null));
75     f = Main.class.getDeclaredField("f");
76     System.out.println(f.getFloat(null));
77     f = Main.class.getDeclaredField("i");
78     System.out.println(f.getInt(null));
79     f = Main.class.getDeclaredField("j");
80     System.out.println(f.getLong(null));
81     f = Main.class.getDeclaredField("s");
82     System.out.println(f.getShort(null));
83 
84     f = Main.class.getDeclaredField("z");
85     f.set(null, Boolean.valueOf(true));
86     f = Main.class.getDeclaredField("b");
87     f.set(null, Byte.valueOf((byte) 6));
88     f = Main.class.getDeclaredField("c");
89     f.set(null, Character.valueOf('z'));
90     f = Main.class.getDeclaredField("d");
91     f.set(null, Double.valueOf(1.3));
92     f = Main.class.getDeclaredField("f");
93     f.set(null, Float.valueOf(1.3f));
94     f = Main.class.getDeclaredField("i");
95     f.set(null, Integer.valueOf(30));
96     f = Main.class.getDeclaredField("j");
97     f.set(null, Long.valueOf(62));
98     f.set(null, Integer.valueOf(62));
99     f = Main.class.getDeclaredField("s");
100     f.set(null, Short.valueOf((short) 14));
101 
102     f = Main.class.getDeclaredField("z");
103     System.out.println(f.getBoolean(null));
104     f = Main.class.getDeclaredField("b");
105     System.out.println(f.getByte(null));
106     f = Main.class.getDeclaredField("c");
107     System.out.println(f.getChar(null));
108     f = Main.class.getDeclaredField("d");
109     System.out.println(f.getDouble(null));
110     f = Main.class.getDeclaredField("f");
111     System.out.println(f.getFloat(null));
112     f = Main.class.getDeclaredField("i");
113     System.out.println(f.getInt(null));
114     f = Main.class.getDeclaredField("j");
115     System.out.println(f.getLong(null));
116     f = Main.class.getDeclaredField("s");
117     System.out.println(f.getShort(null));
118 
119     try {
120       f = Main.class.getDeclaredField("s");
121       f.set(null, Integer.valueOf(14));
122       System.out.println("************* should have thrown!");
123     } catch (IllegalArgumentException expected) {
124       System.out.println("got expected IllegalArgumentException");
125     }
126 
127     f = Main.class.getDeclaredField("z");
128     show(f.get(null));
129     f = Main.class.getDeclaredField("b");
130     show(f.get(null));
131     f = Main.class.getDeclaredField("c");
132     show(f.get(null));
133     f = Main.class.getDeclaredField("d");
134     show(f.get(null));
135     f = Main.class.getDeclaredField("f");
136     show(f.get(null));
137     f = Main.class.getDeclaredField("i");
138     show(f.get(null));
139     f = Main.class.getDeclaredField("j");
140     show(f.get(null));
141     f = Main.class.getDeclaredField("s");
142     show(f.get(null));
143 
144     /*
145     private static boolean z = true;
146     private static byte b = 8;
147     private static char c = 'x';
148     private static double d = Math.PI;
149     private static float f = 3.14f;
150     private static int i = 32;
151     private static long j = 0x0123456789abcdefL;
152     private static short s = 16;
153     */
154   }
155 
show(Object o)156   private static void show(Object o) {
157     System.out.println(o + " (" + (o != null ? o.getClass() : "null") + ")");
158   }
159 
testMethodReflection()160   public static void testMethodReflection() throws Exception {
161     System.out.println(Arrays.toString(String.class.getDeclaredConstructors()));
162     System.out.println(Arrays.toString(String.class.getDeclaredFields()));
163     System.out.println(Arrays.toString(String.class.getDeclaredMethods()));
164 
165     System.out.println(Arrays.toString(Main.class.getInterfaces()));
166     System.out.println(Arrays.toString(String.class.getInterfaces()));
167 
168     System.out.println(Main.class.getModifiers());
169     System.out.println(String.class.getModifiers());
170 
171     System.out.println(String.class.isAssignableFrom(Object.class));
172     System.out.println(Object.class.isAssignableFrom(String.class));
173 
174     System.out.println(String.class.isInstance("hello"));
175     System.out.println(String.class.isInstance(123));
176 
177     Method m;
178 
179     m = Main.class.getDeclaredMethod("IV", int.class);
180     System.out.println(Arrays.toString(m.getParameterTypes()));
181     show(m.invoke(null, 4444));
182     System.out.println(Arrays.toString(m.getParameterTypes()));
183 
184     m = Main.class.getDeclaredMethod("IIV", int.class, int.class);
185     System.out.println(Arrays.toString(m.getParameterTypes()));
186     show(m.invoke(null, 1111, 2222));
187 
188     m = Main.class.getDeclaredMethod("III", int.class, int.class);
189     System.out.println(Arrays.toString(m.getParameterTypes()));
190     show(m.invoke(null, 1111, 2222));
191 
192     m = Main.class.getDeclaredMethod("sumArray", int[].class);
193     System.out.println(Arrays.toString(m.getParameterTypes()));
194     show(m.invoke(null, new int[] { 1, 2, 3, 4 }));
195 
196     m = Main.class.getDeclaredMethod("concat", String[].class);
197     System.out.println(Arrays.toString(m.getParameterTypes()));
198     show(m.invoke(null, (Object) new String[] { "h", "e", "l", "l", "o" }));
199 
200     m = Main.class.getDeclaredMethod("ZBCDFIJSV", boolean.class, byte.class, char.class, double.class, float.class, int.class, long.class, short.class);
201     System.out.println(Arrays.toString(m.getParameterTypes()));
202     show(m.invoke(null, true, (byte) 0, '1', 2, 3, 4, 5, (short) 6));
203 
204     m = Main.class.getDeclaredMethod("ZBCDLFIJSV", boolean.class, byte.class, char.class, double.class, String.class, float.class, int.class, long.class, short.class);
205     System.out.println(Arrays.toString(m.getParameterTypes()));
206     show(m.invoke(null, true, (byte) 0, '1', 2, "hello world", 3, 4, 5, (short) 6));
207 
208     try {
209       m = Main.class.getDeclaredMethod("thrower");
210       System.out.println(Arrays.toString(m.getParameterTypes()));
211       show(m.invoke(null));
212       System.out.println("************* should have thrown!");
213     } catch (InvocationTargetException expected) {
214       System.out.println("got expected InvocationTargetException");
215     }
216   }
217 
thrower()218   private static void thrower() {
219     throw new ArithmeticException("surprise!");
220   }
221 
sumArray(int[] xs)222   private static int sumArray(int[] xs) {
223     int result = 0;
224     for (int x : xs) {
225       result += x;
226     }
227     return result;
228   }
229 
concat(String[] strings)230   private static String concat(String[] strings) {
231     String result = "";
232     for (String s : strings) {
233       result += s;
234     }
235     return result;
236   }
237 
IV(int i)238   private static void IV(int i) {
239     System.out.println(i);
240   }
241 
IIV(int i, int j)242   private static void IIV(int i, int j) {
243     System.out.println(i + " " + j);
244   }
245 
III(int i, int j)246   private static int III(int i, int j) {
247     System.out.println(i + " " + j);
248     return i + j;
249   }
250 
ZBCDFIJSV(boolean z, byte b, char c, double d, float f, int i, long l, short s)251   private static void ZBCDFIJSV(boolean z, byte b, char c, double d, float f, int i, long l, short s) {
252     System.out.println(z + " " + b + " " + c + " " + d + " " + f + " " + i + " " + l + " " + s);
253   }
254 
ZBCDLFIJSV(boolean z, byte b, char c, double d, String string, float f, int i, long l, short s)255   private static void ZBCDLFIJSV(boolean z, byte b, char c, double d, String string, float f, int i, long l, short s) {
256     System.out.println(z + " " + b + " " + c + " " + d + " " + " " + string + " " + f + " " + i + " " + l + " " + s);
257   }
258 
testConstructorReflection()259   public static void testConstructorReflection() throws Exception {
260     Constructor<?> ctor;
261 
262     ctor = String.class.getConstructor(new Class[0]);
263     show(ctor.newInstance((Object[]) null));
264 
265     ctor = String.class.getConstructor(char[].class, int.class, int.class);
266     show(ctor.newInstance(new char[] { 'x', 'y', 'z', '!' }, 1, 2));
267   }
268 
main(String[] args)269   public static void main(String[] args) throws Exception {
270     testFieldReflection();
271     testMethodReflection();
272     testConstructorReflection();
273   }
274 }
275