This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository nuiton-utils. See https://gitlab.nuiton.org/nuiton/nuiton-utils.git commit eb029abdd904c9a80bcad1e15aea0a1b74d4a73b Author: Tony CHEMIT <chemit@codelutin.com> Date: Mon Jul 4 17:47:43 2016 +0200 Add an new parameter on Binder.obtainProperties to keep primitive default values and not change them to null value (Fixes #3955) --- src/main/java/org/nuiton/util/beans/Binder.java | 79 +++++++++++++++---------- 1 file changed, 49 insertions(+), 30 deletions(-) diff --git a/src/main/java/org/nuiton/util/beans/Binder.java b/src/main/java/org/nuiton/util/beans/Binder.java index f8c6cc6..2f9d559 100644 --- a/src/main/java/org/nuiton/util/beans/Binder.java +++ b/src/main/java/org/nuiton/util/beans/Binder.java @@ -166,19 +166,20 @@ public class Binder<I, O> implements Serializable { } /** - * Obtain from the given object all properties registered in the binder - * model. + * Obtain from the given object all properties registered in the binder model. * - * @param source the bean to read - * @param propertyNames subset of properties to load - * @param includeNullValues get <strong>all</strong> the properties and - * values for the given bean. If false, you'll get only the values + * @param source the bean to read + * @param propertyNames subset of properties to load + * @param keepPrimitiveDefaultValues to keep primitive default value and not replace them by a {@code null} value. + * @param includeNullValues get <strong>all</strong> the properties and values for the given bean. If false, you'll get only the values * @return the map of properties obtained indexed by their property name, * or an empty map is the given {@code from} is {@code null}. - * @since 2.3 + * @since 3.0 */ public Map<String, Object> obtainProperties(I source, - boolean includeNullValues, String... propertyNames) { + boolean keepPrimitiveDefaultValues, + boolean includeNullValues, + String... propertyNames) { if (source == null) { // special limit case return Collections.emptyMap(); @@ -195,11 +196,11 @@ public class Binder<I, O> implements Serializable { read = readMethod.invoke(source); if (log.isDebugEnabled()) { log.debug("property " + sourceProperty + ", type : " + - readMethod.getReturnType() + ", value = " + read); + readMethod.getReturnType() + ", value = " + read); } - if (readMethod.getReturnType().isPrimitive() && - ObjectUtil.getNullValue( - readMethod.getReturnType()).equals(read)) { + if (readMethod.getReturnType().isPrimitive() + && !keepPrimitiveDefaultValues + && ObjectUtil.getNullValue(readMethod.getReturnType()).equals(read)) { // for primitive type case, force nullity read = null; } @@ -232,6 +233,24 @@ public class Binder<I, O> implements Serializable { * Obtain from the given object all properties registered in the binder * model. * + * @param source the bean to read + * @param propertyNames subset of properties to load + * @param includeNullValues get <strong>all</strong> the properties and + * values for the given bean. If false, you'll get only the values + * @return the map of properties obtained indexed by their property name, + * or an empty map is the given {@code from} is {@code null}. + * @since 2.3 + */ + public Map<String, Object> obtainProperties(I source, + boolean includeNullValues, String... propertyNames) { + + return obtainProperties(source, false, includeNullValues, propertyNames); + } + + /** + * Obtain from the given object all properties registered in the binder + * model. + * * <b>Note:</b> If a property's value is null, it will not be injected in * the result. * @@ -254,7 +273,7 @@ public class Binder<I, O> implements Serializable { * * @param source the source object to inspect * @param propertyName name of the property to get - * @param <OO> type of property to get + * @param <OO> type of property to get * @return the property value in the source object. * @since 3.0 */ @@ -271,7 +290,7 @@ public class Binder<I, O> implements Serializable { OO result = (OO) readMethod.invoke(source); if (log.isDebugEnabled()) { log.debug("property " + propertyName + ", type : " + - readMethod.getReturnType() + ", value = " + result); + readMethod.getReturnType() + ", value = " + result); } return result; @@ -289,7 +308,7 @@ public class Binder<I, O> implements Serializable { * * @param target the target object to inspect * @param propertyName name of the property to get - * @param <OO> type of property to get + * @param <OO> type of property to get * @return the property value in the target object. * @since 3.0 */ @@ -306,7 +325,7 @@ public class Binder<I, O> implements Serializable { OO result = (OO) readMethod.invoke(target); if (log.isDebugEnabled()) { log.debug("property " + propertyName + ", type : " + - readMethod.getReturnType() + ", value = " + result); + readMethod.getReturnType() + ", value = " + result); } return result; @@ -371,8 +390,8 @@ public class Binder<I, O> implements Serializable { } catch (Exception e) { throw new RuntimeException( "Could not set property [" + - target.getClass().getName() + ":" + - propertyName + "]", e); + target.getClass().getName() + ":" + + propertyName + "]", e); } } @@ -507,8 +526,8 @@ public class Binder<I, O> implements Serializable { } propertyNames = excludeProperties ? - getAllPropertiesExclude(propertyNames) : - getProperties(propertyNames); + getAllPropertiesExclude(propertyNames) : + getProperties(propertyNames); boolean useFunctions = model.isUseFunctions(); @@ -530,7 +549,7 @@ public class Binder<I, O> implements Serializable { } if (log.isDebugEnabled()) { log.debug("property " + sourceProperty + ", type : " + - readMethod.getReturnType() + ", value = " + read); + readMethod.getReturnType() + ", value = " + read); } if (model.containsBinderProperty(sourceProperty)) { @@ -551,10 +570,10 @@ public class Binder<I, O> implements Serializable { } catch (Exception e) { throw new RuntimeException( "Could not bind property [" + - source.getClass().getName() + ":" + - sourceProperty + "] to [" + - target.getClass().getName() + ":" + - targetProperty + "]", e); + source.getClass().getName() + ":" + + sourceProperty + "] to [" + + target.getClass().getName() + ":" + + targetProperty + "]", e); } } } @@ -574,7 +593,7 @@ public class Binder<I, O> implements Serializable { } if (log.isDebugEnabled()) { log.debug("property " + sourceProperty + ", type : " + - readMethod.getReturnType() + ", value = " + read); + readMethod.getReturnType() + ", value = " + read); } if (model.containsBinderProperty(sourceProperty)) { @@ -593,7 +612,7 @@ public class Binder<I, O> implements Serializable { } catch (Exception e) { throw new RuntimeException( "could not read property " + sourceProperty + - " on source " + source); + " on source " + source); } } @@ -609,8 +628,8 @@ public class Binder<I, O> implements Serializable { } propertyNames = excludeProperties ? - getAllPropertiesExclude(propertyNames) : - getProperties(propertyNames); + getAllPropertiesExclude(propertyNames) : + getProperties(propertyNames); List<PropertyDiff> result = new LinkedList<PropertyDiff>(); @@ -718,7 +737,7 @@ public class Binder<I, O> implements Serializable { if (!model.containsSourceProperty(propertyName)) { throw new IllegalArgumentException( "property '" + propertyName + - "' is not known by binder"); + "' is not known by binder"); } } } -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.