反射工具类
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); }
相关文章推荐
- Introspector(内省)简单示例 与 简单应用
- 易学设计模式看书笔记(1) - 设计模式的分类
- 给你一组字符串 如 {5,2,3,2,4,5,1,2,1,5},让你输出里面出现次数最多且数值最大的
- Java内存区域与内存溢出异常与Eclipse运行速度调优
- 设计模式(8) - 迭代器模式(iterator)- 实现ArrayList和linkedList的迭代器
- 排序-Perl
- Idea使用技巧
- Adapter初步学习
- Javascript 设计模式学习之一 Constructor(构造器)模式
- 剑指offer(Java)-复杂链表的复制
- 剑指Offer面试题-二叉搜索树与双向链表
- JS正则表达式大全(整理详细且实用)
- solr中文分词
- solr伪集群搭建
- mybatis的配置文件之 mybatis-config.xml
- mysql之driver、url
- oracle之driver、url
- *-mapper.xml配置文件
- ehcache.xml配置
- 安装nginx