您的位置:首页 > 其它

反射工具类

2016-07-26 00:00 387 查看
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* 反射工具类
*
*/
public final class ReflectionUtil {

private static final Logger LOGGER = LoggerFactory.getLogger(ReflectionUtil.class);

/**
* 创建实例
*/
public static Object newInstance(Class<?> cls) {
Object instance;
try {
instance = cls.newInstance();
} catch (Exception e) {
LOGGER.error("new instance failure", e);
throw new RuntimeException(e);
}
return instance;
}

/**
* 创建实例(根据类名)
*/
public static Object newInstance(String className) {
Class<?> cls = ClassUtil.loadClass(className);
return newInstance(cls);
}

/**
* 调用方法
*/
public static Object invokeMethod(Object obj, Method method, Object... args) {
Object result;
try {
method.setAccessible(true);
result = method.invoke(obj, args);
} catch (Exception e) {
LOGGER.error("invoke method failure", e);
throw new RuntimeException(e);
}
return result;
}

/**
* 设置成员变量的值
*/
public static void setField(Object obj, Field field, Object value) {
try {
field.setAccessible(true);
field.set(obj, value);
} catch (Exception e) {
LOGGER.error("set field failure", e);
throw new RuntimeException(e);
}
}
}


public static Object getField(Object obj, Field field) {
Object value = null;
try {
field.setAccessible(true);
value = field.get(obj);
} catch (Exception e) {
LOGGER.error("set field failure", e);
throw new RuntimeException(e);
}
return value;
}
public static Object getField(Object obj, String fieldName) {
Object value = null;
try {
Field[] fields = obj.getClass().getDeclaredFields();
for (int i = 0;i< fields.length;i++ ){
Field field = fields[i];
String fieldNameTmp = field.getName();
if(fieldName.equals(fieldNameTmp)){
return getField(obj, field);
}
}
} catch (Exception e) {
LOGGER.error("get field by field name failure", e);
throw new RuntimeException(e);
}
return value;
}

public static Object getFieldValue(Object obj,Field field,Class<?> clazz){
Object val = null;
try {
PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz);
Method rM = pd.getReadMethod();
val = rM.invoke(obj);
} catch (Exception e) {
LOGGER.error("invoke get method failure",e);
}
return val;
}

public static Object getFieldValue(Object obj,String fieldName,Class<?> clazz){
Object val = null;
try {
PropertyDescriptor pd = new PropertyDescriptor(fieldName, clazz);
Method rM = pd.getReadMethod();
val = rM.invoke(obj);
} catch (Exception e) {
LOGGER.error("invoke get method failure",e);
}
return val;
}

public static void setFieldValue(Object obj,String fieldName,Object value){
try {
PropertyDescriptor pd = new PropertyDescriptor(fieldName, obj.getClass());
Method rM = pd.getWriteMethod();
rM.invoke(obj, value);
} catch (Exception e) {
LOGGER.error("invoke get method failure",e);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: