Skip to content

Commit db8e94c

Browse files
committed
Guice and empty list properties, support for list of int, long, float, double, string and object. Fix #1337
1 parent 9ea19b1 commit db8e94c

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

modules/jooby-guice/src/main/java/io/jooby/di/JoobyModule.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.List;
2525
import java.util.Map;
2626
import java.util.stream.Collectors;
27+
import java.util.stream.Stream;
2728

2829
/**
2930
* Exposes Jooby objects to Guice. This module exposes {@link Environment}, {@link Config} and
@@ -76,14 +77,23 @@ private void configureResources(ServiceRegistry registry) {
7677
Object value = entry.getValue().unwrapped();
7778
if (value instanceof List) {
7879
List values = (List) value;
79-
Type listType = values.size() == 0
80-
? Types.listOf(String.class)
81-
: Types.listOf(values.get(0).getClass());
82-
Key key = Key.get(listType, Names.named(name));
83-
bind(key).toInstance(values);
80+
componentType(values).forEach(componentType -> {
81+
Type listType = Types.listOf(componentType);
82+
Key key = Key.get(listType, Names.named(name));
83+
bind(key).toInstance(values);
84+
});
8485
value = values.stream().map(Object::toString).collect(Collectors.joining(","));
8586
}
8687
bindConstant().annotatedWith(named).to(value.toString());
8788
}
8889
}
90+
91+
private Stream<Class> componentType(List values) {
92+
if (values.isEmpty()) {
93+
// For empty list we generates a binding for primitive wrappers.
94+
return Stream.of(String.class, Integer.class, Long.class, Float.class, Double.class,
95+
Boolean.class, Object.class);
96+
}
97+
return Stream.of(values.get(0).getClass());
98+
}
8999
}

modules/jooby-guice/src/test/java/io/jooby/di/JoobyModuleTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ public void issue1337() {
5757
LinkedBindingBuilder emptyListBinding = mock(LinkedBindingBuilder.class);
5858
emptyListBinding.toInstance(emptyList);
5959
when(binder.bind(Key.get(Types.listOf(String.class), Names.named("some")))).thenReturn(emptyListBinding);
60+
when(binder.bind(Key.get(Types.listOf(Integer.class), Names.named("some")))).thenReturn(emptyListBinding);
61+
when(binder.bind(Key.get(Types.listOf(Long.class), Names.named("some")))).thenReturn(emptyListBinding);
62+
when(binder.bind(Key.get(Types.listOf(Float.class), Names.named("some")))).thenReturn(emptyListBinding);
63+
when(binder.bind(Key.get(Types.listOf(Double.class), Names.named("some")))).thenReturn(emptyListBinding);
64+
when(binder.bind(Key.get(Types.listOf(Boolean.class), Names.named("some")))).thenReturn(emptyListBinding);
65+
when(binder.bind(Key.get(Types.listOf(Object.class), Names.named("some")))).thenReturn(emptyListBinding);
6066

6167
ConstantBindingBuilder constantBindingBuilder = mock(ConstantBindingBuilder.class);
6268
constantBindingBuilder.to("");

0 commit comments

Comments
 (0)