commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brit...@apache.org
Subject svn commit: r1633705 - in /commons/sandbox/beanutils2/trunk/src: main/java/org/apache/commons/beanutils2/ test/java/org/apache/commons/beanutils2/
Date Wed, 22 Oct 2014 20:07:01 GMT
Author: britter
Date: Wed Oct 22 20:07:00 2014
New Revision: 1633705

URL: http://svn.apache.org/r1633705
Log:
Centralize BeanAccessor construction in BeanAccessorFactory

Removed:
    commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/NullBeanAccessor.java
Modified:
    commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/BeanAccessorFactory.java
    commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultArgumentsAccessor.java
    commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanAccessor.java
    commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanPropertySetter.java
    commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultClassAccessor.java
    commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultIndexedPropertyGetterAccessor.java
    commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultIndexedPropertySetter.java
    commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultMappedPropertyGetterAccessor.java
    commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultMappedPropertySetter.java
    commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/GetPropertyTestCase.java
    commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/MapBeanTestCase.java
    commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/VoidMethodsTestCase.java

Modified: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/BeanAccessorFactory.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/BeanAccessorFactory.java?rev=1633705&r1=1633704&r2=1633705&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/BeanAccessorFactory.java
(original)
+++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/BeanAccessorFactory.java
Wed Oct 22 20:07:00 2014
@@ -17,10 +17,38 @@
 
 package org.apache.commons.beanutils2;
 
+import static java.lang.String.format;
+
+import java.beans.IntrospectionException;
+import java.lang.reflect.Method;
 import java.util.Map;
 
 final class BeanAccessorFactory {
 
+    /**
+     * Creates a BeanAccessor implementation for the given bean.
+     *
+     * @param bean       The bean to create the accessor for.
+     * @param clazz      the class of the bean, the wrapped been has been accessed from.
+     * @param readMethod the read method that read the bean.
+     * @param <B>        the type of the bean to be wrapped.
+     * @return a fitting BeanAccessor implementation
+     */
+    public static <B> BeanAccessor<B> createBeanAccessor(B bean, Class<?>
clazz, Method readMethod) {
+        if (bean == null) {
+            return new NullBeanAccessor<B>(clazz.getName(), readMethod.getName());
+        } else {
+            return createBeanAccessor(bean);
+        }
+    }
+
+    /**
+     * Creates a BeanAccessor implementation for the given bean.
+     *
+     * @param bean The bean to create the accessor for.
+     * @param <B>  the type of the bean to be wrapped.
+     * @return a fitting BeanAccessor implementation
+     */
     public static <B> BeanAccessor<B> createBeanAccessor(B bean) {
         if (bean instanceof Map) {
             return new MapBeanAccessor<B>(asMap(bean));
@@ -49,4 +77,133 @@ final class BeanAccessorFactory {
             Assertions.checkArgument((firstKey instanceof String), "Map keys have to be of
type String!");
         }
     }
+
+    private static final class NullBeanAccessor<B> implements BeanAccessor<B>
{
+
+        private final String errorMessage;
+
+        public NullBeanAccessor(String beanTypeName, String methodName) {
+            errorMessage = format("%s.%s returned null!", beanTypeName, methodName);
+        }
+
+        /**
+         * {@inheritDoc}
+         */
+        public BeanAccessor<?> get(String propertyName) {
+            throw new NullPointerException(errorMessage);
+        }
+
+        /**
+         * {@inheritDoc}
+         */
+        public <V> V getValue(String propertyName) {
+            throw new NullPointerException(errorMessage);
+        }
+
+        /**
+         * {@inheritDoc}
+         */
+        public IndexedPropertyGetterAccessor<?> getIndexed(String propertyName) {
+            throw new NullPointerException(errorMessage);
+        }
+
+        /**
+         * {@inheritDoc}
+         */
+        public MappedPropertyGetterAccessor getMapped(String propertyName) {
+            throw new NullPointerException(errorMessage);
+        }
+
+        /**
+         * {@inheritDoc}
+         */
+        public B get() {
+            return null;
+        }
+
+        /**
+         * {@inheritDoc}
+         */
+        public <V> V cast() {
+            return null;
+        }
+
+        /**
+         * {@inheritDoc}
+         */
+        public BeanPropertySetter<B> set(String propertyName) {
+            throw new NullPointerException(errorMessage);
+        }
+
+        /**
+         * {@inheritDoc}
+         */
+        public IndexedPropertySetterAccessor<B> setIndexed(String propertyName) {
+            throw new NullPointerException(errorMessage);
+        }
+
+        /**
+         * {@inheritDoc}
+         */
+        public MappedPropertySetterAccessor<B> setMapped(String propertyName) {
+            throw new NullPointerException(errorMessage);
+        }
+
+        /**
+         * {@inheritDoc}
+         */
+        public boolean isReadable(String propertyName) throws IntrospectionException {
+            throw new NullPointerException(errorMessage);
+        }
+
+        /**
+         * {@inheritDoc}
+         */
+        public boolean isWritable(String propertyName) throws IntrospectionException {
+            throw new NullPointerException(errorMessage);
+        }
+
+        /**
+         * {@inheritDoc}
+         */
+        public B cloneBean() {
+            return null;
+        }
+
+        /**
+         * {@inheritDoc}
+         */
+        public <T extends B> void copyPropertiesTo(T target) {
+            throw new NullPointerException(errorMessage);
+        }
+
+        /**
+         * {@inheritDoc}
+         */
+        public Map<String, Object> describe() {
+            throw new NullPointerException(errorMessage);
+        }
+
+        /**
+         * {@inheritDoc}
+         */
+        public void populate(Map<String, Object> properties) {
+            throw new NullPointerException(errorMessage);
+        }
+
+        /**
+         * {@inheritDoc}
+         */
+        public ArgumentsAccessor invoke(String methodName) {
+            throw new NullPointerException(errorMessage);
+        }
+
+        /**
+         * {@inheritDoc}
+         */
+        public ArgumentsAccessor invokeExact(String methodName) {
+            throw new NullPointerException(errorMessage);
+        }
+
+    }
 }

Modified: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultArgumentsAccessor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultArgumentsAccessor.java?rev=1633705&r1=1633704&r2=1633705&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultArgumentsAccessor.java
(original)
+++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultArgumentsAccessor.java
Wed Oct 22 20:07:00 2014
@@ -62,11 +62,7 @@ final class DefaultArgumentsAccessor
         }
 
         Object result = invoke(method, arguments);
-
-        if (method.getReturnType() == void.class || result == null) {
-            return new NullBeanAccessor<Object>(beanType.getName(), methodName);
-        }
-        return new DefaultBeanAccessor<Object>(result);
+        return BeanAccessorFactory.createBeanAccessor(result, beanType, method);
     }
 
     private Object invoke(Method method, Argument<?>... arguments) {

Modified: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanAccessor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanAccessor.java?rev=1633705&r1=1633704&r2=1633705&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanAccessor.java
(original)
+++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanAccessor.java
Wed Oct 22 20:07:00 2014
@@ -50,7 +50,7 @@ final class DefaultBeanAccessor<B>
      */
     public BeanAccessor<?> get(String propertyName) {
         Object value = getValue(propertyName);
-        return new DefaultBeanAccessor<Object>(value);
+        return BeanAccessorFactory.createBeanAccessor(value);
     }
 
     public <V> V getValue(String propertyName) {

Modified: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanPropertySetter.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanPropertySetter.java?rev=1633705&r1=1633704&r2=1633705&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanPropertySetter.java
(original)
+++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultBeanPropertySetter.java
Wed Oct 22 20:07:00 2014
@@ -56,7 +56,7 @@ final class DefaultBeanPropertySetter<B>
 
         invokeSetter(value);
 
-        return new DefaultBeanAccessor<B>(bean);
+        return BeanAccessorFactory.createBeanAccessor(bean);
     }
 
     private <V> void invokeSetter(V value) {

Modified: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultClassAccessor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultClassAccessor.java?rev=1633705&r1=1633704&r2=1633705&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultClassAccessor.java
(original)
+++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultClassAccessor.java
Wed Oct 22 20:07:00 2014
@@ -93,7 +93,7 @@ final class DefaultClassAccessor<B>
         }
 
         B bean = doInvokeConstructor(constructor, parameterObjects);
-        return new DefaultBeanAccessor<B>(bean);
+        return BeanAccessorFactory.createBeanAccessor(bean);
     }
 
     /**

Modified: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultIndexedPropertyGetterAccessor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultIndexedPropertyGetterAccessor.java?rev=1633705&r1=1633704&r2=1633705&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultIndexedPropertyGetterAccessor.java
(original)
+++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultIndexedPropertyGetterAccessor.java
Wed Oct 22 20:07:00 2014
@@ -44,10 +44,7 @@ final class DefaultIndexedPropertyGetter
      */
     public BeanAccessor<?> at(int index) {
         Object indexedValue = valueAt(index);
-        if (indexedValue == null) {
-            return new NullBeanAccessor<Object>(bean.getClass().getName(), indexedReadMethod.getName());
-        }
-        return BeanAccessorFactory.createBeanAccessor(indexedValue);
+        return BeanAccessorFactory.createBeanAccessor(indexedValue, bean.getClass(), indexedReadMethod);
     }
 
     public <V> V valueAt(int index) {

Modified: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultIndexedPropertySetter.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultIndexedPropertySetter.java?rev=1633705&r1=1633704&r2=1633705&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultIndexedPropertySetter.java
(original)
+++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultIndexedPropertySetter.java
Wed Oct 22 20:07:00 2014
@@ -62,7 +62,7 @@ final class DefaultIndexedPropertySetter
 
         invokeSetter(value);
 
-        return new DefaultBeanAccessor<B>(bean);
+        return BeanAccessorFactory.createBeanAccessor(bean);
     }
 
     private <V> void invokeSetter(V value) {

Modified: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultMappedPropertyGetterAccessor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultMappedPropertyGetterAccessor.java?rev=1633705&r1=1633704&r2=1633705&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultMappedPropertyGetterAccessor.java
(original)
+++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultMappedPropertyGetterAccessor.java
Wed Oct 22 20:07:00 2014
@@ -49,7 +49,7 @@ class DefaultMappedPropertyGetterAccesso
      */
     public BeanAccessor<?> of(String key) {
         Object mappedValue = valueOf(key);
-        return wrapInAccessor(mappedValue);
+        return BeanAccessorFactory.createBeanAccessor(mappedValue, bean.getClass(), mappedReadMethod);
     }
 
     /**
@@ -74,12 +74,4 @@ class DefaultMappedPropertyGetterAccesso
         }
     }
 
-    private BeanAccessor<?> wrapInAccessor(Object mappedValue) {
-        if (mappedValue != null) {
-            return BeanAccessorFactory.createBeanAccessor(mappedValue);
-        } else {
-            return new NullBeanAccessor<Object>(bean.getClass().getName(), mappedReadMethod.getName());
-        }
-    }
-
 }

Modified: commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultMappedPropertySetter.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultMappedPropertySetter.java?rev=1633705&r1=1633704&r2=1633705&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultMappedPropertySetter.java
(original)
+++ commons/sandbox/beanutils2/trunk/src/main/java/org/apache/commons/beanutils2/DefaultMappedPropertySetter.java
Wed Oct 22 20:07:00 2014
@@ -56,7 +56,7 @@ final class DefaultMappedPropertySetter<
 
         invokeSetter(value);
 
-        return new DefaultBeanAccessor<B>(bean);
+        return BeanAccessorFactory.createBeanAccessor(bean);
     }
 
     private <V> void invokeSetter(V value) {

Modified: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/GetPropertyTestCase.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/GetPropertyTestCase.java?rev=1633705&r1=1633704&r2=1633705&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/GetPropertyTestCase.java
(original)
+++ commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/GetPropertyTestCase.java
Wed Oct 22 20:07:00 2014
@@ -59,6 +59,11 @@ public final class GetPropertyTestCase {
         assertTrue("Got an incorrect value", ((Boolean) value).booleanValue() == bean.getBooleanProperty());
     }
 
+    @Test
+    public void getMapProperty() throws Exception {
+        assertEquals("First Value", on(bean).get("mapProperty").get("First Key").get());
+    }
+
     @Test(expected = PropertyNotReadableException.class)
     public void getWriteOnlyProperty()
             throws Exception {

Modified: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/MapBeanTestCase.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/MapBeanTestCase.java?rev=1633705&r1=1633704&r2=1633705&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/MapBeanTestCase.java
(original)
+++ commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/MapBeanTestCase.java
Wed Oct 22 20:07:00 2014
@@ -223,6 +223,11 @@ public class MapBeanTestCase {
         assertEquals(Integer.valueOf(15), on(bean).getMapped("mappedMap").of("whatever").get("integerProperty").get());
     }
 
+    @Test
+    public void mapsReturnedFromMethodInvocationsAreWrappedCorrectly() throws Exception {
+        assertEquals(Integer.valueOf(15), on(bean).invoke("getMappedMap").with(argument("whatever")).get("integerProperty").get());
+    }
+
     private static class MapTestBean {
 
         private Map<String, Object> map;

Modified: commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/VoidMethodsTestCase.java
URL: http://svn.apache.org/viewvc/commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/VoidMethodsTestCase.java?rev=1633705&r1=1633704&r2=1633705&view=diff
==============================================================================
--- commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/VoidMethodsTestCase.java
(original)
+++ commons/sandbox/beanutils2/trunk/src/test/java/org/apache/commons/beanutils2/VoidMethodsTestCase.java
Wed Oct 22 20:07:00 2014
@@ -39,7 +39,6 @@ public class VoidMethodsTestCase {
     public void setUp()
             throws Exception {
         voidAccessor = on(TestBean.class).invokeStatic("incrementCounter").with();
-        assertTrue(voidAccessor instanceof NullBeanAccessor);
     }
 
     @After



Mime
View raw message