List<EditHistory> getEditHistory(String userId, Obj source, Obj target, Class clazz) {
List<EditHistory> editHistoryList = new ArrayList<>();
try {

for (Method sourceMethod : clazz.getMethods()) {
for (Method targetMethod : clazz.getMethods()) {
String sourceMethodName = sourceMethod.getName();
String targetMethodName = targetMethod.getName();
String startWithGet = "get";
String startWithIs = "is";
boolean methodGet = sourceMethodName.startsWith(startWithGet);
boolean methodIs = sourceMethodName.startsWith(startWithIs);
if ((methodGet || methodIs)
&& !sourceMethodName.equals("getClass")) {
if (sourceMethodName.equals(targetMethodName)) {
Object sourceResult = MethodHandles.lookup().findVirtual(clazz, sourceMethodName,
MethodType.methodType(sourceMethod.getReturnType())).invoke(source);
Object targetResult = MethodHandles.lookup().findVirtual(clazz, targetMethodName,
MethodType.methodType(targetMethod.getReturnType())).invoke(target);

if (sourceResult != null && !sourceResult.equals(targetResult)) {
EditHistory skuEditHistory = new EditHistory();
editHistory.setEditField(sourceMethodName.substring(methodGet ? startWithGet.length() : startWithIs.length()));
editHistory.setOriginValue(sourceResult.toString());
editHistory.setChangedValue(targetResult != null ? targetResult.toString() : null);
editHistory.setCreatedId(userId);
editHistoryList.add(editHistory);
}
}
}
}
}

return editHistoryList;
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
} catch (Exception e) {
throw new RuntimeException(e);
} catch (Throwable e) {
throw new RuntimeException(e);
}
}


'java' 카테고리의 다른 글

java script 숫자 또는 영문자만 허용하기  (0) 2015.03.19
바코드 체크 비트 검증 Java 코드  (0) 2015.03.06
JVM에 관한 토론 기록  (0) 2015.01.21
Java MethodHandle Example (Java 1.7)  (0) 2014.10.07
Java reflection example.  (0) 2014.10.07

+ Recent posts