Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion owner/src/main/java/org/aeonbits/owner/Converters.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object> collection = Arrays.asList(array);
Collection<Object> result = instantiateCollection(targetType);
Collection<Object> result;
if (getGenericType(targetMethod).isEnum() && targetType.equals(EnumSet.class)) {
result = (Collection<Object>) instantiateEnumSet((Class<? extends Enum>) getGenericType(targetMethod));
} else {
result = instantiateCollection(targetType);
}
result.addAll(collection);
return result;
}
Expand Down Expand Up @@ -105,6 +111,14 @@ private <T> Collection<T> instantiateCollectionFromClass(Class<? extends T> targ
}
}

private <E extends Enum<E>> Collection<?> instantiateEnumSet(Class<E> targetType) {
try {
return EnumSet.noneOf(targetType);
} catch (Exception e) {
throw unsupported(e, "Cannot instantiate enumset of type '%s'", targetType.getCanonicalName());
}
}

private <T> Collection<T> instantiateCollectionFromInterface(Class<? extends T> targetType) {
if (List.class.isAssignableFrom(targetType))
return new ArrayList<T>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -61,6 +53,13 @@ public interface CollectionConfig extends Config {

@DefaultValue(INTEGERS)
CollectionWithoutDefaultConstructor<Integer> badCollection();

@DefaultValue("ONE")
EnumSet<EnumTest> enumSet();

enum EnumTest {
ONE, TWO, THREE
}
}

static class CollectionWithoutDefaultConstructor<E> extends ArrayList<E> {
Expand Down Expand Up @@ -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));
}

}