反射工具类

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);
       }



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值