import android.util.Log;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class ReflectUtilities {
private static final String TAG = "ReflectUtilities";
public static final Map<String, Method> sMethods = new ConcurrentHashMap<>();
public static final Map<String, Constructor<?>> sConstructor = new ConcurrentHashMap<>();
public static final Map<String, Field> sField = new ConcurrentHashMap<>();
private ReflectUtilities() {
}
public static <T> T newInstance(Class<T> clazz, Class<?>[] types, Object... args) {
try {
Constructor<?> constructor = getConstructor(clazz, types);
return (T) constructor.newInstance(args);
} catch (Exception e) {
Log.e(TAG, "newInstance:" + e.getMessage());
}
return null;
}
public static <T> T newInstance(String clazz, Class<?>[] types, Object... args) {
try {
return (T) newInstance(Class.forName(clazz), types, args);
} catch (Exception e) {
Log.e(TAG, "newInstance:" + e.getMessage());
}
return null;
}
public static <T> T callObjectMethod(Object target, String method, Class<?>[] types, Object... args) {
try {
Method mt = getMethod(target.getClass(), method, types);
return (T) mt.invoke(target, args);
} catch (Exception e) {
Log.e(TAG, "callObjectMethod " + e.getMessage());
}
return null;
}
public static <T> T callStaticMethod(Class<?> clazz, String method, Class<?>[] types, Object... args) {
try {
Method mt = getMethod(clazz, method, types);
return (T) mt.invoke(null, args);
} catch (Exception e) {
Log.e(TAG, "callObjectMethod " + e.getMessage());
}
return null;
}
public static <T> T callStaticMethod(String clazz, String method, Class<?>[] types, Object... args) {
try {
return callStaticMethod(Class.forName(clazz), method, types, args);
} catch (Exception e) {
Log.e(TAG, "callObjectMethod " + e.getMessage());
}
return null;
}
public static <T> T getFieldValues(Object target,Class<?> clazz, String field) throws Exception {
Field f = getField(clazz, field);
return (T) f.get(target);
}
public static void setFieldValues(Object target, Class<?> clazz, String field, Object value) throws Exception {
Field f = getField(clazz, field);
f.set(target, value);
}
public static void setStaticFieldValues(Class<?> target, String field, Object value) throws Exception {
Field f = getField(target, field);
f.set(null, value);
}
public static void setStaticFieldValues(String target, String field, Object value) throws Exception {
setStaticFieldValues(Class.forName(target), field, value);
}
public static <T> T getStaticFieldValues(Class<?> target, String field) throws Exception {
Field f = getField(target, field);
return (T) f.get(null);
}
private static Constructor<?> getConstructor(Class<?> clazz, Class<?>[] types) throws Exception {
String key = generatedConstructorKey(clazz, types);
Constructor<?> constructor = sConstructor.get(key);
if (constructor == null) {
constructor = clazz.getDeclaredConstructor(types);
sConstructor.put(key, constructor);
Log.d(TAG, "getConstructor: cache " + key);
}
return constructor;
}
private static Method getMethod(Class<?> clazz, String method, Class<?>[] types) throws Exception {
Method mt;
String key = generatedMethodKey(clazz, method, types);
mt = sMethods.get(key);
if (mt == null) {
mt = clazz.getDeclaredMethod(method, types);
sMethods.put(key, mt);
Log.d(TAG, "getMethod: cache " + key);
}
return mt;
}
private static Field getField(Class<?> clazz, String field) throws Exception {
String key = generatedFieldKey(clazz, field);
Field f = sField.get(key);
if (f == null) {
f = clazz.getDeclaredField(field);
f.setAccessible(true);
sField.put(key, f);
Log.d(TAG, "getField: cache " + key);
}
return f;
}
private static String generatedFieldKey(Class<?> target, String field) {
return target.getName() + "/" + field;
}
private static String generatedMethodKey(Class<?> target, String method, Class<?>[] types) {
return target.getName() + "/" + method + "/" + Arrays.toString(types);
}
private static String generatedConstructorKey(Class<?> target, Class<?>[] types) {
return target.getName() + "/" + Arrays.toString(types);
}
public static void releaseCache() {
sMethods.clear();
sConstructor.clear();
sField.clear();
}
}
【Android】反射工具
最新推荐文章于 2023-04-19 23:39:58 发布