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 } catch (IllegalArgumentException expected) { 123 expected.printStackTrace(); 124 } 125 126 f = Main.class.getDeclaredField("z"); 127 show(f.get(null)); 128 f = Main.class.getDeclaredField("b"); 129 show(f.get(null)); 130 f = Main.class.getDeclaredField("c"); 131 show(f.get(null)); 132 f = Main.class.getDeclaredField("d"); 133 show(f.get(null)); 134 f = Main.class.getDeclaredField("f"); 135 show(f.get(null)); 136 f = Main.class.getDeclaredField("i"); 137 show(f.get(null)); 138 f = Main.class.getDeclaredField("j"); 139 show(f.get(null)); 140 f = Main.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 show(Object o)155 private static void show(Object o) { 156 System.out.println(o + " (" + (o != null ? o.getClass() : "null") + ")"); 157 } 158 testMethodReflection()159 public static void testMethodReflection() throws Exception { 160 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(Main.class.getInterfaces())); 165 System.out.println(Arrays.toString(String.class.getInterfaces())); 166 167 System.out.println(Main.class.getModifiers()); 168 System.out.println(String.class.getModifiers()); 169 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 176 Method m; 177 178 m = Main.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 = Main.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 = Main.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 = Main.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 = Main.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 = Main.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 = Main.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 = Main.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 expected) { 213 expected.printStackTrace(); 214 } 215 } 216 thrower()217 private static void thrower() { 218 throw new ArithmeticException("surprise!"); 219 } 220 sumArray(int[] xs)221 private static int sumArray(int[] xs) { 222 int result = 0; 223 for (int x : xs) { 224 result += x; 225 } 226 return result; 227 } 228 concat(String[] strings)229 private static String concat(String[] strings) { 230 String result = ""; 231 for (String s : strings) { 232 result += s; 233 } 234 return result; 235 } 236 IV(int i)237 private static void IV(int i) { 238 System.out.println(i); 239 } 240 IIV(int i, int j)241 private static void IIV(int i, int j) { 242 System.out.println(i + " " + j); 243 } 244 III(int i, int j)245 private static int III(int i, int j) { 246 System.out.println(i + " " + j); 247 return i + j; 248 } 249 ZBCDFIJSV(boolean z, byte b, char c, double d, float f, int i, long l, short s)250 private static void ZBCDFIJSV(boolean z, byte b, char c, double d, float f, int i, long l, short s) { 251 System.out.println(z + " " + b + " " + c + " " + d + " " + f + " " + i + " " + l + " " + s); 252 } 253 ZBCDLFIJSV(boolean z, byte b, char c, double d, String string, float f, int i, long l, short s)254 private static void ZBCDLFIJSV(boolean z, byte b, char c, double d, String string, float f, int i, long l, short s) { 255 System.out.println(z + " " + b + " " + c + " " + d + " " + " " + string + " " + f + " " + i + " " + l + " " + s); 256 } 257 testConstructorReflection()258 public static void testConstructorReflection() throws Exception { 259 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 main(String[] args)268 public static void main(String[] args) throws Exception { 269 testFieldReflection(); 270 testMethodReflection(); 271 testConstructorReflection(); 272 } 273 } 274