diff --git a/src/main/java/com/typesafe/config/Config.java b/src/main/java/com/typesafe/config/Config.java index 8a69d587..5576a1de 100644 --- a/src/main/java/com/typesafe/config/Config.java +++ b/src/main/java/com/typesafe/config/Config.java @@ -81,13 +81,22 @@ public final class Config { } public static ConfigRoot emptyRoot(String rootPath) { - return ConfigImpl.emptyRoot(rootPath); + return emptyRoot(rootPath, null); } public static ConfigObject empty() { - return ConfigImpl.empty(); + return empty(null); } + public static ConfigRoot emptyRoot(String rootPath, String originDescription) { + return ConfigImpl.emptyRoot(rootPath, originDescription); + } + + public static ConfigObject empty(String originDescription) { + return ConfigImpl.empty(originDescription); + } + + public static ConfigRoot systemPropertiesRoot(String rootPath) { return ConfigImpl.systemPropertiesRoot(rootPath); } @@ -250,7 +259,7 @@ public final class Config { * ConfigObject interface. The units parsed are interpreted as powers of * two, that is, the convention for memory rather than the convention for * disk space. - * + * * @param input * the string to parse * @param originForException diff --git a/src/main/java/com/typesafe/config/impl/ConfigImpl.java b/src/main/java/com/typesafe/config/impl/ConfigImpl.java index 0db0fe7c..e9270a2b 100644 --- a/src/main/java/com/typesafe/config/impl/ConfigImpl.java +++ b/src/main/java/com/typesafe/config/impl/ConfigImpl.java @@ -111,14 +111,17 @@ public class ConfigImpl { } /** For use ONLY by library internals, DO NOT TOUCH not guaranteed ABI */ - public static ConfigRoot emptyRoot(String rootPath) { - return SimpleConfigObject.empty(new SimpleConfigOrigin(rootPath)) + public static ConfigRoot emptyRoot(String rootPath, String originDescription) { + String desc = originDescription != null ? originDescription : rootPath; + return SimpleConfigObject.empty(new SimpleConfigOrigin(desc)) .asRoot( Parser.parsePath(rootPath)); } - public static ConfigObject empty() { - return SimpleConfigObject.empty(); + public static ConfigObject empty(String originDescription) { + return SimpleConfigObject + .empty(originDescription != null ? new SimpleConfigOrigin( + originDescription) : null); } /** For use ONLY by library internals, DO NOT TOUCH not guaranteed ABI */ diff --git a/src/main/java/com/typesafe/config/impl/SimpleConfigObject.java b/src/main/java/com/typesafe/config/impl/SimpleConfigObject.java index b5fda36f..3a30fb09 100644 --- a/src/main/java/com/typesafe/config/impl/SimpleConfigObject.java +++ b/src/main/java/com/typesafe/config/impl/SimpleConfigObject.java @@ -113,16 +113,20 @@ final class SimpleConfigObject extends AbstractConfigObject { return new HashSet(value.values()); } - final static String EMPTY_NAME = "empty config"; + final private static String EMPTY_NAME = "empty config"; + final private static SimpleConfigObject emptyInstance = empty(new SimpleConfigOrigin( + EMPTY_NAME)); final static SimpleConfigObject empty() { - return new SimpleConfigObject(new SimpleConfigOrigin(EMPTY_NAME), - Collections. emptyMap()); + return emptyInstance; } final static SimpleConfigObject empty(ConfigOrigin origin) { - return new SimpleConfigObject(origin, - Collections. emptyMap()); + if (origin == null) + return empty(); + else + return new SimpleConfigObject(origin, + Collections. emptyMap()); } final static SimpleConfigObject emptyMissing(ConfigOrigin baseOrigin) { diff --git a/src/test/scala/com/typesafe/config/impl/PublicApiTest.scala b/src/test/scala/com/typesafe/config/impl/PublicApiTest.scala index d14ec94a..6385afc6 100644 --- a/src/test/scala/com/typesafe/config/impl/PublicApiTest.scala +++ b/src/test/scala/com/typesafe/config/impl/PublicApiTest.scala @@ -71,4 +71,13 @@ class PublicApiTest extends TestUtils { conf.getInt("fromProps.one") } } + + @Test + def emptyObjects() { + assertEquals(0, Config.empty().size()) + assertEquals(0, Config.empty("foo").size()) + assertEquals("foo", Config.empty("foo").origin().description()) + assertEquals(0, Config.emptyRoot("foo.bar").size()) + assertEquals("foo.bar", Config.emptyRoot("foo.bar").origin().description()) + } }