diff --git a/owner/src/main/java/org/aeonbits/owner/Converters.java b/owner/src/main/java/org/aeonbits/owner/Converters.java index 4231c7f4..9d5796dd 100644 --- a/owner/src/main/java/org/aeonbits/owner/Converters.java +++ b/owner/src/main/java/org/aeonbits/owner/Converters.java @@ -64,13 +64,19 @@ Object tryConvert(Method targetMethod, Class targetType, String text) { }, COLLECTION { + @SuppressWarnings("unchecked") @Override Object tryConvert(Method targetMethod, Class targetType, String text) { if (!Collection.class.isAssignableFrom(targetType)) return SKIP; Object[] array = convertToArray(targetMethod, text); Collection collection = Arrays.asList(array); - Collection result = instantiateCollection(targetType); + Collection result; + if (getGenericType(targetMethod).isEnum() && targetType.equals(EnumSet.class)) { + result = (Collection) instantiateEnumSet((Class) getGenericType(targetMethod)); + } else { + result = instantiateCollection(targetType); + } result.addAll(collection); return result; } @@ -105,6 +111,14 @@ private Collection instantiateCollectionFromClass(Class targ } } + private > Collection instantiateEnumSet(Class targetType) { + try { + return EnumSet.noneOf(targetType); + } catch (Exception e) { + throw unsupported(e, "Cannot instantiate enumset of type '%s'", targetType.getCanonicalName()); + } + } + private Collection instantiateCollectionFromInterface(Class targetType) { if (List.class.isAssignableFrom(targetType)) return new ArrayList(); diff --git a/owner/src/test/java/org/aeonbits/owner/typeconversion/collections/CollectionSupportTest.java b/owner/src/test/java/org/aeonbits/owner/typeconversion/collections/CollectionSupportTest.java index 03a5ffa0..45a8fd58 100644 --- a/owner/src/test/java/org/aeonbits/owner/typeconversion/collections/CollectionSupportTest.java +++ b/owner/src/test/java/org/aeonbits/owner/typeconversion/collections/CollectionSupportTest.java @@ -13,20 +13,12 @@ import org.junit.Before; import org.junit.Test; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.LinkedHashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Set; -import java.util.SortedSet; +import java.util.*; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.containsInAnyOrder; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThat; +import static org.junit.Assert.*; /** * @author Dmytro Chyzhykov @@ -61,6 +53,13 @@ public interface CollectionConfig extends Config { @DefaultValue(INTEGERS) CollectionWithoutDefaultConstructor badCollection(); + + @DefaultValue("ONE") + EnumSet enumSet(); + + enum EnumTest { + ONE, TWO, THREE + } } static class CollectionWithoutDefaultConstructor extends ArrayList { @@ -116,5 +115,10 @@ public void itShouldWorkWithRawCollectionAsWithCollectionOfStrings() throws Exce assertEquals(Arrays.asList("1", "2", "3"), cfg.rawCollection()); } + @Test + public void enumSetCreated() throws Exception { + assertTrue(cfg.enumSet().contains(CollectionConfig.EnumTest.ONE)); + } + }