import android.content.SharedPreferences;
import android.text.TextUtils;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* @author Afra55
* @date 2018/4/2
* A smile is the best business card.
* <p>
* 反射工具类
* 通过反射获得对应函数功能
*/
public class ReflectionUtil {
/**
* 通过类对象,运行指定方法
*
* @param obj 类对象
* @param methodName 方法名
* @param params 参数值
* @return 失败返回null
*/
public static Object invokeMethod(Object obj, String methodName, Object[] params) {
if (obj == null || TextUtils.isEmpty(methodName)) {
return null;
}
Class<?> clazz = obj.getClass();
try {
Class<?>[] paramTypes = null;
if (params != null) {
paramTypes = new Class[params.length];
for (int i = 0; i < params.length; ++i) {
paramTypes[i] = params[i].getClass();
}
}
Method method = clazz.getMethod(methodName, paramTypes);
method.setAccessible(true);
return method.invoke(obj, params);
} catch (NoSuchMethodException e) {
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 通过类对象,运行指定方法
*
* @param obj 类对象
* @param methodName 方法名
* @param params 参数值
* @return 失败返回null
*/
public static Object invokeDeclaredMethod(Object obj, String methodName, Object[] params) {
if (obj == null || TextUtils.isEmpty(methodName)) {
return null;
}
Class<?> clazz = obj.getClass();
try {
Class<?>[] paramTypes = null;
if (params != null) {
paramTypes = new Class[params.length];
for (int i = 0; i < params.length; ++i) {
paramTypes[i] = params[i].getClass();
}
}
Method method = clazz.getDeclaredMethod(methodName, paramTypes);
method.setAccessible(true);
return method.invoke(obj, params);
} catch (NoSuchMethodException e) {
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static Object getFieldValue(Object obj, String fieldName) {
if (obj == null || TextUtils.isEmpty(fieldName)) {
return null;
}
Class<?> clazz = obj.getClass();
while (clazz != Object.class) {
try {
Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
return field.get(obj);
} catch (Exception e) {
}
clazz = clazz.getSuperclass();
}
return null;
}
public static void setFieldValue(Object obj, String fieldName, Object value) {
if (obj == null || TextUtils.isEmpty(fieldName)) {
return;
}
Class<?> clazz = obj.getClass();
while (clazz != Object.class) {
try {
Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
field.set(obj, value);
return;
} catch (Exception e) {
}
clazz = clazz.getSuperclass();
}
}
/**
* 通过类全路径,运行指定静态方法
*
* @param obj 类全路径
* @param methodName 方法名
* @param params 参数值
* @return 失败返回null
*/
public static Object invokeStaticMethod(String obj, String methodName, Object[] params, Class<?>... parameterTypes) {
if (obj == null || TextUtils.isEmpty(methodName)) {
return null;
}
try {
Class<?> clazz = Class.forName(obj);
Method method = clazz.getMethod(methodName, parameterTypes);
method.setAccessible(true);
return method.invoke(null, params);
} catch (NoSuchMethodException e) {
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static SharedPreferences invokeSp(String name) {
try {
return (SharedPreferences) ReflectionUtil.invokeStaticMethod("com.sp.cons.SpConsForKernel", "getSp", new Object[]{name}, String.class);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
Android反射工具类
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- mybatis提供了一个底层的反射工具类。我们在业务代码中也可以使用。 1. 相关API的使用 MeatObjec...
- 在Android开发中,我们会用到许多相同的方法,我们经常将这些方法归纳总结到一起,做成一个工具类。SUtils就...
- DevUtils Android开发工具类,常用的文件操作,bitmap操作,数据库操作,Toast显示,Dial...