From 37e2dd321960b293a57b336e922b9280044a8d40 Mon Sep 17 00:00:00 2001 From: jason Date: Wed, 24 Jun 2015 07:08:15 +0930 Subject: [PATCH] Adds the ability for ConfigBeanImpl to handle a List of Beans. Add test entry to beanconfig01.conf, ArraysConfig and ConfigBeanFactoryTest. --- .../com/typesafe/config/impl/ConfigBeanImpl.java | 9 ++++++++- config/src/test/java/beanconfig/ArraysConfig.java | 9 +++++++++ .../src/test/resources/beanconfig/beanconfig01.conf | 12 +++++++++++- .../typesafe/config/impl/ConfigBeanFactoryTest.scala | 9 +++++++++ 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/config/src/main/java/com/typesafe/config/impl/ConfigBeanImpl.java b/config/src/main/java/com/typesafe/config/impl/ConfigBeanImpl.java index b383efe3..d4ec98df 100644 --- a/config/src/main/java/com/typesafe/config/impl/ConfigBeanImpl.java +++ b/config/src/main/java/com/typesafe/config/impl/ConfigBeanImpl.java @@ -170,7 +170,7 @@ public class ConfigBeanImpl { private static Object getListValue(Class beanClass, Type parameterType, Class parameterClass, Config config, String configPropName) { Type elementType = ((ParameterizedType)parameterType).getActualTypeArguments()[0]; - + if (elementType == Boolean.class) { return config.getBooleanList(configPropName); } else if (elementType == Integer.class) { @@ -193,6 +193,13 @@ public class ConfigBeanImpl { return config.getObjectList(configPropName); } else if (elementType == ConfigValue.class) { return config.getList(configPropName); + } else if (hasAtLeastOneBeanProperty((Class) elementType)) { + List beanList = new ArrayList<>(); + List configList = config.getConfigList(configPropName); + for (Config listMember : configList) { + beanList.add(createInternal(listMember, (Class) elementType)); + } + return beanList; } else { throw new ConfigException.BadBean("Bean property '" + configPropName + "' of class " + beanClass.getName() + " has unsupported list element type " + elementType); } diff --git a/config/src/test/java/beanconfig/ArraysConfig.java b/config/src/test/java/beanconfig/ArraysConfig.java index 7369a278..0b5122d7 100644 --- a/config/src/test/java/beanconfig/ArraysConfig.java +++ b/config/src/test/java/beanconfig/ArraysConfig.java @@ -23,6 +23,7 @@ public class ArraysConfig { List ofConfigValue; List ofDuration; List ofMemorySize; + List ofStringBean; public List getEmpty() { return empty; @@ -127,4 +128,12 @@ public class ArraysConfig { public void setOfMemorySize(List ofMemorySize) { this.ofMemorySize = ofMemorySize; } + + public List getOfStringBean() { + return ofStringBean; + } + + public void setOfStringBean(List ofStringBean) { + this.ofStringBean = ofStringBean; + } } diff --git a/config/src/test/resources/beanconfig/beanconfig01.conf b/config/src/test/resources/beanconfig/beanconfig01.conf index d17c0468..80b328a9 100644 --- a/config/src/test/resources/beanconfig/beanconfig01.conf +++ b/config/src/test/resources/beanconfig/beanconfig01.conf @@ -52,7 +52,17 @@ "ofConfigObject" : [${numbers}, ${booleans}, ${strings}], "ofConfigValue" : [1, 2, "a"], "ofDuration" : [1, 2h, 3 days], - "ofMemorySize" : [1024, 1M, 1G] + "ofMemorySize" : [1024, 1M, 1G], + "ofStringBean" : [ + { + abcd : "testAbcdOne" + yes : "testYesOne" + }, + { + abcd : "testAbcdTwo" + yes : "testYesTwo" + } + ] }, "bytes" : { "kilobyte" : "1kB", diff --git a/config/src/test/scala/com/typesafe/config/impl/ConfigBeanFactoryTest.scala b/config/src/test/scala/com/typesafe/config/impl/ConfigBeanFactoryTest.scala index 2c9dc8ac..7d9a61c1 100644 --- a/config/src/test/scala/com/typesafe/config/impl/ConfigBeanFactoryTest.scala +++ b/config/src/test/scala/com/typesafe/config/impl/ConfigBeanFactoryTest.scala @@ -111,6 +111,15 @@ class ConfigBeanFactoryTest extends TestUtils { ConfigMemorySize.ofBytes(1048576), ConfigMemorySize.ofBytes(1073741824)), beanConfig.getOfMemorySize.asScala) + + val stringsConfigOne = new StringsConfig(); + stringsConfigOne.setAbcd("testAbcdOne") + stringsConfigOne.setYes("testYesOne") + val stringsConfigTwo = new StringsConfig(); + stringsConfigTwo.setAbcd("testAbcdTwo") + stringsConfigTwo.setYes("testYesTwo") + + assertEquals(List(stringsConfigOne, stringsConfigTwo), beanConfig.getOfStringBean) } @Test