This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository nuiton-utils. See http://git.nuiton.org/nuiton-utils.git commit 05829817ca5294570341450dbf6d3b9db44626e2 Author: Tony CHEMIT <chemit@codelutin.com> Date: Mon Aug 17 08:53:54 2015 +0200 Add Binder.injectProperties method See #3753 --- src/main/java/org/nuiton/util/beans/Binder.java | 52 ++++++++++++++++++++++ .../java/org/nuiton/util/beans/BinderTest.java | 46 +++++++++++++++++++ src/test/resources/log4j.properties | 1 + 3 files changed, 99 insertions(+) diff --git a/src/main/java/org/nuiton/util/beans/Binder.java b/src/main/java/org/nuiton/util/beans/Binder.java index 529da44..f4a630a 100644 --- a/src/main/java/org/nuiton/util/beans/Binder.java +++ b/src/main/java/org/nuiton/util/beans/Binder.java @@ -246,6 +246,58 @@ public class Binder<I, O> implements Serializable { } /** + * Inject all not null properties to the target bean. + * + * @param properties properties to set into bean + * @param target the bean to set + * @since 3.0 + */ + public void injectProperties(Map<String, Object> properties, O target) { + injectProperties(properties, target, false); + } + + /** + * Inject all properties to the target bean. + * + * @param properties properties to set into bean + * @param target the bean to set + * @param includeNullValues {@code true} to set also null properties values + * @since 3.0 + */ + public void injectProperties(Map<String, Object> properties, O target, boolean includeNullValues) { + + for (Map.Entry<String, Object> entry : properties.entrySet()) { + String propertyName = entry.getKey(); + if (!getModel().containsTargetProperty(propertyName)) { + + throw new IllegalStateException("Could not find property '" + propertyName + "' in binder " + this + "."); + } + + Object propertyValue = entry.getValue(); + if (propertyValue == null && !includeNullValues) { + + // Skip null value + continue; + } + + if (log.isDebugEnabled()) { + log.debug("Inject property: " + propertyName + " to " + target); + } + Method writeMethod = getModel().getTargetWriteMethod(propertyName); + try { + writeMethod.invoke(target, propertyValue); + } catch (IllegalAccessException | InvocationTargetException e) { + throw new RuntimeException( + "Could not set property [" + + target.getClass().getName() + ":" + + propertyName + "]", e); + } + + } + + } + + /** * Copy properties from a source bean to a destination one according to * the model of the binder. If {@code propertyNames} is defined, only * those properties will be copied. diff --git a/src/test/java/org/nuiton/util/beans/BinderTest.java b/src/test/java/org/nuiton/util/beans/BinderTest.java index 2e513e5..2a37cbe 100644 --- a/src/test/java/org/nuiton/util/beans/BinderTest.java +++ b/src/test/java/org/nuiton/util/beans/BinderTest.java @@ -22,6 +22,7 @@ package org.nuiton.util.beans; +import com.google.common.collect.ImmutableMap; import org.junit.AfterClass; import org.junit.Assert; import org.junit.Before; @@ -161,6 +162,51 @@ public class BinderTest { } @Test + public void testInjectProperties() { + + ImmutableMap.Builder<String, Object> builder = new ImmutableMap.Builder<>(); + builder.put(BeanA.PROPERTY_A, "a"); + + Assert.assertNull(a.getA()); + + { + ImmutableMap<String, Object> properties = builder.build(); + binderA.injectProperties(properties, a); + + Assert.assertEquals(a.getA(), properties.get(BeanA.PROPERTY_A)); + + } + + builder.put(BeanA.PROPERTY_B, "b"); + + { + + ImmutableMap<String, Object> properties = builder.build(); + try { + binderA.injectProperties(properties, a); + Assert.fail(); + } catch (IllegalStateException e) { + // Ok normal case binder does not have properties b + } + + } + + { + + Assert.assertNull(b.getA()); + Assert.assertNull(b.getB()); + + ImmutableMap<String, Object> properties = builder.build(); + binderB.injectProperties(properties, b); + + Assert.assertEquals(b.getA(), properties.get(BeanB.PROPERTY_A)); + Assert.assertEquals(b.getB(), properties.get(BeanB.PROPERTY_B)); + Assert.assertEquals(b.getC(), properties.get(BeanB.PROPERTY_B)); + } + + } + + @Test public void testCopy() { a.setA(VALUE_A); diff --git a/src/test/resources/log4j.properties b/src/test/resources/log4j.properties index 67c3151..1d27876 100644 --- a/src/test/resources/log4j.properties +++ b/src/test/resources/log4j.properties @@ -29,3 +29,4 @@ log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) %M - %m%n # package level log4j.logger.org.nuiton.util=INFO +log4j.logger.org.nuiton.util.beans.Binder=DEBUG -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.