■CommonUtil.java
/**
*Reresh ServiceOutDto
* (String Type : Null -> Blank)
*
* @param bean
* @return Object
*/
public static Object refreshServOutDto(Object bean) {
if (bean != null) {
return refreshServOutDto(bean, StringUtils.EMPTY);
}
return bean;
}
/**
 *Reresh ServiceOutDto
 * 
 * @param bean
 * @param defaultStr
 * @return Object
 */
@SuppressWarnings({"rawtypes", "unchecked"})
public static Object refreshServOutDto(Object bean, String defaultStr) {
    if (bean != null) {
        Map bMap = new BeanMap(bean);
        HashMap hMap = new HashMap(bMap);
        hMap.remove("class");
        Iterator itr = hMap.keySet().iterator();
        while (itr.hasNext()) {
            String propNm = (String) itr.next();
            reSetter(bean, propNm, defaultStr);
        }
    }
    return bean;
}
/**
 *Convertir Null en vide
 * 
 * @param bean
 * @param propNm
 * @param defaultStr
 */
@SuppressWarnings("rawtypes")
public static <T> void reSetter(Object bean, String propNm, String defaultStr) {
    //Préparez un descripteur
    PropertyDescriptor nameProp;
    try {
        nameProp = new PropertyDescriptor(propNm, bean.getClass());
        //Acquisition de la méthode Getter
        Method nameGetter = nameProp.getReadMethod();
        Object nowVal = nameGetter.invoke(bean, (Object[]) null);
        //Obtenir la méthode du setter
        Method nameSetter = nameProp.getWriteMethod();
        String PropType = nameProp.getPropertyType().toString();
        if (PropType.endsWith("java.util.List")) {
            //Tableau
            if (nowVal == null) {
                nameSetter.invoke(bean, (List) new ArrayList());
            }
        } else if (PropType.endsWith("java.lang.String")) {
            //Chaîne
            String tmpStr = (String) nowVal;
            tmpStr = StringUtils.defaultIfEmpty(((String) nowVal), defaultStr);
            // "null"/"NULL"/"Null"Si, convertir
            if (Objects.equal(tmpStr.toLowerCase(), "null")) {
                tmpStr = defaultStr;
            }
            nameSetter.invoke(bean, tmpStr);
        } else if (PropType.endsWith("int")) {
            // int
        } else if (PropType.endsWith("java.lang.Number")) {
            // Number
        } else if (PropType.endsWith("boolean")) {
            // boolean
        } else if (PropType.endsWith("java.util.Date")) {
            // Date
        } else if (PropType.matches(".*com\\.smbc_card\\.credit\\.compass\\.service\\.dto\\..+")) {
            // Java Bean Class (ex.Adresse etc.)
            if (nowVal == null) {
                nameSetter.invoke(    bean,
                                    refreshServOutDto(getClassForName(
                                                                        PropType.replaceFirst(    "class ",
                                                                                                StringUtils.EMPTY))
                                        .newInstance()));
            }
        }
    } catch (IntrospectionException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
        | InstantiationException | ClassNotFoundException e) {
        System.out.println("Informations anormales:" + e);
    }
}
/**
 * getClassForName
 * 
 * @param className
 * @return Class<T>
 * @throws ClassNotFoundException
 */
@SuppressWarnings("unchecked")
public static <T> Class<T> getClassForName(String className) throws ClassNotFoundException {
    return (Class<T>) Class.forName(className);
}
        Recommended Posts