public class ReflectUtils {
private static ReflectUtils sInstance;
private ReflectUtils() {
}
public static ReflectUtils getInstance() {
if (sInstance == null) {
sInstance = new ReflectUtils();
}
return sInstance;
}
public Method getMethod(Class<?> clazz, String methodName, Class<?> ...values) {
Method method = null;
try {
if(values != null) {
method = clazz.getDeclaredMethod(methodName, values);
}else {
method = clazz.getDeclaredMethod(methodName);
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return method;
}
public Object invokeStaticMethod(Method method, Object ...values) {
return invokeMethod(method, null, values);
}
public Object invokeMethod(Method method, Object classValue, Object ...values) {
if(method != null) {
try {
return method.invoke(classValue, values);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 这里我们通过反射静态方法 getUnsafe()方法来获取Unsafe对象,其实在之前的代码
* 的时候我们也可以直接反射静态的成员变量来直接获取Unsafe对象的,其实都是差不多的。
*/
public Object getUnsafe(Class<?> clazz) {
Method method = getMethod(clazz, "getUnsafe");
return invokeStaticMethod(method);
}
}
public class UnsafeProxy {
private static ReflectUtils sUtils;
private static Class sUnsafeClass;
private static Object sUnsafe;
static {
try {
sUtils = ReflectUtils.getInstance();
sUnsafeClass = Class.forName("sun.misc.Unsafe");
sUnsafe = sUtils.getUnsafe(sUnsafeClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Object allocateInstance(Class<?> params) {
Method method = sUtils.getMethod(sUnsafeClass, "allocateInstance", Class.class);
return sUtils.invokeMethod(method, sUnsafe, params);
}
public static long objectFieldOffset(Field field) {
Method method = sUtils.getMethod(sUnsafeClass, "objectFieldOffset", Field.class);
Object obj = sUtils.invokeMethod(method, sUnsafe, field);
return obj == null ? 0 : (Long) obj;
}
public static long putLong(Object object, long offset, long newValue) {
Method method = sUtils.getMethod(sUnsafeClass, "putLong", Object.class, long.class, long.class);
Object obj = sUtils.invokeMethod(method, sUnsafe, object, offset, newValue);
return obj == null ? 0 : (Long) obj;
}
public static long putInt(Object object, long offset, int newValue) {
Method method = sUtils.getMethod(sUnsafeClass, "putInt", Object.class, long.class, int.class);
Object obj = sUtils.invokeMethod(method, sUnsafe, object, offset, newValue);
return obj == null ? 0 : (Long) obj;
}
public static long putObject(Object object, long offset, Object newValue) {
Method method = sUtils.getMethod(sUnsafeClass, "putObject", Object.class, long.class, Object.class);
Object obj = sUtils.invokeMethod(method, sUnsafe, object, offset, newValue);
return obj == null ? 0 : (Long) obj;
}
public static int arrayIndexScale(Class clazz) {
Method method = sUtils.getMethod(sUnsafeClass, "arrayIndexScale", Class.class);
Object obj = sUtils.invokeMethod(method, sUnsafe, clazz);
return obj == null ? 0 : (Integer) obj;
}
public static int arrayBaseOffset(Class clazz) {
Method method = sUtils.getMethod(sUnsafeClass, "arrayBaseOffset", Class.class);
Object obj = sUtils.invokeMethod(method, sUnsafe, clazz);
return obj == null ? 0 : (Integer) obj;
}
public static boolean compareAndSwapInt(Object object, long offset, int expectedValue, int newValue) {
Method method = sUtils.getMethod(sUnsafeClass, "compareAndSwapInt", Object.class, long.class, int.class, int.class);
Object obj = sUtils.invokeMethod(method, sUnsafe, object, offset, expectedValue, newValue);
Log.d("Test", "" + (obj == null) + "method:" + (method) + "sUnsafe:" + sUnsafe);
return obj == null ? false : (Boolean) obj;
}
}