/**
* Parse an object to another object with the same name
*
* @param org ->
* Original Object
* @param tar ->
* Target Object Class
* @return
*/
public static Object transformSameClass(Object org, Class tarClz) {
if (org == null)
return null;
Object tar = null;
try {
tar = tarClz.newInstance();
// Class tarClz = tar.getClass();
Class orgClz = org.getClass();
Method[] tarMethods = tarClz.getMethods();
Method[] orgMethods = orgClz.getMethods();
LinkedList orgMethodsList = new LinkedList();
for (int i = 0; i < orgMethods.length; i++) {
String tmpMethodName = orgMethods[i].getName();
if (tmpMethodName.startsWith("get") && !tmpMethodName.startsWith("getClass")) {
orgMethodsList.add(orgMethods[i]);
}
}
Class[] parameterTypes = new Class[1];
Object[] arguments = new Object[1];
for (int i = 0; i < tarMethods.length; i++) {
String tarMethod = tarMethods[i].getName();
if (tarMethod.startsWith("get") && !tarMethod.startsWith("getClass")) {
for (int j = 0; j < orgMethodsList.size(); j++) {
Method orgMethod = (Method) orgMethodsList.get(j);
String orgMethodStr = orgMethod.getName();
if (tarMethod.equalsIgnoreCase(orgMethodStr)) {
orgMethodsList.remove(j); // remove from list, optimize next loop
Object field = orgMethod.invoke(org, null);
if (field != null) {
if (tarMethods[i].getReturnType().equals(orgMethod.getReturnType())) {
parameterTypes[0] = orgMethod.getReturnType();
arguments[0] = field;
// keep the same class type
} else if (tarMethods[i].getReturnType().equals(Long.class) && orgMethod.getReturnType().equals(Integer.class)) {
// parse integer to long
parameterTypes[0] = Long.class;
arguments[0] = new Long(Integer.parseInt(field.toString()));
} else if (tarMethods[i].getReturnType().equals(Integer.class) && orgMethod.getReturnType().equals(Long.class)) {
// parse long to Integer
parameterTypes[0] = Integer.class;
arguments[0] = new Integer(Integer.parseInt(field.toString()));
} else if (tarMethods[i].getReturnType().equals(boolean.class) && orgMethod.getReturnType().equals(Boolean.class)) {
// parse boolean
parameterTypes[0] = boolean.class;
arguments[0] = field;
} else if (tarMethods[i].getReturnType().equals(Boolean.class) && orgMethod.getReturnType().equals(boolean.class)) {
// parse Boolean
parameterTypes[0] = Boolean.class;
arguments[0] = field;
} else {
break;
}
Method med = tarClz.getMethod(StringUtil.replaceStringWithSet(tarMethod), parameterTypes);
med.invoke(tar, arguments);
}
break;
}
}
}
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
return tar;
}
/**
* Long fields (Number) passed from flex is null, but changed to '0' in Java. This method is to parse that fields
* back to Null
*
* @param obj
* Object that contains Long Type field
* @return Object
*/
public static Object paser0FieldsToNull(Object obj) {
if (obj == null)
return null;
Class clz = obj.getClass();
Object retObj = obj;
Method[] methods = clz.getMethods();
Class[] parameterTypes = new Class[1];
for (int i = 0; i < methods.length; i++) {
Class retType = methods[i].getReturnType();
if (retType.equals(Long.class) || retType.equals(Integer.class)) {
parameterTypes[0] = retType;
String name = methods[i].getName();
Object field;
try {
field = methods[i].invoke(obj, null);
if (field != null && !name.equals("getVersion")) { // version=0 is correct
Long val = new Long(field.toString());
if (val.equals(new Long(0))||val.equals(new Integer(0))) {
if (log.isDebugEnabled())
log.debug("==> This field value is 0, and will parse to Null: " + name);
// Method med = clz.getMethod(name.replace("get", "set"), parameterTypes);
Method med = clz.getMethod(StringUtil.replaceStringWithSet(name), parameterTypes);
Object[] arguments = new Object[1];
arguments[0] = null;
med.invoke(retObj, arguments);
}
}
} catch (Exception e) {
log.error(e.getMessage());
if (log.isDebugEnabled()) {
e.printStackTrace();
}
}
}
}
return retObj;
}