|
@@ -1,162 +0,0 @@
|
1
|
|
-/*
|
2
|
|
- * Copyright (c) 2006-2015 DMDirc Developers
|
3
|
|
- *
|
4
|
|
- * Permission is hereby granted, free of charge, to any person obtaining a copy
|
5
|
|
- * of this software and associated documentation files (the "Software"), to deal
|
6
|
|
- * in the Software without restriction, including without limitation the rights
|
7
|
|
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
|
- * copies of the Software, and to permit persons to whom the Software is
|
9
|
|
- * furnished to do so, subject to the following conditions:
|
10
|
|
- *
|
11
|
|
- * The above copyright notice and this permission notice shall be included in
|
12
|
|
- * all copies or substantial portions of the Software.
|
13
|
|
- *
|
14
|
|
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
15
|
|
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
16
|
|
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
17
|
|
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
18
|
|
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
19
|
|
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
20
|
|
- * SOFTWARE.
|
21
|
|
- */
|
22
|
|
-
|
23
|
|
-package com.dmdirc.util;
|
24
|
|
-
|
25
|
|
-import java.lang.reflect.Constructor;
|
26
|
|
-import java.util.HashMap;
|
27
|
|
-import java.util.Map;
|
28
|
|
-
|
29
|
|
-/**
|
30
|
|
- * Facilitates simple injection of parameters into a constructor.
|
31
|
|
- * <p>
|
32
|
|
- * Injectors may be given a set of parent injectors, from which they will
|
33
|
|
- * inherit parameters. No checking is performed to prevent circular references
|
34
|
|
- * within parents.
|
35
|
|
- */
|
36
|
|
-public class SimpleInjector {
|
37
|
|
-
|
38
|
|
- /** The parent injectors, if any. */
|
39
|
|
- private final SimpleInjector[] parents;
|
40
|
|
-
|
41
|
|
- /** A mapping of known classes to the objects that should be injected. */
|
42
|
|
- private final Map<Class<?>, Object> parameters = new HashMap<>();
|
43
|
|
-
|
44
|
|
- /**
|
45
|
|
- * Creates a new injector which will inherit injection parameters from
|
46
|
|
- * the specified parent. Parent graphs must be acyclic.
|
47
|
|
- *
|
48
|
|
- * @param parents The injectors to inherit parameters from.
|
49
|
|
- */
|
50
|
|
- public SimpleInjector(final SimpleInjector ... parents) {
|
51
|
|
- this.parents = new SimpleInjector[parents.length];
|
52
|
|
- System.arraycopy(parents, 0, this.parents, 0, parents.length);
|
53
|
|
- }
|
54
|
|
-
|
55
|
|
- /**
|
56
|
|
- * Adds a new injectable object to this injector. If a constructor requires
|
57
|
|
- * an instance of the specified class, it will be given the specified
|
58
|
|
- * object. If an association has already been made for the specified class,
|
59
|
|
- * it will be replaced with the new object.
|
60
|
|
- *
|
61
|
|
- * @param clazz The type of parameter which should be injected
|
62
|
|
- * @param object The value to inject for the parameter
|
63
|
|
- */
|
64
|
|
- public void addParameter(final Class<?> clazz, final Object object) {
|
65
|
|
- parameters.put(clazz, object);
|
66
|
|
- }
|
67
|
|
-
|
68
|
|
- /**
|
69
|
|
- * Adds the specified object as an injectable parameter for all of its
|
70
|
|
- * known classes and interface. This is equivalent to calling
|
71
|
|
- * {@link #addParameter(Class, Object)} for the object's
|
72
|
|
- * class, all superclasses, and all interfaces.
|
73
|
|
- *
|
74
|
|
- * @param object The object to be injected
|
75
|
|
- */
|
76
|
|
- public void addParameter(final Object object) {
|
77
|
|
- // Iterate the object hierarchy up
|
78
|
|
- Class<?> target = object.getClass();
|
79
|
|
- do {
|
80
|
|
- addParameter(target, object);
|
81
|
|
-
|
82
|
|
- // Add all interfaces
|
83
|
|
- for (Class<?> iface : target.getInterfaces()) {
|
84
|
|
- addParameter(iface, object);
|
85
|
|
- }
|
86
|
|
-
|
87
|
|
- target = target.getSuperclass();
|
88
|
|
- } while (target != null);
|
89
|
|
- }
|
90
|
|
-
|
91
|
|
- /**
|
92
|
|
- * Retrieves a mapping of known injectable types to their corresponding
|
93
|
|
- * values. This mapping will also include mappings defined in all of this
|
94
|
|
- * injector's parents.
|
95
|
|
- *
|
96
|
|
- * @return A map of injectable parameters
|
97
|
|
- */
|
98
|
|
- public Map<Class<?>, Object> getParameters() {
|
99
|
|
- final Map<Class<?>, Object> localParams = new HashMap<>(parameters.size());
|
100
|
|
-
|
101
|
|
- for (SimpleInjector parent : parents) {
|
102
|
|
- localParams.putAll(parent.getParameters());
|
103
|
|
- }
|
104
|
|
-
|
105
|
|
- // Note: insert local parameters after so they override parent params.
|
106
|
|
- localParams.putAll(parameters);
|
107
|
|
-
|
108
|
|
- return localParams;
|
109
|
|
- }
|
110
|
|
-
|
111
|
|
- /**
|
112
|
|
- * Attempts to create a new instance of the specified class by injecting
|
113
|
|
- * parameters into the constructor(s). If no constructors are found which
|
114
|
|
- * can be satisfied by the known parameters, or if all satisfiable
|
115
|
|
- * constructors throw exceptions, an IllegalArgumentException will be
|
116
|
|
- * thrown.
|
117
|
|
- * <p>
|
118
|
|
- * If multiple constructors are available that are satisfiable using the
|
119
|
|
- * known arguments, no guarantee is given as to which will be used.
|
120
|
|
- *
|
121
|
|
- * @param <T> The type of object being instantiated
|
122
|
|
- * @param clazz The class to create an instance of
|
123
|
|
- * @return An instance of the specified class
|
124
|
|
- * @throws IllegalArgumentException If the class could not be instantiated
|
125
|
|
- */
|
126
|
|
- @SuppressWarnings("unchecked")
|
127
|
|
- public <T> T createInstance(final Class<T> clazz) {
|
128
|
|
- final Map<Class<?>, Object> params = getParameters();
|
129
|
|
- Throwable throwable = null;
|
130
|
|
-
|
131
|
|
- for (Constructor<?> rawCtor : clazz.getConstructors()) {
|
132
|
|
- final Constructor<T> ctor = (Constructor<T>) rawCtor;
|
133
|
|
- final Object[] args = new Object[ctor.getParameterTypes().length];
|
134
|
|
-
|
135
|
|
- int i = 0;
|
136
|
|
- for (Class<?> paramType : ctor.getParameterTypes()) {
|
137
|
|
- if (params.containsKey(paramType)) {
|
138
|
|
- args[i++] = params.get(paramType);
|
139
|
|
- } else {
|
140
|
|
- break;
|
141
|
|
- }
|
142
|
|
- }
|
143
|
|
-
|
144
|
|
- if (i == args.length) {
|
145
|
|
- try {
|
146
|
|
- return ctor.newInstance(args);
|
147
|
|
- } catch (ReflectiveOperationException ex) {
|
148
|
|
- throwable = ex;
|
149
|
|
- }
|
150
|
|
- }
|
151
|
|
- }
|
152
|
|
-
|
153
|
|
- if (throwable == null) {
|
154
|
|
- throw new IllegalArgumentException("No declared constructors "
|
155
|
|
- + "could be satisfied with known parameters");
|
156
|
|
- }
|
157
|
|
-
|
158
|
|
- throw new IllegalArgumentException("A satisfiable constructor was "
|
159
|
|
- + "found but threw an exception", throwable);
|
160
|
|
- }
|
161
|
|
-
|
162
|
|
-}
|