21 | * author: Blankj 22 | * blog : http://blankj.com 23 | * time : 2017/12/15 24 | * desc : utils about reflect 25 | *26 | */ 27 | public final class ReflectUtils { 28 | 29 | private final Class> type; 30 | 31 | private final Object object; 32 | 33 | private ReflectUtils(final Class> type) { 34 | this(type, type); 35 | } 36 | 37 | private ReflectUtils(final Class> type, Object object) { 38 | this.type = type; 39 | this.object = object; 40 | } 41 | 42 | /////////////////////////////////////////////////////////////////////////// 43 | // reflect 44 | /////////////////////////////////////////////////////////////////////////// 45 | 46 | /** 47 | * Reflect the class. 48 | * 49 | * @param className The name of class. 50 | * @return the single {@link ReflectUtils} instance 51 | * @throws ReflectException if reflect unsuccessfully 52 | */ 53 | public static ReflectUtils reflect(final String className) 54 | throws ReflectException { 55 | return reflect(forName(className)); 56 | } 57 | 58 | /** 59 | * Reflect the class. 60 | * 61 | * @param className The name of class. 62 | * @param classLoader The loader of class. 63 | * @return the single {@link ReflectUtils} instance 64 | * @throws ReflectException if reflect unsuccessfully 65 | */ 66 | public static ReflectUtils reflect(final String className, final ClassLoader classLoader) 67 | throws ReflectException { 68 | return reflect(forName(className, classLoader)); 69 | } 70 | 71 | /** 72 | * Reflect the class. 73 | * 74 | * @param clazz The class. 75 | * @return the single {@link ReflectUtils} instance 76 | * @throws ReflectException if reflect unsuccessfully 77 | */ 78 | public static ReflectUtils reflect(final Class> clazz) 79 | throws ReflectException { 80 | return new ReflectUtils(clazz); 81 | } 82 | 83 | /** 84 | * Reflect the class. 85 | * 86 | * @param object The object. 87 | * @return the single {@link ReflectUtils} instance 88 | * @throws ReflectException if reflect unsuccessfully 89 | */ 90 | public static ReflectUtils reflect(final Object object) 91 | throws ReflectException { 92 | return new ReflectUtils(object == null ? Object.class : object.getClass(), object); 93 | } 94 | 95 | private static Class> forName(String className) { 96 | try { 97 | return Class.forName(className); 98 | } catch (ClassNotFoundException e) { 99 | throw new ReflectException(e); 100 | } 101 | } 102 | 103 | private static Class> forName(String name, ClassLoader classLoader) { 104 | try { 105 | return Class.forName(name, true, classLoader); 106 | } catch (ClassNotFoundException e) { 107 | throw new ReflectException(e); 108 | } 109 | } 110 | 111 | /////////////////////////////////////////////////////////////////////////// 112 | // newInstance 113 | /////////////////////////////////////////////////////////////////////////// 114 | 115 | /** 116 | * Create and initialize a new instance. 117 | * 118 | * @return the single {@link ReflectUtils} instance 119 | */ 120 | public ReflectUtils newInstance() { 121 | return newInstance(new Object[0]); 122 | } 123 | 124 | /** 125 | * Create and initialize a new instance. 126 | * 127 | * @param args The args. 128 | * @return the single {@link ReflectUtils} instance 129 | */ 130 | public ReflectUtils newInstance(Object... args) { 131 | Class>[] types = getArgsType(args); 132 | try { 133 | Constructor> constructor = type().getDeclaredConstructor(types); 134 | return newInstance(constructor, args); 135 | } catch (NoSuchMethodException e) { 136 | List
P proxy(final Class
proxyType) {
435 | final boolean isMap = (object instanceof Map);
436 | final InvocationHandler handler = new InvocationHandler() {
437 | @Override
438 | @SuppressWarnings("null")
439 | public Object invoke(Object proxy, Method method, Object[] args) {
440 | String name = method.getName();
441 | try {
442 | return reflect(object).method(name, args).get();
443 | } catch (ReflectException e) {
444 | if (isMap) {
445 | Map