Browse Source

Merge pull request #46 from greboid/dev2

Delete SimpleInjector.
pull/48/head
Chris Smith 9 years ago
parent
commit
bcfc945be9
2 changed files with 0 additions and 289 deletions
  1. 0
    162
      src/com/dmdirc/util/SimpleInjector.java
  2. 0
    127
      test/com/dmdirc/util/SimpleInjectorTest.java

+ 0
- 162
src/com/dmdirc/util/SimpleInjector.java View File

@@ -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
-}

+ 0
- 127
test/com/dmdirc/util/SimpleInjectorTest.java View File

@@ -1,127 +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
20
- * THE SOFTWARE.
21
- */
22
-package com.dmdirc.util;
23
-
24
-import java.io.Serializable;
25
-
26
-import org.junit.Test;
27
-
28
-import static org.junit.Assert.*;
29
-
30
-public class SimpleInjectorTest {
31
-
32
-    @Test
33
-    public void testNoParams() {
34
-        final SimpleInjector instance = new SimpleInjector();
35
-        assertTrue(instance.getParameters().isEmpty());
36
-    }
37
-
38
-    @Test
39
-    public void testAddParams() {
40
-        final Object object = new Object();
41
-        final SimpleInjector instance = new SimpleInjector();
42
-        instance.addParameter(Object.class, object);
43
-        assertEquals(1, instance.getParameters().size());
44
-    }
45
-
46
-    @Test
47
-    public void testAddObjectsParams() {
48
-        final TestObject object = new TestObject("");
49
-        final SimpleInjector instance = new SimpleInjector();
50
-        instance.addParameter(object);
51
-        assertEquals(3, instance.getParameters().size());
52
-    }
53
-
54
-    @Test
55
-    public void testParentsParams() {
56
-        final Object object = new Object();
57
-        final SimpleInjector parent = new SimpleInjector();
58
-        parent.addParameter(Object.class, object);
59
-        final SimpleInjector child = new SimpleInjector(parent);
60
-        assertEquals(1, child.getParameters().size());
61
-    }
62
-
63
-    @Test
64
-    public void testCreateInstanceNoParams() {
65
-        final SimpleInjector instance = new SimpleInjector();
66
-        final Object result = instance.createInstance(Object.class);
67
-        assertNotNull(result);
68
-    }
69
-
70
-    @Test
71
-    public void testCreateInstanceParams() {
72
-        final SimpleInjector instance = new SimpleInjector();
73
-        instance.addParameter(String.class, "test");
74
-        instance.addParameter(SimpleInjectorTest.class, this);
75
-        final TestObject result = instance.createInstance(TestObject.class);
76
-        assertNotNull(result);
77
-    }
78
-
79
-    @Test(expected=IllegalArgumentException.class)
80
-    public void testCreateUnsatisfiedConstructor() {
81
-        final SimpleInjector instance = new SimpleInjector();
82
-        final Object result = instance.createInstance(TestObject.class);
83
-        assertNull(result);
84
-    }
85
-
86
-    @Test(expected=IllegalArgumentException.class)
87
-    public void testCreatePrivateConstructor() {
88
-        final SimpleInjector instance = new SimpleInjector();
89
-        instance.addParameter(String.class, "");
90
-        final Object result = instance.createInstance(PrivateObject.class);
91
-        assertNull(result);
92
-    }
93
-
94
-    @Test(expected=IllegalArgumentException.class)
95
-    public void testCreateExceptionConstructor() {
96
-        final SimpleInjector instance = new SimpleInjector();
97
-        instance.addParameter(String.class, "");
98
-        instance.addParameter(SimpleInjectorTest.class, this);
99
-        final Object result = instance.createInstance(ExceptionObject.class);
100
-        assertNull(result);
101
-    }
102
-
103
-    static class TestObject implements Serializable {
104
-
105
-        private static final long serialVersionUID = 1L;
106
-
107
-        public TestObject(final String test) { // NOPMD
108
-        }
109
-    }
110
-
111
-    static class PrivateObject implements Serializable {
112
-
113
-        private static final long serialVersionUID = 1L;
114
-
115
-        private PrivateObject() {
116
-        }
117
-    }
118
-
119
-    static class ExceptionObject implements Serializable {
120
-
121
-        private static final long serialVersionUID = 1L;
122
-
123
-        public ExceptionObject() {
124
-            throw new IndexOutOfBoundsException();
125
-        }
126
-    }
127
-}

Loading…
Cancel
Save