JAVA 獲取/設置bean的某個屬性值


public class test{
    // 設置bean的某個屬性值
    public static void setProperty(MaterialPriceLineVO materialPriceLineVO, String fieldName, String value) throws Exception {
        // 獲取bean的某個屬性的描述符
        PropertyDescriptor propDesc = new PropertyDescriptor(fieldName, MaterialPriceLineVO.class);
        // 獲得用於寫入屬性值的方法
        Method methodSetUserName = propDesc.getWriteMethod();
        // 寫入屬性值
        methodSetUserName.invoke(materialPriceLineVO, value);
    }

    // 獲取bean的某個屬性值
    public static String getStringProperty(MaterialPriceLineVO materialPriceLineVO, String fieldName) throws Exception {
        // 獲取Bean的某個屬性的描述符
        PropertyDescriptor proDescriptor = new PropertyDescriptor(fieldName, MaterialPriceLineVO.class);
        // 獲得用於讀取屬性值的方法
        Method methodGet = proDescriptor.getReadMethod();
        // 讀取屬性值
        Object objValue = methodGet.invoke(materialPriceLineVO);
        if (ObjectUtils.isEmpty(objValue) || "null".equals(objValue)){
            return null;
        }else {
            return objValue.toString();
        }
    }

    public static String getIntegerProperty(MaterialPriceLineVO materialPriceLineVO, String fieldName) throws Exception {
        // 獲取Bean的某個屬性的描述符
        PropertyDescriptor proDescriptor = new PropertyDescriptor(fieldName, MaterialPriceLineVO.class);
        // 獲得用於讀取屬性值的方法
        Method methodGet = proDescriptor.getReadMethod();
        // 讀取屬性值
        Object objValue = methodGet.invoke(materialPriceLineVO);
        if (ObjectUtils.isEmpty(objValue) || "null".equals(objValue)){
            return null;
        }else {
            return objValue.toString();
        }
    }

    public static String getBigDecimalProperty(MaterialPriceLineVO materialPriceLineVO, String fieldName) throws Exception {
        // 獲取Bean的某個屬性的描述符
        PropertyDescriptor proDescriptor = new PropertyDescriptor(fieldName, MaterialPriceLineVO.class);
        // 獲得用於讀取屬性值的方法
        Method methodGet = proDescriptor.getReadMethod();
        // 讀取屬性值
        Object objValue = methodGet.invoke(materialPriceLineVO);
        if (ObjectUtils.isEmpty(objValue) || "null".equals(objValue)){
            return null;
        }else {
            return objValue.toString();
        }
    }
}

 


免責聲明!

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



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