commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1744955 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration2/AbstractConfiguration.java test/java/org/apache/commons/configuration2/TestAbstractConfigurationBasicFeatures.java
Date Sat, 21 May 2016 15:20:02 GMT
Author: oheger
Date: Sat May 21 15:20:02 2016
New Revision: 1744955

URL: http://svn.apache.org/viewvc?rev=1744955&view=rev
Log:
[CONFIGURATION-626] get() now supports array conversions.

The method now checks whether the target class of the conversion is
an array class. If so, it delegates to an internal method doing an
array conversion.

Modified:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestAbstractConfigurationBasicFeatures.java

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java?rev=1744955&r1=1744954&r2=1744955&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java
(original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java
Sat May 21 15:20:02 2016
@@ -1505,10 +1505,7 @@ public abstract class AbstractConfigurat
     @Override
     public Object getArray(Class<?> cls, String key, Object defaultValue)
     {
-        checkDefaultValueArray(cls, defaultValue);
-        return ObjectUtils.defaultIfNull(
-                getConversionHandler().toArray(getProperty(key), cls,
-                        getInterpolator()), defaultValue);
+        return convertToArray(cls, key, defaultValue);
     }
 
     @Override
@@ -1780,6 +1777,11 @@ public abstract class AbstractConfigurat
     private <T> T convert(Class<T> cls, String key, T defValue,
             boolean throwOnMissing)
     {
+        if (cls.isArray())
+        {
+            return cls.cast(convertToArray(cls.getComponentType(), key, defValue));
+        }
+
         T result = getAndConvertProperty(cls, key, defValue);
         if (result == null)
         {
@@ -1794,6 +1796,28 @@ public abstract class AbstractConfigurat
     }
 
     /**
+     * Performs a conversion to an array result class. This implementation
+     * delegates to the {@link ConversionHandler} to perform the actual type
+     * conversion. If this results in a <b>null</b> result (because the property
+     * is undefined), the default value is returned. It is checked whether the
+     * default value is an array with the correct component type. If not, an
+     * exception is thrown.
+     *
+     * @param cls the component class of the array
+     * @param key the configuration key
+     * @param defaultValue an optional default value
+     * @return the converted array
+     * @throws IllegalArgumentException if the default value is not a compatible
+     *         array
+     */
+    private Object convertToArray(Class<?> cls, String key, Object defaultValue)
+    {
+        checkDefaultValueArray(cls, defaultValue);
+        return ObjectUtils.defaultIfNull(getConversionHandler().toArray(
+                getProperty(key), cls, getInterpolator()), defaultValue);
+    }
+
+    /**
      * Checks an object provided as default value for the {@code getArray()}
      * method. Throws an exception if this is not an array with the correct
      * component type.

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestAbstractConfigurationBasicFeatures.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestAbstractConfigurationBasicFeatures.java?rev=1744955&r1=1744954&r2=1744955&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestAbstractConfigurationBasicFeatures.java
(original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration2/TestAbstractConfigurationBasicFeatures.java
Sat May 21 15:20:02 2016
@@ -765,8 +765,7 @@ public class TestAbstractConfigurationBa
             config.addProperty(KEY_PREFIX, String.valueOf(i));
             expected[i] = Integer.valueOf(i);
         }
-        Integer[] result =
-                (Integer[]) config.getArray(Integer.class, KEY_PREFIX);
+        Integer[] result = config.get(Integer[].class, KEY_PREFIX);
         assertArrayEquals("Wrong result", expected, result);
     }
 
@@ -784,22 +783,22 @@ public class TestAbstractConfigurationBa
             expected[i] = (short) i;
         }
         short[] result =
-                (short[]) config.getArray(Short.TYPE, KEY_PREFIX, new short[0]);
+                config.get(short[].class, KEY_PREFIX, new short[0]);
         assertArrayEquals("Wrong result", expected, result);
     }
 
     /**
-     * Tests getArray() for an unknown property if no default value is provided.
+     * Tests get() for an unknown array property if no default value is provided.
      */
     @Test
     public void testGetArrayUnknownNoDefault()
     {
         PropertiesConfiguration config = new PropertiesConfiguration();
-        assertNull("Wrong result", config.getArray(Integer.class, KEY_PREFIX));
+        assertNull("Wrong result", config.get(Integer[].class, KEY_PREFIX));
     }
 
     /**
-     * Tests getArray() for an unknown property if a default value is provided.
+     * Tests get() for an unknown array property if a default value is provided.
      */
     @Test
     public void testGetArrayUnknownWithDefault()
@@ -809,7 +808,7 @@ public class TestAbstractConfigurationBa
                 1, 2, 3
         };
         assertArrayEquals("Wrong result", defValue,
-                (int[]) config.getArray(Integer.TYPE, KEY_PREFIX, defValue));
+                config.get(int[].class, KEY_PREFIX, defValue));
     }
 
     /**



Mime
View raw message