【java】反射設值取值、調用方法


 

1、設值

/**
  * 根據屬性名設置屬性值
  *
  * @param fieldName
  * @param object
  * @return
  */
 public boolean setFieldValueByFieldName(String fieldName, Object object,String value) {
     try {
        // 獲取obj類的字節文件對象
        Class c = object.getClass();
        // 獲取該類的成員變量
        Field f = c.getDeclaredField(fieldName);
        // 取消語言訪問檢查
        f.setAccessible(true);
        // 給變量賦值
        f.set(object, value);
        return true;
     } catch (Exception e) {
         log.error("反射取值異常",e);
         return false;
     }
 }

 

2、取值

/**
 * 根據屬性名獲取屬性值
 *
 * @param fieldName
 * @param object
 * @return
 */
public Object getFieldValueByFieldName(String fieldName, Object object) {
    try {
        Field field = object.getClass().getDeclaredField(fieldName);
        //設置對象的訪問權限,保證對private的屬性的訪問
        field.setAccessible(true);
        return  field.get(object);
    } catch (Exception e) {
        log.error("反射設值異常",e);
        return null;
    }
}

 

3、反射調用無參方法

public void execution(String className, String methodName) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
    Class<?> aClass = Class.forName(className);
    Object obj = aClass.newInstance();
    Method method = aClass.getMethod(methodName);
    method.invoke(obj);
}

 

4、反射調用有參方法

/**
 * 這里只列舉只有一個參數的情況,多個類似
 * 
 * @param className
 * @param methodName
 * @param parameter 參數
 * @throws ClassNotFoundException
 * @throws IllegalAccessException
 * @throws InstantiationException
 * @throws NoSuchMethodException
 * @throws InvocationTargetException
 */
public void execution(String className, String methodName, Object parameter) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
    Class<?> aClass = Class.forName(className);
    Object obj = aClass.newInstance();
    Method method = aClass.getMethod(methodName, parameter.getClass());
    method.invoke(obj, parameter);
}

 

5、反射調用方法(無參也適用)

/**
 * 反射調用方法
 *
 * @param className 全限定類名,例如:com.xiaostudy.test.Mytest1
 * @param methodName 方法名,例如:show
 * @param parameters 0或多個參數
 * @throws ClassNotFoundException
 * @throws IllegalAccessException
 * @throws InstantiationException
 * @throws NoSuchMethodException
 * @throws InvocationTargetException
 */
public static void execution(String className, String methodName, Object... parameters) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
    Class<?> aClass = Class.forName(className);
    Object obj = aClass.newInstance();
    Class[] classes = new Class[parameters.length];for (int i = 0, length = parameters.length; i < length; i++) {
        classes[i] = parameters[i].getClass();
    }
    Method method = aClass.getMethod(methodName, classes);
    method.invoke(obj, parameters);
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM