Author: bleny Date: 2011-09-01 12:09:16 +0200 (Thu, 01 Sep 2011) New Revision: 2199 Url: http://nuiton.org/repositories/revision/nuiton-utils/2199 Log: #1728: a bit of refactoring on diff() Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/Binder.java Modified: trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/Binder.java =================================================================== --- trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/Binder.java 2011-09-01 09:53:58 UTC (rev 2198) +++ trunk/nuiton-utils/src/main/java/org/nuiton/util/beans/Binder.java 2011-09-01 10:09:16 UTC (rev 2199) @@ -310,10 +310,10 @@ } } - protected Object readSourceProperty(String sourceProperty, I source) { + protected Object readProperty(String sourceProperty, Object source, + Method readMethod) { try { Object read = null; - Method readMethod = model.getSourceReadMethod(sourceProperty); if (source != null) { // obtain value from source read = readMethod.invoke(source); @@ -348,44 +348,6 @@ } } - protected Object readTargetProperty(String targetProperty, O target) { - try { - Object read = null; - Method readMethod = model.getTargetReadMethod(targetProperty); - if (target != null) { - // obtain value from source - read = readMethod.invoke(target); - } - // obtain acceptable null value (for primitive types, use - // default values). - if (read == null) { - read = ObjectUtil.getNullValue(readMethod.getReturnType()); - } - if (log.isDebugEnabled()) { - log.debug("property " + targetProperty + ", type : " + - readMethod.getReturnType() + ", value = " + read); - } - - if (model.containsBinderProperty(targetProperty)) { - if (model.containsCollectionProperty(targetProperty)) { - read = bindCollection(targetProperty, read); - } else { - read = bindProperty(targetProperty, read); - } - } else if (model.containsCollectionProperty(targetProperty)) { - - // specific collection strategy is set, must use it - read = getCollectionValue(targetProperty, read); - } - - return read; - } catch (Exception e) { - throw new RuntimeException( - "could not read property " + targetProperty + - " on target " + target); - } - } - public class PropertyDiff { protected String sourceProperty; @@ -440,6 +402,9 @@ protected List<PropertyDiff> diff(I source, O target, boolean excludeProperties, String... propertyNames) { + if (source == null) { + throw new NullPointerException("parameter 'source' can no be null"); + } if (target == null) { throw new NullPointerException("parameter 'target' can no be null"); } @@ -452,12 +417,16 @@ for (String sourceProperty : propertyNames) { - Object sourceRead = readSourceProperty(sourceProperty, source); + Method sourceReadMethod = model.getSourceReadMethod(sourceProperty); + Object sourceRead = readProperty(sourceProperty, source, sourceReadMethod); + String targetProperty = model.getTargetProperty(sourceProperty); - Object targetRead = readTargetProperty(targetProperty, target); + Method targetReadMethod = model.getTargetReadMethod(targetProperty); + Object targetRead = readProperty(targetProperty, target, targetReadMethod); + if (ObjectUtils.notEqual(sourceRead, targetRead)) { PropertyDiff propertyDiff = new PropertyDiff(sourceProperty, sourceRead, targetProperty, targetRead); result.add(propertyDiff);