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
|
package com.n1ght;
import java.lang.reflect.AccessibleObject; import java.lang.reflect.Array; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.util.HashMap; import sun.reflect.ReflectionFactory;
public class ReflectTools { public ReflectTools() { }
public static void setAccessible(AccessibleObject member) throws Exception { member.setAccessible(true); }
public static Field getField(Class<?> clazz, String fieldName) { Field field = null;
try { field = clazz.getDeclaredField(fieldName); setAccessible(field); } catch (Exception var4) { if (clazz.getSuperclass() != null) { field = getField(clazz.getSuperclass(), fieldName); } }
return field; }
public static void setFieldValue(Object obj, String fieldName, Object value) throws Exception { Field field = getField(obj.getClass(), fieldName); field.set(obj, value); }
public static Object getFieldValue(Object obj, String fieldName) throws Exception { Field field = getField(obj.getClass(), fieldName); return field.get(obj); }
public static Constructor<?> getFirstCtor(String name) throws Exception { Constructor<?> ctor = Class.forName(name).getDeclaredConstructors()[0]; setAccessible(ctor); return ctor; }
public static Object newInstance(String className, Object... args) throws Exception { return getFirstCtor(className).newInstance(args); }
public static <T> T createWithoutConstructor(Class<T> classToInstantiate) throws Exception { return createWithConstructor(classToInstantiate, Object.class, new Class[0], new Object[0]); }
public static <T> T createWithConstructor(Class<T> classToInstantiate, Class<? super T> constructorClass, Class<?>[] consArgTypes, Object[] consArgs) throws Exception { Constructor<? super T> objCons = constructorClass.getDeclaredConstructor(consArgTypes); setAccessible(objCons); Constructor<?> sc = ReflectionFactory.getReflectionFactory().newConstructorForSerialization(classToInstantiate, objCons); setAccessible(sc); return (T) sc.newInstance(consArgs); }
public static HashMap<Object, Object> makeMap(Object v1, Object v2) throws Exception { HashMap<Object, Object> s = new HashMap(); setFieldValue(s, "size", 2);
Class nodeC; try { nodeC = Class.forName("java.util.HashMap$Node"); } catch (ClassNotFoundException var6) { nodeC = Class.forName("java.util.HashMap$EntAry"); }
Constructor<?> nodeCons = nodeC.getDeclaredConstructor(Integer.TYPE, Object.class, Object.class, nodeC); nodeCons.setAccessible(true); Object tbl = Array.newInstance(nodeC, 2); Array.set(tbl, 0, nodeCons.newInstance(0, v1, v1, null)); Array.set(tbl, 1, nodeCons.newInstance(0, v2, v2, null)); setFieldValue(s, "table", tbl); return s; }
public static Method getDeclaredMethod(Class clazz, String methodName, Class[] params) { Method method = null;
while(clazz != null) { try { method = clazz.getDeclaredMethod(methodName, params); method.setAccessible(true); break; } catch (NoSuchMethodException var5) { clazz = clazz.getSuperclass(); } }
return method; }
public static Method getMethod(Class clazz, String methodName, Class[] params) { Method method = null;
while(clazz != null) { try { method = clazz.getMethod(methodName, params); break; } catch (NoSuchMethodException var5) { clazz = clazz.getSuperclass(); } }
return method; } }
|