xref: /aosp_15_r20/external/jsr330/src/javax/inject/Inject.java (revision 3ff81872dd771505ae446579ad46cd1fa54f7475)
1*3ff81872SXin Li /*
2*3ff81872SXin Li  * Copyright (C) 2009 The JSR-330 Expert Group
3*3ff81872SXin Li  *
4*3ff81872SXin Li  * Licensed under the Apache License, Version 2.0 (the "License");
5*3ff81872SXin Li  * you may not use this file except in compliance with the License.
6*3ff81872SXin Li  * You may obtain a copy of the License at
7*3ff81872SXin Li  *
8*3ff81872SXin Li  *      http://www.apache.org/licenses/LICENSE-2.0
9*3ff81872SXin Li  *
10*3ff81872SXin Li  * Unless required by applicable law or agreed to in writing, software
11*3ff81872SXin Li  * distributed under the License is distributed on an "AS IS" BASIS,
12*3ff81872SXin Li  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*3ff81872SXin Li  * See the License for the specific language governing permissions and
14*3ff81872SXin Li  * limitations under the License.
15*3ff81872SXin Li  */
16*3ff81872SXin Li 
17*3ff81872SXin Li package javax.inject;
18*3ff81872SXin Li 
19*3ff81872SXin Li import java.lang.annotation.Target;
20*3ff81872SXin Li import java.lang.annotation.Retention;
21*3ff81872SXin Li import java.lang.annotation.Documented;
22*3ff81872SXin Li import static java.lang.annotation.RetentionPolicy.RUNTIME;
23*3ff81872SXin Li import static java.lang.annotation.ElementType.METHOD;
24*3ff81872SXin Li import static java.lang.annotation.ElementType.CONSTRUCTOR;
25*3ff81872SXin Li import static java.lang.annotation.ElementType.FIELD;
26*3ff81872SXin Li 
27*3ff81872SXin Li /**
28*3ff81872SXin Li  * Identifies injectable constructors, methods, and fields. May apply to static
29*3ff81872SXin Li  * as well as instance members. An injectable member may have any access
30*3ff81872SXin Li  * modifier (private, package-private, protected, public). Constructors are
31*3ff81872SXin Li  * injected first, followed by fields, and then methods. Fields and methods
32*3ff81872SXin Li  * in superclasses are injected before those in subclasses. Ordering of
33*3ff81872SXin Li  * injection among fields and among methods in the same class is not specified.
34*3ff81872SXin Li  *
35*3ff81872SXin Li  * <p>Injectable constructors are annotated with {@code @Inject} and accept
36*3ff81872SXin Li  * zero or more dependencies as arguments. {@code @Inject} can apply to at most
37*3ff81872SXin Li  * one constructor per class.
38*3ff81872SXin Li  *
39*3ff81872SXin Li  * <p><tt><blockquote style="padding-left: 2em; text-indent: -2em;">@Inject
40*3ff81872SXin Li  *       <i>ConstructorModifiers<sub>opt</sub></i>
41*3ff81872SXin Li  *       <i>SimpleTypeName</i>(<i>FormalParameterList<sub>opt</sub></i>)
42*3ff81872SXin Li  *       <i>Throws<sub>opt</sub></i>
43*3ff81872SXin Li  *       <i>ConstructorBody</i></blockquote></tt>
44*3ff81872SXin Li  *
45*3ff81872SXin Li  * <p>{@code @Inject} is optional for public, no-argument constructors when no
46*3ff81872SXin Li  * other constructors are present. This enables injectors to invoke default
47*3ff81872SXin Li  * constructors.
48*3ff81872SXin Li  *
49*3ff81872SXin Li  * <p><tt><blockquote style="padding-left: 2em; text-indent: -2em;">
50*3ff81872SXin Li  *       {@literal @}Inject<sub><i>opt</i></sub>
51*3ff81872SXin Li  *       <i>Annotations<sub>opt</sub></i>
52*3ff81872SXin Li  *       public
53*3ff81872SXin Li  *       <i>SimpleTypeName</i>()
54*3ff81872SXin Li  *       <i>Throws<sub>opt</sub></i>
55*3ff81872SXin Li  *       <i>ConstructorBody</i></blockquote></tt>
56*3ff81872SXin Li  *
57*3ff81872SXin Li  * <p>Injectable fields:
58*3ff81872SXin Li  * <ul>
59*3ff81872SXin Li  *   <li>are annotated with {@code @Inject}.
60*3ff81872SXin Li  *   <li>are not final.
61*3ff81872SXin Li  *   <li>may have any otherwise valid name.</li></ul>
62*3ff81872SXin Li  *
63*3ff81872SXin Li  * <p><tt><blockquote style="padding-left: 2em; text-indent: -2em;">@Inject
64*3ff81872SXin Li  *       <i>FieldModifiers<sub>opt</sub></i>
65*3ff81872SXin Li  *       <i>Type</i>
66*3ff81872SXin Li  *       <i>VariableDeclarators</i>;</blockquote></tt>
67*3ff81872SXin Li  *
68*3ff81872SXin Li  * <p>Injectable methods:
69*3ff81872SXin Li  * <ul>
70*3ff81872SXin Li  *   <li>are annotated with {@code @Inject}.</li>
71*3ff81872SXin Li  *   <li>are not abstract.</li>
72*3ff81872SXin Li  *   <li>do not declare type parameters of their own.</li>
73*3ff81872SXin Li  *   <li>may return a result</li>
74*3ff81872SXin Li  *   <li>may have any otherwise valid name.</li>
75*3ff81872SXin Li  *   <li>accept zero or more dependencies as arguments.</li></ul>
76*3ff81872SXin Li  *
77*3ff81872SXin Li  * <p><tt><blockquote style="padding-left: 2em; text-indent: -2em;">@Inject
78*3ff81872SXin Li  *       <i>MethodModifiers<sub>opt</sub></i>
79*3ff81872SXin Li  *       <i>ResultType</i>
80*3ff81872SXin Li  *       <i>Identifier</i>(<i>FormalParameterList<sub>opt</sub></i>)
81*3ff81872SXin Li  *       <i>Throws<sub>opt</sub></i>
82*3ff81872SXin Li  *       <i>MethodBody</i></blockquote></tt>
83*3ff81872SXin Li  *
84*3ff81872SXin Li  * <p>The injector ignores the result of an injected method, but
85*3ff81872SXin Li  * non-{@code void} return types are allowed to support use of the method in
86*3ff81872SXin Li  * other contexts (builder-style method chaining, for example).
87*3ff81872SXin Li  *
88*3ff81872SXin Li  * <p>Examples:
89*3ff81872SXin Li  *
90*3ff81872SXin Li  * <pre>
91*3ff81872SXin Li  *   public class Car {
92*3ff81872SXin Li  *     // Injectable constructor
93*3ff81872SXin Li  *     &#064;Inject public Car(Engine engine) { ... }
94*3ff81872SXin Li  *
95*3ff81872SXin Li  *     // Injectable field
96*3ff81872SXin Li  *     &#064;Inject private Provider&lt;Seat> seatProvider;
97*3ff81872SXin Li  *
98*3ff81872SXin Li  *     // Injectable package-private method
99*3ff81872SXin Li  *     &#064;Inject void install(Windshield windshield, Trunk trunk) { ... }
100*3ff81872SXin Li  *   }</pre>
101*3ff81872SXin Li  *
102*3ff81872SXin Li  * <p>A method annotated with {@code @Inject} that overrides another method
103*3ff81872SXin Li  * annotated with {@code @Inject} will only be injected once per injection
104*3ff81872SXin Li  * request per instance. A method with <i>no</i> {@code @Inject} annotation
105*3ff81872SXin Li  * that overrides a method annotated with {@code @Inject} will not be
106*3ff81872SXin Li  * injected.
107*3ff81872SXin Li  *
108*3ff81872SXin Li  * <p>Injection of members annotated with {@code @Inject} is required. While an
109*3ff81872SXin Li  * injectable member may use any accessibility modifier (including
110*3ff81872SXin Li  * <tt>private</tt>), platform or injector limitations (like security
111*3ff81872SXin Li  * restrictions or lack of reflection support) might preclude injection
112*3ff81872SXin Li  * of non-public members.
113*3ff81872SXin Li  *
114*3ff81872SXin Li  * <h3>Qualifiers</h3>
115*3ff81872SXin Li  *
116*3ff81872SXin Li  * <p>A {@linkplain Qualifier qualifier} may annotate an injectable field
117*3ff81872SXin Li  * or parameter and, combined with the type, identify the implementation to
118*3ff81872SXin Li  * inject. Qualifiers are optional, and when used with {@code @Inject} in
119*3ff81872SXin Li  * injector-independent classes, no more than one qualifier should annotate a
120*3ff81872SXin Li  * single field or parameter. The qualifiers are bold in the following example:
121*3ff81872SXin Li  *
122*3ff81872SXin Li  * <pre>
123*3ff81872SXin Li  *   public class Car {
124*3ff81872SXin Li  *     &#064;Inject private <b>@Leather</b> Provider&lt;Seat> seatProvider;
125*3ff81872SXin Li  *
126*3ff81872SXin Li  *     &#064;Inject void install(<b>@Tinted</b> Windshield windshield,
127*3ff81872SXin Li  *         <b>@Big</b> Trunk trunk) { ... }
128*3ff81872SXin Li  *   }</pre>
129*3ff81872SXin Li  *
130*3ff81872SXin Li  * <p>If one injectable method overrides another, the overriding method's
131*3ff81872SXin Li  * parameters do not automatically inherit qualifiers from the overridden
132*3ff81872SXin Li  * method's parameters.
133*3ff81872SXin Li  *
134*3ff81872SXin Li  * <h3>Injectable Values</h3>
135*3ff81872SXin Li  *
136*3ff81872SXin Li  * <p>For a given type T and optional qualifier, an injector must be able to
137*3ff81872SXin Li  * inject a user-specified class that:
138*3ff81872SXin Li  *
139*3ff81872SXin Li  * <ol type="a">
140*3ff81872SXin Li  *   <li>is assignment compatible with T and</li>
141*3ff81872SXin Li  *   <li>has an injectable constructor.</li>
142*3ff81872SXin Li  * </ol>
143*3ff81872SXin Li  *
144*3ff81872SXin Li  * <p>For example, the user might use external configuration to pick an
145*3ff81872SXin Li  * implementation of T. Beyond that, which values are injected depend upon the
146*3ff81872SXin Li  * injector implementation and its configuration.
147*3ff81872SXin Li  *
148*3ff81872SXin Li  * <h3>Circular Dependencies</h3>
149*3ff81872SXin Li  *
150*3ff81872SXin Li  * <p>Detecting and resolving circular dependencies is left as an exercise for
151*3ff81872SXin Li  * the injector implementation. Circular dependencies between two constructors
152*3ff81872SXin Li  * is an obvious problem, but you can also have a circular dependency between
153*3ff81872SXin Li  * injectable fields or methods:
154*3ff81872SXin Li  *
155*3ff81872SXin Li  * <pre>
156*3ff81872SXin Li  *   class A {
157*3ff81872SXin Li  *     &#064;Inject B b;
158*3ff81872SXin Li  *   }
159*3ff81872SXin Li  *   class B {
160*3ff81872SXin Li  *     &#064;Inject A a;
161*3ff81872SXin Li  *   }</pre>
162*3ff81872SXin Li  *
163*3ff81872SXin Li  * <p>When constructing an instance of {@code A}, a naive injector
164*3ff81872SXin Li  * implementation might go into an infinite loop constructing an instance of
165*3ff81872SXin Li  * {@code B} to set on {@code A}, a second instance of {@code A} to set on
166*3ff81872SXin Li  * {@code B}, a second instance of {@code B} to set on the second instance of
167*3ff81872SXin Li  * {@code A}, and so on.
168*3ff81872SXin Li  *
169*3ff81872SXin Li  * <p>A conservative injector might detect the circular dependency at build
170*3ff81872SXin Li  * time and generate an error, at which point the programmer could break the
171*3ff81872SXin Li  * circular dependency by injecting {@link Provider Provider&lt;A>} or {@code
172*3ff81872SXin Li  * Provider<B>} instead of {@code A} or {@code B} respectively. Calling {@link
173*3ff81872SXin Li  * Provider#get() get()} on the provider directly from the constructor or
174*3ff81872SXin Li  * method it was injected into defeats the provider's ability to break up
175*3ff81872SXin Li  * circular dependencies. In the case of method or field injection, scoping
176*3ff81872SXin Li  * one of the dependencies (using {@linkplain Singleton singleton scope}, for
177*3ff81872SXin Li  * example) may also enable a valid circular relationship.
178*3ff81872SXin Li  *
179*3ff81872SXin Li  * @see javax.inject.Qualifier @Qualifier
180*3ff81872SXin Li  * @see javax.inject.Provider
181*3ff81872SXin Li  */
182*3ff81872SXin Li @Target({ METHOD, CONSTRUCTOR, FIELD })
183*3ff81872SXin Li @Retention(RUNTIME)
184*3ff81872SXin Li @Documented
185*3ff81872SXin Li public @interface Inject {}
186