|
5 | 5 | */ |
6 | 6 | package io.jooby.internal.converter; |
7 | 7 |
|
8 | | -import io.jooby.Usage; |
9 | | -import io.jooby.exception.BadRequestException; |
10 | | -import io.jooby.BeanConverter; |
11 | | -import io.jooby.FileUpload; |
12 | | -import io.jooby.exception.MissingValueException; |
13 | | -import io.jooby.Multipart; |
14 | | -import io.jooby.exception.ProvisioningException; |
15 | | -import io.jooby.ValueNode; |
16 | | -import io.jooby.internal.reflect.$Types; |
| 8 | +import static io.jooby.SneakyThrows.propagate; |
17 | 9 |
|
18 | | -import javax.annotation.Nonnull; |
19 | | -import javax.inject.Inject; |
20 | | -import javax.inject.Named; |
21 | 10 | import java.lang.annotation.Annotation; |
22 | 11 | import java.lang.reflect.Constructor; |
23 | 12 | import java.lang.reflect.Executable; |
|
32 | 21 | import java.util.Set; |
33 | 22 | import java.util.function.Consumer; |
34 | 23 |
|
35 | | -import static io.jooby.SneakyThrows.propagate; |
| 24 | +import javax.annotation.Nonnull; |
| 25 | +import javax.inject.Inject; |
| 26 | +import javax.inject.Named; |
| 27 | + |
| 28 | +import io.jooby.BeanConverter; |
| 29 | +import io.jooby.FileUpload; |
| 30 | +import io.jooby.Multipart; |
| 31 | +import io.jooby.Usage; |
| 32 | +import io.jooby.ValueNode; |
| 33 | +import io.jooby.exception.BadRequestException; |
| 34 | +import io.jooby.exception.MissingValueException; |
| 35 | +import io.jooby.exception.ProvisioningException; |
| 36 | +import io.jooby.internal.reflect.$Types; |
36 | 37 |
|
37 | 38 | public class ReflectiveBeanConverter implements BeanConverter { |
38 | 39 | private static final String AMBIGUOUS_CONSTRUCTOR = |
@@ -72,27 +73,33 @@ private static <T> T newInstance(Class<T> type, ValueNode node) |
72 | 73 | } |
73 | 74 |
|
74 | 75 | private static Constructor selectConstructor(Constructor[] constructors) { |
75 | | - Constructor result = null; |
76 | 76 | if (constructors.length == 1) { |
77 | | - result = constructors[0]; |
| 77 | + return constructors[0]; |
78 | 78 | } else { |
| 79 | + Constructor injectConstructor = null; |
| 80 | + Constructor defaultConstructor = null; |
79 | 81 | for (Constructor constructor : constructors) { |
80 | 82 | if (Modifier.isPublic(constructor.getModifiers())) { |
81 | 83 | Annotation inject = constructor.getAnnotation(Inject.class); |
82 | | - if (inject != null) { |
83 | | - if (result == null) { |
84 | | - result = constructor; |
| 84 | + if (inject == null) { |
| 85 | + if (constructor.getParameterCount() == 0) { |
| 86 | + defaultConstructor = constructor; |
| 87 | + } |
| 88 | + } else { |
| 89 | + if (injectConstructor == null) { |
| 90 | + injectConstructor = constructor; |
85 | 91 | } else { |
86 | 92 | throw new IllegalStateException(AMBIGUOUS_CONSTRUCTOR); |
87 | 93 | } |
88 | 94 | } |
89 | 95 | } |
90 | 96 | } |
| 97 | + Constructor result = injectConstructor == null ? defaultConstructor : injectConstructor; |
| 98 | + if (result == null) { |
| 99 | + throw new IllegalStateException(AMBIGUOUS_CONSTRUCTOR); |
| 100 | + } |
| 101 | + return result; |
91 | 102 | } |
92 | | - if (result == null) { |
93 | | - throw new IllegalStateException(AMBIGUOUS_CONSTRUCTOR); |
94 | | - } |
95 | | - return result; |
96 | 103 | } |
97 | 104 |
|
98 | 105 | public static Object[] inject(ValueNode scope, Executable method, Consumer<ValueNode> state) { |
|
0 commit comments