-
Notifications
You must be signed in to change notification settings - Fork 10
/
ObjectInstantiator.java
43 lines (33 loc) · 1.64 KB
/
ObjectInstantiator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package br.com.caelum.iogi;
import br.com.caelum.iogi.exceptions.InvalidTypeException;
import br.com.caelum.iogi.parameters.Parameters;
import br.com.caelum.iogi.reflection.Target;
import br.com.caelum.iogi.spi.DependencyProvider;
import br.com.caelum.iogi.spi.ParameterNamesProvider;
public class ObjectInstantiator implements Instantiator<Object> {
private final Instantiator<Object> argumentInstantiator;
private final DependenciesInjector dependenciesInjector;
private final ParameterNamesProvider parameterNamesProvider;
public ObjectInstantiator(final Instantiator<Object> argumentInstantiator, final DependencyProvider dependencyProvider, final ParameterNamesProvider parameterNamesProvider) {
this.argumentInstantiator = argumentInstantiator;
this.dependenciesInjector = new DependenciesInjector(dependencyProvider);
this.parameterNamesProvider = parameterNamesProvider;
}
public boolean isAbleToInstantiate(final Target<?> target) {
return true;
}
public Object instantiate(final Target<?> target, final Parameters parameters) {
expectingAConcreteTarget(target);
final Parameters parametersForTarget = parameters.focusedOn(target);
return target
.constructors(parameterNamesProvider, dependenciesInjector)
.compatibleWith(parametersForTarget)
.largest()
.instantiate(argumentInstantiator)
.valueWithPropertiesSet();
}
private <T> void expectingAConcreteTarget(final Target<T> target) {
if (!target.isInstantiable())
throw new InvalidTypeException("Cannot instantiate abstract type %s", target.getClassType());
}
}