-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathInstantiable.java
More file actions
54 lines (44 loc) · 2.66 KB
/
Instantiable.java
File metadata and controls
54 lines (44 loc) · 2.66 KB
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
44
45
46
47
48
49
50
51
52
53
54
package pl.pojo.tester.internal.instantiator;
import org.apache.commons.collections4.MultiValuedMap;
import pl.pojo.tester.api.ConstructorParameters;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static pl.pojo.tester.internal.instantiator.Instantiator.INSTANTIATORS;
public final class Instantiable {
private Instantiable() {
}
static Object[] instantiateClasses(final Class<?>[] classes,
final MultiValuedMap<Class<?>, ConstructorParameters> constructorParameters) {
return Arrays.stream(classes)
.map(clazz -> Instantiable.forClass(clazz, constructorParameters))
.map(AbstractObjectInstantiator::instantiate)
.toArray();
}
static AbstractObjectInstantiator forClass(final Class<?> clazz,
final MultiValuedMap<Class<?>, ConstructorParameters> constructorParameters) {
return instantiateInstantiators(clazz, constructorParameters).stream()
.filter(AbstractObjectInstantiator::canInstantiate)
.findFirst()
.orElseThrow(() -> new RuntimeException("No instantiator found for class " + clazz.getName()));
}
private static List<AbstractObjectInstantiator> instantiateInstantiators(final Class<?> clazz,
final MultiValuedMap<Class<?>, ConstructorParameters> constructorParameters) {
final List<AbstractObjectInstantiator> instantiators = new ArrayList<>();
try {
for (final Class<? extends AbstractObjectInstantiator> instantiator : INSTANTIATORS) {
final Constructor<? extends AbstractObjectInstantiator> constructor =
instantiator.getDeclaredConstructor(Class.class, MultiValuedMap.class);
constructor.setAccessible(true);
final AbstractObjectInstantiator abstractObjectInstantiator = constructor.newInstance(clazz,
constructorParameters);
instantiators.add(abstractObjectInstantiator);
}
} catch (final Exception e) {
throw new RuntimeException("Cannot load instantiators form pl.pojo.tester.internal.instantiator package.",
e);
}
return instantiators;
}
}