1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
package 面试题;
import java.lang.reflect.*; import java.util.Arrays; import java.util.List;
/** * Created by piqiu on 3/3/16. */ public class ReflectionDemo {
private static Class<User> clz = User.class; private static Class<OOXX> ooxxClass = OOXX.class;
private ReflectionDemo(){}
public static void main(String[] args) throws Exception { // getConstructors(); // getConstructor(); // getDeclaredConstructors(); // getDeclaredConstructor(); // invokeConstructor(); // getAllMethods(); // getOneMethod(); // invokeMethod(); // invokeStaticMethod(); // invokeVariableLengthMethod(); // getAllFields(); // invokeField(); // getFieldValue(); // getOtherAPI(); // invokeGenericMethod(); getGenericFieldType(); }
/** * 获取所有的【public修饰】的构造器 */ private static void getConstructors() { Constructor<?>[] constructors = clz.getConstructors(); for (Constructor c : constructors) { System.out.println(c); System.out.println(Arrays.toString(c.getParameterTypes())); System.out.println(c.getDeclaringClass()); } /** public 面试题.User(java.lang.String,int,int) [class java.lang.String, int, int] class 面试题.User public 面试题.User() [] class 面试题.User */ }
/** * 根据构造器参数的Class类型获得对应的Constructor,只能获取【public修饰】的 * @throws NoSuchMethodException */ private static void getConstructor() throws NoSuchMethodException { Constructor<User> constructor1 = clz.getConstructor(); System.out.println(constructor1); // public 面试题.User() Constructor<User> constructor3 = clz.getConstructor(String.class, int.class, int.class); System.out.println(constructor3); // public 面试题.User(java.lang.String,int,int) }
/** * 获取所有构造器,和访问权限无关 */ private static void getDeclaredConstructors() { Constructor<?>[] declaredConstructors = clz.getDeclaredConstructors(); for (Constructor c : declaredConstructors) { System.out.println(c); System.out.println(Arrays.toString(c.getParameterTypes())); System.out.println(c.getDeclaringClass()); } /** public 面试题.User(java.lang.String,int,int) [class java.lang.String, int, int] class 面试题.User public 面试题.User(java.lang.String) [class java.lang.String] class 面试题.User private 面试题.User() [] class 面试题.User */ }
/** * 获取指定参数类型的构造器,和访问权限无关 * @throws NoSuchMethodException */ private static void getDeclaredConstructor() throws NoSuchMethodException { Constructor<User> declaredConstructor = clz.getDeclaredConstructor(String.class); System.out.println(declaredConstructor); // private 面试题.User(java.lang.String) }
private static void invokeConstructor() throws Exception { // 这种newInstance方法只能作用于 public 的 无参构造方法,局限性太大 User user1 = clz.newInstance(); System.out.println(user1);
Constructor<User> constructor1 = clz.getDeclaredConstructor(); User user2 = constructor1.newInstance(); System.out.println(user2);
Constructor<User> constructor2 = clz.getDeclaredConstructor(String.class); constructor2.setAccessible(true); // 不加这句就实例化不了私有(private)的构造器 User user3 = constructor2.newInstance("呵呵"); System.out.println(user3);
Constructor<User> constructor3 = clz.getDeclaredConstructor(String.class, int.class, int.class); User user4 = constructor3.newInstance("哈哈", 22, 2); System.out.println(user4);
/** public User() User -> name: 无名氏 age: 无年龄 sex: 无性别 public User() User -> name: 无名氏 age: 无年龄 sex: 无性别 private User(String name): 呵呵 User -> name: 呵呵 age: 无年龄 sex: 无性别 public User(String name, int age, int sex): 哈哈 22 2 User -> name: 哈哈 age: 22 sex: 女 */ }
/** * getDeclaredMethods和getMethods区别 * getMethods获得包括自身和继承过来的所有public方法 * getDeclaredMathods获取自身所有的方法,不包括继承的,和访问权限无关 */ private static void getAllMethods() { Method[] methods = clz.getDeclaredMethods(); for (Method m : methods) { System.out.println(m); } /** public java.lang.String 面试题.User.toString() public java.lang.String 面试题.User.getName() public void 面试题.User.setName(java.lang.String) public int 面试题.User.getAge() public void 面试题.User.setAge(int) public int 面试题.User.getSex() public void 面试题.User.setSex(int) private void 面试题.User.sayHello() public void 面试题.User.sayHi(java.lang.String) */ }
/** * 只有通过方法签名才能找到唯一的方法 * 方法名 + 参数列表 * 得到私有方法必须使用 getDeclaredMethod */ private static void getOneMethod() throws NoSuchMethodException { Method method1 = clz.getMethod("sayHi", String.class); System.out.println(method1); // public void 面试题.User.sayHi(java.lang.String)
Method method2 = clz.getDeclaredMethod("sayHello"); System.out.println(method2); // private void 面试题.User.sayHello() }
private static void invokeMethod() throws NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException { Method method = clz.getMethod("getMoney", String.class); double money = (Double)method.invoke(clz.newInstance(), "benjamin"); System.out.println(money); /** public User() get money 200.12 for benjamin 200.12 */ }
/** * 调用静态方法 * 如果方法是静态的,那么可以忽略指定的obj参数,将obj参数设为null即可 */ private static void invokeStaticMethod() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { Method method = clz.getMethod("staticMethod"); method.invoke(null); // invoke static method... }
/** * 调用可变参数的静态方法 */ private static void invokeVariableLengthMethod() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { Method method = clz.getMethod("show", int[].class); int result = (Integer)method.invoke(null, new int[]{2, 4}); System.out.println(result); // 6
Method method2 = clz.getMethod("show2", String[].class); // 下面这么写会报Exception in thread "main" java.lang.IllegalArgumentException: wrong number of arguments // 基本类型和引用类型不同,引用类型会自动解包 // 对于数组类型的引用类型的参数,底层会自动进行解包,为了解决该问题,我们使用Object的一维数组把实际参数包装起来 // method2.invoke(null, new String[]{"A", "B", "C"});
method2.invoke(null, new Object[]{new String[]{"A", "B", "C"}}); // [A, B, C] }
/** * 获取所有字段,包括私有的 * 如果想只获取public的字段以及父类的public字段,使用getFields()方法 */ private static void getAllFields() { Field[] declaredFields = clz.getDeclaredFields(); for (Field field : declaredFields) { System.out.println(field); } /** private java.lang.String 面试题.User.name private int 面试题.User.age private int 面试题.User.sex */ }
/** * 设置字段的值 */ private static void invokeField() throws NoSuchFieldException, IllegalAccessException, InstantiationException { Field ageField = clz.getDeclaredField("age"); ageField.setAccessible(true); Object obj = clz.newInstance(); ageField.set(obj, 12); System.out.println(obj); /** public User() User -> name: 无名氏 age: 12 sex: 无性别 */ }
/** * 获取字段值 */ private static void getFieldValue() throws NoSuchFieldException, IllegalAccessException, InstantiationException { Field ageField = clz.getDeclaredField("age"); ageField.setAccessible(true); Object obj = clz.newInstance(); Object age = ageField.get(obj); System.out.println(age); /** public User() 0 */ }
private static void getOtherAPI() { int modifiers = clz.getModifiers(); System.out.println(modifiers); // 1 System.out.println(Modifier.toString(modifiers)); // public
System.out.println(clz.getName()); // 面试题.User System.out.println(clz.getSimpleName()); // User System.out.println(clz.getPackage()); // package 面试题 System.out.println(clz.getSuperclass()); // class java.lang.Object System.out.println(clz.isArray()); // false System.out.println(clz.isEnum()); // false }
/** * 调用泛型参数的方法 */ private static void invokeGenericMethod() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { Class clz = Arrays.class; Method asListMethod = clz.getDeclaredMethod("asList", Object[].class); List result = (List) asListMethod.invoke(null, new Object[]{new Object[]{"A", "B", "C"}}); System.out.println(result); // [A, B, C] }
/** * 得到泛型字段的类型 */ private static void getGenericFieldType() throws NoSuchFieldException { Field cacheField = ooxxClass.getDeclaredField("cache"); Type genericType = cacheField.getGenericType(); System.out.println(genericType); // java.util.Map<java.lang.String, java.lang.Object> ParameterizedType parameterizedType = (ParameterizedType) genericType; Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); System.out.println(Arrays.toString(actualTypeArguments)); // [class java.lang.String, class java.lang.Object] } } |