Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
IInjector |
|
| 1.0;1 |
1 | /** | |
2 | Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | contributor license agreements. See the NOTICE file distributed with | |
4 | this work for additional information regarding copyright ownership. | |
5 | The ASF licenses this file to You under the Apache License, Version 2.0 | |
6 | (the "License"); you may not use this file except in compliance with | |
7 | the License. You may obtain a copy of the License at | |
8 | ||
9 | http://www.apache.org/licenses/LICENSE-2.0 | |
10 | ||
11 | Unless required by applicable law or agreed to in writing, software | |
12 | distributed under the License is distributed on an "AS IS" BASIS, | |
13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | See the License for the specific language governing permissions and | |
15 | limitations under the License. | |
16 | */ | |
17 | package org.apache.commons.inject.api; | |
18 | ||
19 | /** | |
20 | * The {@link IInjector injector} is used to build objects for your application. | |
21 | * It does so by maintaining a map of {@link IKey keys} and associated | |
22 | * {@link IBinding bindings}. The map is created by {@link IModule modules}. | |
23 | * | |
24 | * Some bindings are present automatically: | |
25 | * - The {@link Injector} itself. | |
26 | * - For any {@link IBinding IBinding<T>} (binding of type T), there is also | |
27 | * a {@link IProvider provider} of type T. | |
28 | * | |
29 | * To create an {@link IInjector injector}, use the class {@link CommonsInject}. | |
30 | */ | |
31 | public interface IInjector { | |
32 | /** | |
33 | * Returns an instance of {@code pType}, if a matching binding is present. | |
34 | * This is a shortcut for | |
35 | * <pre> | |
36 | * getInstance(pType, "") | |
37 | * </pre> | |
38 | * or | |
39 | * <pre> | |
40 | * getInstance(pType, Key.NO_NAME) | |
41 | * </pre> | |
42 | * @param pType The requested type. | |
43 | * @return The created instance, or null. | |
44 | * @see #getInstance(Class, String) | |
45 | * @see #getInstance(IKey) | |
46 | * @see #requireInstance(Class) | |
47 | */ | |
48 | <T> T getInstance(Class<T> pType); | |
49 | /** | |
50 | * Returns an instance of the given type, with the given name, if a | |
51 | * matching binding is present. This is a shortcut for | |
52 | * <pre> | |
53 | * Key key = new Key(pType, pName); | |
54 | * getInstance(key) | |
55 | * </pre> | |
56 | * or | |
57 | * <pre> | |
58 | * Key key = new Key(pType, pName, Key.NO_ANNOTATIONS); | |
59 | * getInstance(key) | |
60 | * </pre> | |
61 | * @param pType The requested type. | |
62 | * @param pName The requested objects name. | |
63 | * @return The created instance, or null. | |
64 | * @see #getInstance(IKey) | |
65 | * @see #requireInstance(Class, String) | |
66 | */ | |
67 | <T> T getInstance(Class<T> pType, String pName); | |
68 | /** | |
69 | * Returns an instance of the binding that has been registered for the | |
70 | * given key. | |
71 | * @param pKey A binding key, for which a binding has been registered. | |
72 | * @return The created instance, or null. | |
73 | * @see #getInstance(Class) | |
74 | * @see #getInstance(Class, String) | |
75 | * @see #requireInstance(IKey) | |
76 | */ | |
77 | <T> T getInstance(IKey<T> pKey); | |
78 | ||
79 | /** | |
80 | * Returns an instance of {@code pType}, if a matching binding is present. | |
81 | * This is a shortcut for | |
82 | * <pre> | |
83 | * requireInstance(pType, "") | |
84 | * </pre> | |
85 | * or | |
86 | * <pre> | |
87 | * requireInstance(pType, Key.NO_NAME) | |
88 | * </pre> | |
89 | * @param pType The requested type. | |
90 | * @return The created instance. | |
91 | * @throws NoSuchBindingException No matching binding has been registered with | |
92 | * the injector. | |
93 | * @see #getInstance(Class, String) | |
94 | * @see #getInstance(IKey) | |
95 | * @see #requireInstance(Class) | |
96 | */ | |
97 | <T> T requireInstance(Class<T> pType) throws NoSuchBindingException; | |
98 | /** | |
99 | * Returns an instance of the given type, with the given name, if a | |
100 | * matching binding is present. This is a shortcut for | |
101 | * <pre> | |
102 | * Key key = new Key(pType, pName); | |
103 | * requireInstance(key) | |
104 | * </pre> | |
105 | * or | |
106 | * <pre> | |
107 | * Key key = new Key(pType, pName, Key.NO_ANNOTATIONS); | |
108 | * requireInstance(key) | |
109 | * </pre> | |
110 | * @param pType The requested type. | |
111 | * @param pName The requested objects name. | |
112 | * @return The created instance. | |
113 | * @throws NoSuchBindingException No matching binding has been registered with | |
114 | * the injector. | |
115 | * @see #getInstance(Class, String) | |
116 | * @see #requireInstance(Class) | |
117 | * @see #requireInstance(IKey) | |
118 | */ | |
119 | <T> T requireInstance(Class<T> pType, String pName) throws NoSuchBindingException; | |
120 | /** | |
121 | * Returns an instance of the binding that has been registered for the | |
122 | * given key. | |
123 | * @param pKey A binding key, for which a binding has been registered. | |
124 | * @return The created instance. | |
125 | * @throws NoSuchBindingException No matching binding has been registered with | |
126 | * the injector. | |
127 | * @see #getInstance(IKey) | |
128 | * @see #requireInstance(Class) | |
129 | * @see #requireInstance(Class, String) | |
130 | */ | |
131 | <T> T requireInstance(IKey<T> pKey) throws NoSuchBindingException; | |
132 | ||
133 | /** | |
134 | * Injects members into the given instance, as if it where created by | |
135 | * the {@link IInjector injector} itself. In other words, fills fields | |
136 | * and invokes methods annotated with @Inject, assuming that a binding | |
137 | * is present for those fields, and method parameters. | |
138 | */ | |
139 | void injectMembers(Object pInstance); | |
140 | } |