/** * 取得一个类,此操作会初始化类的static区域. * * @see Class#forName(String) */ private static Class<?> forName(String name) throws ReflectException { try { return Class.forName(name); } catch (Exception e) { throw new ReflectException(e); } }
Reflect.on(dm).set("mService", boxBinder.getProxyInterface()); } catch (ReflectException e) { e.printStackTrace();
Reflect.on(dm).set("mService", boxBinder.getProxyInterface()); } catch (ReflectException e) { e.printStackTrace();
private static Class<?> forName(String name, ClassLoader classLoader) throws ReflectException { try { return Class.forName(name, true, classLoader); } catch (Exception e) { throw new ReflectException(e); } }
Reflect.on(dm).set("mService", boxBinder.getProxyInterface()); } catch (ReflectException e) { e.printStackTrace();
/** * 取得指定名称的字段 * * @param name name * @return reflect * @throws ReflectException */ public Reflect field(String name) throws ReflectException { try { Field field = field0(name); return on(field.get(object)); } catch (Exception e) { throw new ReflectException(object.getClass().getName(), e); } }
private static Reflect on(Constructor<?> constructor, Object... args) throws ReflectException { try { return on(accessible(constructor).newInstance(args)); } catch (Exception e) { throw new ReflectException(e); } }
private Field field0(String name) throws ReflectException { Class<?> type = type(); // 先尝试取得公有字段 try { return type.getField(name); } // 此时尝试非公有字段 catch (NoSuchFieldException e) { do { try { return accessible(type.getDeclaredField(name)); } catch (NoSuchFieldException ignore) { } type = type.getSuperclass(); } while (type != null); throw new ReflectException(e); } }
private static Reflect on(Method method, Object object, Object... args) throws ReflectException { try { accessible(method); if (method.getReturnType() == void.class) { method.invoke(object, args); return on(object); } else { return on(method.invoke(object, args)); } } catch (Exception e) { throw new ReflectException(e); } }
/** * 设置指定字段为指定值 * * @param name * @param value * @return * @throws ReflectException */ public Reflect set(String name, Object value) throws ReflectException { try { Field field = field0(name); field.setAccessible(true); field.set(object, unwrap(value)); return this; } catch (Exception e) { throw new ReflectException(e); } }
throw new ReflectException("no method found for " + name, new NoSuchMethodException("No best method " + name + " with params " + Arrays.toString(types) + " could be found on type " + type() + "."));
/** * 创建一个实例根据传入的参数 * * @param args 参数 * @return Reflect * @throws ReflectException */ public Reflect create(Object... args) throws ReflectException { Class<?>[] types = types(args); try { Constructor<?> constructor = type().getDeclaredConstructor(types); return on(constructor, args); } catch (NoSuchMethodException e) { for (Constructor<?> constructor : type().getDeclaredConstructors()) { if (match(constructor.getParameterTypes(), types)) { return on(constructor, args); } } throw new ReflectException(e); } }
/** * 调用方法根据传入的参数 * * @param name * @param args * @return * @throws ReflectException */ public Reflect call(String name, Object... args) throws ReflectException { Class<?>[] types = types(args); try { Method method = exactMethod(name, types); return on(method, object, args); } catch (NoSuchMethodException e) { try { Method method = similarMethod(name, types); return on(method, object, args); } catch (NoSuchMethodException e1) { throw new ReflectException(e1); } } }
/** * 取得一个类,此操作会初始化类的static区域. * * @see Class#forName(String) */ private static Class<?> forName(String name) throws ReflectException { try { return Class.forName(name); } catch (Exception e) { throw new ReflectException(e); } }
private static Class<?> forName(String name, ClassLoader classLoader) throws ReflectException { try { return Class.forName(name, true, classLoader); } catch (Exception e) { throw new ReflectException(e); } }
private static Class<?> forName(String name, ClassLoader classLoader) throws ReflectException { try { return Class.forName(name, true, classLoader); } catch (Exception e) { throw new ReflectException(e); } }
/** * 取得一个类,此操作会初始化类的static区域. * * @see Class#forName(String) */ private static Class<?> forName(String name) throws ReflectException { try { return Class.forName(name); } catch (Exception e) { throw new ReflectException(e); } }
private static Reflect on(Constructor<?> constructor, Object... args) throws ReflectException { try { return on(accessible(constructor).newInstance(args)); } catch (Exception e) { throw new ReflectException(e); } }
private static Reflect on(Constructor<?> constructor, Object... args) throws ReflectException { try { return on(accessible(constructor).newInstance(args)); } catch (Exception e) { throw new ReflectException(e); } }
/** * 取得指定名称的字段 * * @param name name * @return reflect * @throws ReflectException */ public Reflect field(String name) throws ReflectException { try { Field field = field0(name); return on(field.get(object)); } catch (Exception e) { throw new ReflectException(object.getClass().getName(), e); } }