branch develop updated (07d19ab -> 657a5f6)
This is an automated email from the git hooks/post-receive script. New change to branch develop in repository nuiton-utils. See http://git.nuiton.org/nuiton-utils.git from 07d19ab Add also generic type methods (refs #3757) new 307a6d6 Add obtainSourceProperty and obtainTargetProperty in Binder (see #3769) new 657a5f6 Add obtainSourceProperty and obtainTargetProperty in Binder (fixes #3769) Merge branch 'feature/3769' into develop The 2 revisions listed above as "new" are entirely new to this repository and will be described in separate emails. The revisions listed as "adds" were already present in the repository and have only been added to this reference. Detailed log of new commits: commit 657a5f68eeb4e24e26c2b1126f641196fedfac85 Merge: 07d19ab 307a6d6 Author: Tony CHEMIT <chemit@codelutin.com> Date: Wed Aug 26 09:59:13 2015 +0200 Add obtainSourceProperty and obtainTargetProperty in Binder (fixes #3769) Merge branch 'feature/3769' into develop commit 307a6d6a6f7304627c6dbdacb4e26d9552d12a3f Author: Tony CHEMIT <chemit@codelutin.com> Date: Wed Aug 26 09:59:10 2015 +0200 Add obtainSourceProperty and obtainTargetProperty in Binder (see #3769) Summary of changes: src/main/java/org/nuiton/util/beans/Binder.java | 72 ++++++++++++++++++++++ .../java/org/nuiton/util/beans/BinderTest.java | 66 ++++++++++++++++++++ 2 files changed, 138 insertions(+) -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
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 307a6d6a6f7304627c6dbdacb4e26d9552d12a3f Author: Tony CHEMIT <chemit@codelutin.com> Date: Wed Aug 26 09:59:10 2015 +0200 Add obtainSourceProperty and obtainTargetProperty in Binder (see #3769) --- src/main/java/org/nuiton/util/beans/Binder.java | 72 ++++++++++++++++++++++ .../java/org/nuiton/util/beans/BinderTest.java | 66 ++++++++++++++++++++ 2 files changed, 138 insertions(+) diff --git a/src/main/java/org/nuiton/util/beans/Binder.java b/src/main/java/org/nuiton/util/beans/Binder.java index 81a6a6d..20354bf 100644 --- a/src/main/java/org/nuiton/util/beans/Binder.java +++ b/src/main/java/org/nuiton/util/beans/Binder.java @@ -23,6 +23,7 @@ package org.nuiton.util.beans; import com.google.common.base.Function; +import com.google.common.base.Preconditions; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -244,6 +245,77 @@ public class Binder<I, O> implements Serializable { } /** + * Obtain a property from a source object (A source object type reflect + * the source type of the binder). + * + * <b>Note:</b> The property value has no special treatment, the result + * is the exact value from the source object (no binder collection transformation, ...). + * + * @param source the source object to inspect + * @param propertyName name of the property to get + * @param <OO> type of property to get + * @return the property value in the source object. + * @since 3.0 + */ + public <OO> OO obtainSourceProperty(I source, String propertyName) { + + Preconditions.checkNotNull(source, "source can not be null"); + Preconditions.checkNotNull(propertyName, "propertyName can not be null"); + + Method readMethod = model.getSourceReadMethod(propertyName); + + Preconditions.checkNotNull(readMethod, "Could not find source getter for property: " + propertyName); + + try { + OO result = (OO) readMethod.invoke(source); + if (log.isDebugEnabled()) { + log.debug("property " + propertyName + ", type : " + + readMethod.getReturnType() + ", value = " + result); + } + return result; + + } catch (Exception e) { + throw new RuntimeException("Could not obtain property: " + propertyName, e); + } + } + + /** + * Obtain a property from a target object (A target object type reflect + * the target type of the binder). + * + * <b>Note:</b> The property value has no special treatment, the result + * is the exact value from the target object (no binder collection transformation, ...). + * + * @param target the target object to inspect + * @param propertyName name of the property to get + * @param <OO> type of property to get + * @return the property value in the target object. + * @since 3.0 + */ + public <OO> OO obtainTargetProperty(I target, String propertyName) { + + Preconditions.checkNotNull(target, "target can not be null"); + Preconditions.checkNotNull(propertyName, "propertyName can not be null"); + + Method readMethod = model.getTargetReadMethod(propertyName); + + Preconditions.checkNotNull(readMethod, "Could not find target getter for property: " + propertyName); + + try { + OO result = (OO) readMethod.invoke(target); + if (log.isDebugEnabled()) { + log.debug("property " + propertyName + ", type : " + + readMethod.getReturnType() + ", value = " + result); + } + return result; + + } catch (Exception e) { + throw new RuntimeException("Could not obtain property: " + propertyName, e); + } + + } + + /** * Inject all not null properties to the target bean. * * @param properties properties to set into bean diff --git a/src/test/java/org/nuiton/util/beans/BinderTest.java b/src/test/java/org/nuiton/util/beans/BinderTest.java index 4e4b95e..dc47bfd 100644 --- a/src/test/java/org/nuiton/util/beans/BinderTest.java +++ b/src/test/java/org/nuiton/util/beans/BinderTest.java @@ -162,6 +162,72 @@ public class BinderTest { } @Test + public void testObtainSourceProperty() { + + { + String value = binderA.obtainSourceProperty(a, BeanA.PROPERTY_A); + Assert.assertNull(value); + } + a.setA(VALUE_A); + { + String value = binderA.obtainSourceProperty(a, BeanA.PROPERTY_A); + Assert.assertEquals(VALUE_A, value); + } + + // Binder A has no property e declared + try { + binderA.obtainSourceProperty(a, BeanA.PROPERTY_E); + Assert.fail(); + } catch (Exception e) { + Assert.assertTrue(true); + } + + { + int value = binderB.obtainSourceProperty(a, BeanA.PROPERTY_E); + Assert.assertEquals(0, value); + } + a.setE(VALUE_E); + { + int value = binderB.obtainSourceProperty(a, BeanA.PROPERTY_E); + Assert.assertEquals(VALUE_E, value); + } + + } + + @Test + public void testObtainTargetProperty() { + + { + String value = binderA.obtainTargetProperty(a, BeanA.PROPERTY_A); + Assert.assertNull(value); + } + a.setA(VALUE_A); + { + String value = binderA.obtainTargetProperty(a, BeanA.PROPERTY_A); + Assert.assertEquals(VALUE_A, value); + } + + // Binder A has no property e declared + try { + binderA.obtainTargetProperty(a, BeanA.PROPERTY_E); + Assert.fail(); + } catch (Exception e) { + Assert.assertTrue(true); + } + + { + int value = binderB.obtainTargetProperty(b, BeanB.PROPERTY_E2); + Assert.assertEquals(0, value); + } + b.setE2(VALUE_E); + { + int value = binderB.obtainTargetProperty(b, BeanB.PROPERTY_E2); + Assert.assertEquals(VALUE_E, value); + } + + } + + @Test public void testInjectProperties() { ImmutableMap.Builder<String, Object> builder = new ImmutableMap.Builder<>(); -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
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 657a5f68eeb4e24e26c2b1126f641196fedfac85 Merge: 07d19ab 307a6d6 Author: Tony CHEMIT <chemit@codelutin.com> Date: Wed Aug 26 09:59:13 2015 +0200 Add obtainSourceProperty and obtainTargetProperty in Binder (fixes #3769) Merge branch 'feature/3769' into develop src/main/java/org/nuiton/util/beans/Binder.java | 72 ++++++++++++++++++++++ .../java/org/nuiton/util/beans/BinderTest.java | 66 ++++++++++++++++++++ 2 files changed, 138 insertions(+) -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
participants (1)
-
nuiton.org scm