â– 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;
}
/**
*Convert Null to blank
*
* @param bean
* @param propNm
* @param defaultStr
*/
@SuppressWarnings("rawtypes")
public static <T> void reSetter(Object bean, String propNm, String defaultStr) {
//Prepare a descriptor
PropertyDescriptor nameProp;
try {
nameProp = new PropertyDescriptor(propNm, bean.getClass());
//Getter method acquisition
Method nameGetter = nameProp.getReadMethod();
Object nowVal = nameGetter.invoke(bean, (Object[]) null);
//Get setter method
Method nameSetter = nameProp.getWriteMethod();
String PropType = nameProp.getPropertyType().toString();
if (PropType.endsWith("java.util.List")) {
//Array
if (nowVal == null) {
nameSetter.invoke(bean, (List) new ArrayList());
}
} else if (PropType.endsWith("java.lang.String")) {
//String
String tmpStr = (String) nowVal;
tmpStr = StringUtils.defaultIfEmpty(((String) nowVal), defaultStr);
// "null"/"NULL"/"Null"If, convert
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.Address 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("Abnormal information:" + 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