javaarraysproperties-fileapache-commons-config

What's wrong with Commons Configuration AbstractConfiguration's get() and getArray()?


Using commons-configuration2:2.4.

my.properties:

arrayOfSingle=0xA
arrayOfMultiples=0xD,0xA

 

CONFIG.getArray(int[].class, "arrayOfSingle"));

  org.apache.commons.configuration2.ex.ConversionException:
    The value '0xA' (class java.lang.String) can't be converted to a [I object

CONFIG.getArray(int[].class, "arrayOfMultiples"));

  org.apache.commons.configuration2.ex.ConversionException:
    The value '0xD,0xA' (class java.lang.String) can't be converted to a [I object

CONFIG.getArray(Integer[].class, "arrayOfSingle"));

  org.apache.commons.configuration2.ex.ConversionException: 
    The value '0xA' (class java.lang.String) can't be converted to a [Ljava.lang.Integer; object

CONFIG.getArray(Integer[].class, "arrayOfMultiples"));

  org.apache.commons.configuration2.ex.ConversionException:
    The value '0xD,0xA' (class java.lang.String) can't be converted to a [Ljava.lang.Integer; object

CONFIG.get(int[].class, "arrayOfSingle") // prints "[I@7dba7035", i.e.works

CONFIG.get(int[].class, "arrayOfMultiples")

  org.apache.commons.configuration2.ex.ConversionException:
    Could not convert 0xD,0xA to java.lang.Integer! Invalid hex number.

CONFIG.get(Integer[].class, "arrayOfSingle") // prints "[Ljava.lang.Integer;@44d379bb", i.e. works

CONFIG.get(Integer[].class, "arrayOfMultiples")

  org.apache.commons.configuration2.ex.ConversionException:
    Could not convert 0xD,0xA to java.lang.Integer! Invalid hex number.

getArray(Class<?> cls, String key, Object defaultValue) is deprecated (the use of get(Class<T> cls, String key, T defaultValue) is recommended) but getArray(Class<?> cls, String key) isn't and get(Class<T> cls, String key) doesn't work either.


Solution

  • I hope this will be useful for those who want to have properties comma-separated as shown below and get List or Array of values.
    Even this is accepted but if you do not like multiple entries of the same keys then the following solution works.

    Add Dependency:

    compile("org.apache.commons:commons-configuration2:2.7")
    

    application.properties file:

    arrayOfMultiples=0xD,0xA
    

    Client:

    PropertiesConfiguration propertiesConfiguration = new FileBasedConfigurationBuilder<>(PropertiesConfiguration.class)
            .configure(new Parameters()
                    .properties()
                    .setFileName("application.properties-file-path")
                    .setListDelimiterHandler(new DefaultListDelimiterHandler(',')))
            .getConfiguration();
    

    output:

    propertiesConfiguration.get(Integer[].class, "arrayOfMultiples");
    // or
    propertiesConfiguration.get(int[].class, "arrayOfMultiples"))
    

    [13, 10]

    NOTE: since values are in hexadecimal, the converter converts it to decimal. But works for any int values.

    Also works for the following types of read when you need List

    propertiesConfiguration.getList(int.class, "arrayOfMultiples");
    // or
    propertiesConfiguration.getList(Integer.class, "arrayOfMultiples");