xref: /aosp_15_r20/external/dagger2/java/dagger/Component.java (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
1*f585d8a3SJacky Wang /*
2*f585d8a3SJacky Wang  * Copyright (C) 2014 The Dagger Authors.
3*f585d8a3SJacky Wang  *
4*f585d8a3SJacky Wang  * Licensed under the Apache License, Version 2.0 (the "License");
5*f585d8a3SJacky Wang  * you may not use this file except in compliance with the License.
6*f585d8a3SJacky Wang  * You may obtain a copy of the License at
7*f585d8a3SJacky Wang  *
8*f585d8a3SJacky Wang  * http://www.apache.org/licenses/LICENSE-2.0
9*f585d8a3SJacky Wang  *
10*f585d8a3SJacky Wang  * Unless required by applicable law or agreed to in writing, software
11*f585d8a3SJacky Wang  * distributed under the License is distributed on an "AS IS" BASIS,
12*f585d8a3SJacky Wang  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*f585d8a3SJacky Wang  * See the License for the specific language governing permissions and
14*f585d8a3SJacky Wang  * limitations under the License.
15*f585d8a3SJacky Wang  */
16*f585d8a3SJacky Wang 
17*f585d8a3SJacky Wang package dagger;
18*f585d8a3SJacky Wang 
19*f585d8a3SJacky Wang import static java.lang.annotation.ElementType.TYPE;
20*f585d8a3SJacky Wang import static java.lang.annotation.RetentionPolicy.RUNTIME;
21*f585d8a3SJacky Wang 
22*f585d8a3SJacky Wang import java.lang.annotation.Documented;
23*f585d8a3SJacky Wang import java.lang.annotation.Retention;
24*f585d8a3SJacky Wang import java.lang.annotation.Target;
25*f585d8a3SJacky Wang import javax.inject.Inject;
26*f585d8a3SJacky Wang import javax.inject.Provider;
27*f585d8a3SJacky Wang import javax.inject.Qualifier;
28*f585d8a3SJacky Wang import javax.inject.Scope;
29*f585d8a3SJacky Wang import javax.inject.Singleton;
30*f585d8a3SJacky Wang 
31*f585d8a3SJacky Wang /**
32*f585d8a3SJacky Wang  * Annotates an interface or abstract class for which a fully-formed, dependency-injected
33*f585d8a3SJacky Wang  * implementation is to be generated from a set of {@linkplain #modules}. The generated class will
34*f585d8a3SJacky Wang  * have the name of the type annotated with {@code @Component} prepended with {@code Dagger}. For
35*f585d8a3SJacky Wang  * example, {@code @Component interface MyComponent {...}} will produce an implementation named
36*f585d8a3SJacky Wang  * {@code DaggerMyComponent}.
37*f585d8a3SJacky Wang  *
38*f585d8a3SJacky Wang  * <p><a id="component-methods"></a>
39*f585d8a3SJacky Wang  *
40*f585d8a3SJacky Wang  * <h2>Component methods</h2>
41*f585d8a3SJacky Wang  *
42*f585d8a3SJacky Wang  * <p>Every type annotated with {@code @Component} must contain at least one abstract component
43*f585d8a3SJacky Wang  * method. Component methods may have any name, but must have signatures that conform to either
44*f585d8a3SJacky Wang  * {@linkplain Provider provision} or {@linkplain MembersInjector members-injection} contracts.
45*f585d8a3SJacky Wang  *
46*f585d8a3SJacky Wang  * <p><a id="provision-methods"></a>
47*f585d8a3SJacky Wang  *
48*f585d8a3SJacky Wang  * <h3>Provision methods</h3>
49*f585d8a3SJacky Wang  *
50*f585d8a3SJacky Wang  * <p>Provision methods have no parameters and return an {@link Inject injected} or {@link Provides
51*f585d8a3SJacky Wang  * provided} type. Each method may have a {@link Qualifier} annotation as well. The following are
52*f585d8a3SJacky Wang  * all valid provision method declarations:
53*f585d8a3SJacky Wang  *
54*f585d8a3SJacky Wang  * <pre><code>
55*f585d8a3SJacky Wang  *   SomeType getSomeType();
56*f585d8a3SJacky Wang  *   {@literal Set<SomeType>} getSomeTypes();
57*f585d8a3SJacky Wang  *   {@literal @PortNumber} int getPortNumber();
58*f585d8a3SJacky Wang  * </code></pre>
59*f585d8a3SJacky Wang  *
60*f585d8a3SJacky Wang  * <p>Provision methods, like typical {@link Inject injection} sites, may use {@link Provider} or
61*f585d8a3SJacky Wang  * {@link Lazy} to more explicitly control provision requests. A {@link Provider} allows the user of
62*f585d8a3SJacky Wang  * the component to request provision any number of times by calling {@link Provider#get}. A {@link
63*f585d8a3SJacky Wang  * Lazy} will only ever request a single provision, but will defer it until the first call to {@link
64*f585d8a3SJacky Wang  * Lazy#get}. The following provision methods all request provision of the same type, but each
65*f585d8a3SJacky Wang  * implies different semantics:
66*f585d8a3SJacky Wang  *
67*f585d8a3SJacky Wang  * <pre><code>
68*f585d8a3SJacky Wang  *   SomeType getSomeType();
69*f585d8a3SJacky Wang  *   {@literal Provider<SomeType>} getSomeTypeProvider();
70*f585d8a3SJacky Wang  *   {@literal Lazy<SomeType>} getLazySomeType();
71*f585d8a3SJacky Wang  * </code></pre>
72*f585d8a3SJacky Wang  *
73*f585d8a3SJacky Wang  * <a id="members-injection-methods"></a>
74*f585d8a3SJacky Wang  *
75*f585d8a3SJacky Wang  * <h3>Members-injection methods</h3>
76*f585d8a3SJacky Wang  *
77*f585d8a3SJacky Wang  * <p>Members-injection methods have a single parameter and inject dependencies into each of the
78*f585d8a3SJacky Wang  * {@link Inject}-annotated fields and methods of the passed instance. A members-injection method
79*f585d8a3SJacky Wang  * may be void or return its single parameter as a convenience for chaining. The following are all
80*f585d8a3SJacky Wang  * valid members-injection method declarations:
81*f585d8a3SJacky Wang  *
82*f585d8a3SJacky Wang  * <pre><code>
83*f585d8a3SJacky Wang  *   void injectSomeType(SomeType someType);
84*f585d8a3SJacky Wang  *   SomeType injectAndReturnSomeType(SomeType someType);
85*f585d8a3SJacky Wang  * </code></pre>
86*f585d8a3SJacky Wang  *
87*f585d8a3SJacky Wang  * <p>A method with no parameters that returns a {@link MembersInjector} is equivalent to a members
88*f585d8a3SJacky Wang  * injection method. Calling {@link MembersInjector#injectMembers} on the returned object will
89*f585d8a3SJacky Wang  * perform the same work as a members injection method. For example:
90*f585d8a3SJacky Wang  *
91*f585d8a3SJacky Wang  * <pre><code>
92*f585d8a3SJacky Wang  *   {@literal MembersInjector<SomeType>} getSomeTypeMembersInjector();
93*f585d8a3SJacky Wang  * </code></pre>
94*f585d8a3SJacky Wang  *
95*f585d8a3SJacky Wang  * <h4>A note about covariance</h4>
96*f585d8a3SJacky Wang  *
97*f585d8a3SJacky Wang  * <p>While a members-injection method for a type will accept instances of its subtypes, only {@link
98*f585d8a3SJacky Wang  * Inject}-annotated members of the parameter type and its supertypes will be injected; members of
99*f585d8a3SJacky Wang  * subtypes will not. For example, given the following types, only {@code a} and {@code b} will be
100*f585d8a3SJacky Wang  * injected into an instance of {@code Child} when it is passed to the members-injection method
101*f585d8a3SJacky Wang  * {@code injectSelf(Self instance)}:
102*f585d8a3SJacky Wang  *
103*f585d8a3SJacky Wang  * <pre><code>
104*f585d8a3SJacky Wang  *   class Parent {
105*f585d8a3SJacky Wang  *     {@literal @}Inject A a;
106*f585d8a3SJacky Wang  *   }
107*f585d8a3SJacky Wang  *
108*f585d8a3SJacky Wang  *   class Self extends Parent {
109*f585d8a3SJacky Wang  *     {@literal @}Inject B b;
110*f585d8a3SJacky Wang  *   }
111*f585d8a3SJacky Wang  *
112*f585d8a3SJacky Wang  *   class Child extends Self {
113*f585d8a3SJacky Wang  *     {@literal @}Inject C c;
114*f585d8a3SJacky Wang  *   }
115*f585d8a3SJacky Wang  * </code></pre>
116*f585d8a3SJacky Wang  *
117*f585d8a3SJacky Wang  * <a id="instantiation"></a>
118*f585d8a3SJacky Wang  *
119*f585d8a3SJacky Wang  * <h2>Instantiation</h2>
120*f585d8a3SJacky Wang  *
121*f585d8a3SJacky Wang  * <p>Component implementations are primarily instantiated via a generated <a
122*f585d8a3SJacky Wang  * href="http://en.wikipedia.org/wiki/Builder_pattern">builder</a> or <a
123*f585d8a3SJacky Wang  * href="https://en.wikipedia.org/wiki/Factory_(object-oriented_programming)">factory</a>.
124*f585d8a3SJacky Wang  *
125*f585d8a3SJacky Wang  * <p>If a nested {@link Builder @Component.Builder} or {@link Factory @Component.Factory} type
126*f585d8a3SJacky Wang  * exists in the component, Dagger will generate an implementation of that type. If neither exists,
127*f585d8a3SJacky Wang  * Dagger will generate a builder type that has a method to set each of the {@linkplain #modules}
128*f585d8a3SJacky Wang  * and component {@linkplain #dependencies} named with the <a
129*f585d8a3SJacky Wang  * href="http://en.wikipedia.org/wiki/CamelCase">lower camel case</a> version of the module or
130*f585d8a3SJacky Wang  * dependency type.
131*f585d8a3SJacky Wang  *
132*f585d8a3SJacky Wang  * <p>In either case, the Dagger-generated component type will have a static method, named either
133*f585d8a3SJacky Wang  * {@code builder()} or {@code factory()}, that returns a builder or factory instance.
134*f585d8a3SJacky Wang  *
135*f585d8a3SJacky Wang  * <p>Example of using a builder:
136*f585d8a3SJacky Wang  *
137*f585d8a3SJacky Wang  * <pre>{@code
138*f585d8a3SJacky Wang  * public static void main(String[] args) {
139*f585d8a3SJacky Wang  *   OtherComponent otherComponent = ...;
140*f585d8a3SJacky Wang  *   MyComponent component = DaggerMyComponent.builder()
141*f585d8a3SJacky Wang  *       // required because component dependencies must be set
142*f585d8a3SJacky Wang  *       .otherComponent(otherComponent)
143*f585d8a3SJacky Wang  *       // required because FlagsModule has constructor parameters
144*f585d8a3SJacky Wang  *       .flagsModule(new FlagsModule(args))
145*f585d8a3SJacky Wang  *       // may be elided because a no-args constructor is visible
146*f585d8a3SJacky Wang  *       .myApplicationModule(new MyApplicationModule())
147*f585d8a3SJacky Wang  *       .build();
148*f585d8a3SJacky Wang  * }
149*f585d8a3SJacky Wang  * }</pre>
150*f585d8a3SJacky Wang  *
151*f585d8a3SJacky Wang  * <p>Example of using a factory:
152*f585d8a3SJacky Wang  *
153*f585d8a3SJacky Wang  * <pre>{@code
154*f585d8a3SJacky Wang  * public static void main(String[] args) {
155*f585d8a3SJacky Wang  *     OtherComponent otherComponent = ...;
156*f585d8a3SJacky Wang  *     MyComponent component = DaggerMyComponent.factory()
157*f585d8a3SJacky Wang  *         .create(otherComponent, new FlagsModule(args), new MyApplicationModule());
158*f585d8a3SJacky Wang  *     // Note that all parameters to a factory method are required, even if one is for a module
159*f585d8a3SJacky Wang  *     // that Dagger could instantiate. The only case where null is legal is for a
160*f585d8a3SJacky Wang  *     // @BindsInstance @Nullable parameter.
161*f585d8a3SJacky Wang  *   }
162*f585d8a3SJacky Wang  * }</pre>
163*f585d8a3SJacky Wang  *
164*f585d8a3SJacky Wang  * <p>In the case that a component has no component dependencies and only no-arg modules, the
165*f585d8a3SJacky Wang  * generated component will also have a factory method {@code create()}. {@code
166*f585d8a3SJacky Wang  * SomeComponent.create()} and {@code SomeComponent.builder().build()} are both valid and
167*f585d8a3SJacky Wang  * equivalent.
168*f585d8a3SJacky Wang  *
169*f585d8a3SJacky Wang  * <p><a id="scope"></a>
170*f585d8a3SJacky Wang  *
171*f585d8a3SJacky Wang  * <h2>Scope</h2>
172*f585d8a3SJacky Wang  *
173*f585d8a3SJacky Wang  * <p>Each Dagger component can be associated with a scope by annotating it with the {@linkplain
174*f585d8a3SJacky Wang  * Scope scope annotation}. The component implementation ensures that there is only one provision of
175*f585d8a3SJacky Wang  * each scoped binding per instance of the component. If the component declares a scope, it may only
176*f585d8a3SJacky Wang  * contain unscoped bindings or bindings of that scope anywhere in the graph. For example:
177*f585d8a3SJacky Wang  *
178*f585d8a3SJacky Wang  * <pre><code>
179*f585d8a3SJacky Wang  *   {@literal @}Singleton {@literal @}Component
180*f585d8a3SJacky Wang  *   interface MyApplicationComponent {
181*f585d8a3SJacky Wang  *     // this component can only inject types using unscoped or {@literal @}Singleton bindings
182*f585d8a3SJacky Wang  *   }
183*f585d8a3SJacky Wang  * </code></pre>
184*f585d8a3SJacky Wang  *
185*f585d8a3SJacky Wang  * <p>In order to get the proper behavior associated with a scope annotation, it is the caller's
186*f585d8a3SJacky Wang  * responsibility to instantiate new component instances when appropriate. A {@link Singleton}
187*f585d8a3SJacky Wang  * component, for instance, should only be instantiated once per application, while a {@code
188*f585d8a3SJacky Wang  * RequestScoped} component should be instantiated once per request. Because components are
189*f585d8a3SJacky Wang  * self-contained implementations, exiting a scope is as simple as dropping all references to the
190*f585d8a3SJacky Wang  * component instance.
191*f585d8a3SJacky Wang  *
192*f585d8a3SJacky Wang  * <p><a id="component-relationships"></a>
193*f585d8a3SJacky Wang  *
194*f585d8a3SJacky Wang  * <h2>Component relationships</h2>
195*f585d8a3SJacky Wang  *
196*f585d8a3SJacky Wang  * <p>While there is much utility in isolated components with purely unscoped bindings, many
197*f585d8a3SJacky Wang  * applications will call for multiple components with multiple scopes to interact. Dagger provides
198*f585d8a3SJacky Wang  * two mechanisms for relating components.
199*f585d8a3SJacky Wang  *
200*f585d8a3SJacky Wang  * <p><a id="subcomponents"></a>
201*f585d8a3SJacky Wang  *
202*f585d8a3SJacky Wang  * <h3>Subcomponents</h3>
203*f585d8a3SJacky Wang  *
204*f585d8a3SJacky Wang  * <p>The simplest way to relate two components is by declaring a {@link Subcomponent}. A
205*f585d8a3SJacky Wang  * subcomponent behaves exactly like a component, but has its implementation generated within a
206*f585d8a3SJacky Wang  * parent component or subcomponent. That relationship allows the subcomponent implementation to
207*f585d8a3SJacky Wang  * inherit the <em>entire</em> binding graph from its parent when it is declared. For that reason, a
208*f585d8a3SJacky Wang  * subcomponent isn't evaluated for completeness until it is associated with a parent.
209*f585d8a3SJacky Wang  *
210*f585d8a3SJacky Wang  * <p>Subcomponents are declared by listing the class in the {@link Module#subcomponents()}
211*f585d8a3SJacky Wang  * attribute of one of the parent component's modules. This binds the {@link Subcomponent.Builder}
212*f585d8a3SJacky Wang  * or {@link Subcomponent.Factory} for that subcomponent within the parent component.
213*f585d8a3SJacky Wang  *
214*f585d8a3SJacky Wang  * <p>Subcomponents may also be declared via a factory method on a parent component or subcomponent.
215*f585d8a3SJacky Wang  * The method may have any name, but must return the subcomponent. The factory method's parameters
216*f585d8a3SJacky Wang  * may be any number of the subcomponent's modules, but must at least include those without visible
217*f585d8a3SJacky Wang  * no-arg constructors. The following is an example of a factory method that creates a
218*f585d8a3SJacky Wang  * request-scoped subcomponent from a singleton-scoped parent:
219*f585d8a3SJacky Wang  *
220*f585d8a3SJacky Wang  * <pre><code>
221*f585d8a3SJacky Wang  *   {@literal @}Singleton {@literal @}Component
222*f585d8a3SJacky Wang  *   interface ApplicationComponent {
223*f585d8a3SJacky Wang  *     // component methods...
224*f585d8a3SJacky Wang  *
225*f585d8a3SJacky Wang  *     RequestComponent newRequestComponent(RequestModule requestModule);
226*f585d8a3SJacky Wang  *   }
227*f585d8a3SJacky Wang  * </code></pre>
228*f585d8a3SJacky Wang  *
229*f585d8a3SJacky Wang  * <a id="component-dependencies"></a>
230*f585d8a3SJacky Wang  *
231*f585d8a3SJacky Wang  * <h3>Component dependencies</h3>
232*f585d8a3SJacky Wang  *
233*f585d8a3SJacky Wang  * <p>While subcomponents are the simplest way to compose subgraphs of bindings, subcomponents are
234*f585d8a3SJacky Wang  * tightly coupled with the parents; they may use any binding defined by their ancestor component
235*f585d8a3SJacky Wang  * and subcomponents. As an alternative, components can use bindings only from another <em>component
236*f585d8a3SJacky Wang  * interface</em> by declaring a {@linkplain #dependencies component dependency}. When a type is
237*f585d8a3SJacky Wang  * used as a component dependency, each <a href="#provision-methods">provision method</a> on the
238*f585d8a3SJacky Wang  * dependency is bound as a provider. Note that <em>only</em> the bindings exposed as provision
239*f585d8a3SJacky Wang  * methods are available through component dependencies.
240*f585d8a3SJacky Wang  *
241*f585d8a3SJacky Wang  * @since 2.0
242*f585d8a3SJacky Wang  */
243*f585d8a3SJacky Wang @Retention(RUNTIME) // Allows runtimes to have specialized behavior interoperating with Dagger.
244*f585d8a3SJacky Wang @Target(TYPE)
245*f585d8a3SJacky Wang @Documented
246*f585d8a3SJacky Wang public @interface Component {
247*f585d8a3SJacky Wang   /**
248*f585d8a3SJacky Wang    * A list of classes annotated with {@link Module} whose bindings are used to generate the
249*f585d8a3SJacky Wang    * component implementation. Note that through the use of {@link Module#includes} the full set of
250*f585d8a3SJacky Wang    * modules used to implement the component may include more modules that just those listed here.
251*f585d8a3SJacky Wang    */
modules()252*f585d8a3SJacky Wang   Class<?>[] modules() default {};
253*f585d8a3SJacky Wang 
254*f585d8a3SJacky Wang   /**
255*f585d8a3SJacky Wang    * A list of types that are to be used as <a href="#component-dependencies">component
256*f585d8a3SJacky Wang    * dependencies</a>.
257*f585d8a3SJacky Wang    */
dependencies()258*f585d8a3SJacky Wang   Class<?>[] dependencies() default {};
259*f585d8a3SJacky Wang 
260*f585d8a3SJacky Wang   /**
261*f585d8a3SJacky Wang    * A builder for a component.
262*f585d8a3SJacky Wang    *
263*f585d8a3SJacky Wang    * <p>A builder is a type with setter methods for the {@linkplain Component#modules modules},
264*f585d8a3SJacky Wang    * {@linkplain Component#dependencies dependencies} and {@linkplain BindsInstance bound instances}
265*f585d8a3SJacky Wang    * required by the component and a single no-argument build method that creates a new component
266*f585d8a3SJacky Wang    * instance.
267*f585d8a3SJacky Wang    *
268*f585d8a3SJacky Wang    * <p>Components may have a single nested {@code static abstract class} or {@code interface}
269*f585d8a3SJacky Wang    * annotated with {@code @Component.Builder}. If they do, then Dagger will generate a builder
270*f585d8a3SJacky Wang    * class that implements that type. Note that a component with a {@code @Component.Builder} may
271*f585d8a3SJacky Wang    * not also have a {@code @Component.Factory}.
272*f585d8a3SJacky Wang    *
273*f585d8a3SJacky Wang    * <p>Builder types must follow some rules:
274*f585d8a3SJacky Wang    *
275*f585d8a3SJacky Wang    * <ul>
276*f585d8a3SJacky Wang    *   <li>There <i>must</i> be exactly one abstract no-argument method that returns the component
277*f585d8a3SJacky Wang    *       type or one of its supertypes, called the "build method".
278*f585d8a3SJacky Wang    *   <li>There <i>may</i> be other other abstract methods, called "setter methods".
279*f585d8a3SJacky Wang    *   <li>Setter methods <i>must</i> take a single argument and return {@code void}, the builder
280*f585d8a3SJacky Wang    *       type or a supertype of the builder type.
281*f585d8a3SJacky Wang    *   <li>There <i>must</i> be a setter method for each {@linkplain Component#dependencies
282*f585d8a3SJacky Wang    *       component dependency}.
283*f585d8a3SJacky Wang    *   <li>There <i>must</i> be a setter method for each non-{@code abstract} {@linkplain
284*f585d8a3SJacky Wang    *       Component#modules module} that has non-{@code static} binding methods, unless Dagger can
285*f585d8a3SJacky Wang    *       instantiate that module with a visible no-argument constructor.
286*f585d8a3SJacky Wang    *   <li>There <i>may</i> be setter methods for modules that Dagger can instantiate or does not
287*f585d8a3SJacky Wang    *       need to instantiate.
288*f585d8a3SJacky Wang    *   <li>There <i>may</i> be setter methods annotated with {@code @BindsInstance}. These methods
289*f585d8a3SJacky Wang    *       bind the instance passed to them within the component. See {@link
290*f585d8a3SJacky Wang    *       BindsInstance @BindsInstance} for more information.
291*f585d8a3SJacky Wang    *   <li>There <i>may</i> be non-{@code abstract} methods, but they are ignored as far as
292*f585d8a3SJacky Wang    *       validation and builder generation are concerned.
293*f585d8a3SJacky Wang    * </ul>
294*f585d8a3SJacky Wang    *
295*f585d8a3SJacky Wang    * For example, this could be a valid {@code Component} with a {@code Builder}:
296*f585d8a3SJacky Wang    *
297*f585d8a3SJacky Wang    * <pre><code>
298*f585d8a3SJacky Wang    * {@literal @}Component(modules = {BackendModule.class, FrontendModule.class})
299*f585d8a3SJacky Wang    * interface MyComponent {
300*f585d8a3SJacky Wang    *   MyWidget myWidget();
301*f585d8a3SJacky Wang    *
302*f585d8a3SJacky Wang    *   {@literal @}Component.Builder
303*f585d8a3SJacky Wang    *   interface Builder {
304*f585d8a3SJacky Wang    *     Builder backendModule(BackendModule bm);
305*f585d8a3SJacky Wang    *     Builder frontendModule(FrontendModule fm);
306*f585d8a3SJacky Wang    *     {@literal @}BindsInstance
307*f585d8a3SJacky Wang    *     Builder foo(Foo foo);
308*f585d8a3SJacky Wang    *     MyComponent build();
309*f585d8a3SJacky Wang    *   }
310*f585d8a3SJacky Wang    * }</code></pre>
311*f585d8a3SJacky Wang    */
312*f585d8a3SJacky Wang   @Retention(RUNTIME) // Allows runtimes to have specialized behavior interoperating with Dagger.
313*f585d8a3SJacky Wang   @Target(TYPE)
314*f585d8a3SJacky Wang   @Documented
315*f585d8a3SJacky Wang   @interface Builder {}
316*f585d8a3SJacky Wang 
317*f585d8a3SJacky Wang   /**
318*f585d8a3SJacky Wang    * A factory for a component.
319*f585d8a3SJacky Wang    *
320*f585d8a3SJacky Wang    * <p>A factory is a type with a single method that returns a new component instance each time it
321*f585d8a3SJacky Wang    * is called. The parameters of that method allow the caller to provide the {@linkplain
322*f585d8a3SJacky Wang    * Component#modules modules}, {@linkplain Component#dependencies dependencies} and {@linkplain
323*f585d8a3SJacky Wang    * BindsInstance bound instances} required by the component.
324*f585d8a3SJacky Wang    *
325*f585d8a3SJacky Wang    * <p>Components may have a single nested {@code static abstract class} or {@code interface}
326*f585d8a3SJacky Wang    * annotated with {@code @Component.Factory}. If they do, then Dagger will generate a factory
327*f585d8a3SJacky Wang    * class that will implement that type. Note that a component with a {@code @Component.Factory}
328*f585d8a3SJacky Wang    * may not also have a {@code @Component.Builder}.
329*f585d8a3SJacky Wang    *
330*f585d8a3SJacky Wang    * <p>Factory types must follow some rules:
331*f585d8a3SJacky Wang    *
332*f585d8a3SJacky Wang    * <ul>
333*f585d8a3SJacky Wang    *   <li>There <i>must</i> be exactly one abstract method, which must return the component type or
334*f585d8a3SJacky Wang    *       one of its supertypes.
335*f585d8a3SJacky Wang    *   <li>The method <i>must</i> have a parameter for each {@linkplain Component#dependencies
336*f585d8a3SJacky Wang    *       component dependency}.
337*f585d8a3SJacky Wang    *   <li>The method <i>must</i> have a parameter for each non-{@code abstract} {@linkplain
338*f585d8a3SJacky Wang    *       Component#modules module} that has non-{@code static} binding methods, unless Dagger can
339*f585d8a3SJacky Wang    *       instantiate that module with a visible no-argument constructor.
340*f585d8a3SJacky Wang    *   <li>The method <i>may</i> have parameters for modules that Dagger can instantiate or does not
341*f585d8a3SJacky Wang    *       need to instantiate.
342*f585d8a3SJacky Wang    *   <li>The method <i>may</i> have parameters annotated with {@code @BindsInstance}. These
343*f585d8a3SJacky Wang    *       parameters bind the instance passed for that parameter within the component. See {@link
344*f585d8a3SJacky Wang    *       BindsInstance @BindsInstance} for more information.
345*f585d8a3SJacky Wang    *   <li>There <i>may</i> be non-{@code abstract} methods, but they are ignored as far as
346*f585d8a3SJacky Wang    *       validation and factory generation are concerned.
347*f585d8a3SJacky Wang    * </ul>
348*f585d8a3SJacky Wang    *
349*f585d8a3SJacky Wang    * For example, this could be a valid {@code Component} with a {@code Factory}:
350*f585d8a3SJacky Wang    *
351*f585d8a3SJacky Wang    * <pre><code>
352*f585d8a3SJacky Wang    * {@literal @}Component(modules = {BackendModule.class, FrontendModule.class})
353*f585d8a3SJacky Wang    * interface MyComponent {
354*f585d8a3SJacky Wang    *   MyWidget myWidget();
355*f585d8a3SJacky Wang    *
356*f585d8a3SJacky Wang    *   {@literal @}Component.Factory
357*f585d8a3SJacky Wang    *   interface Factory {
358*f585d8a3SJacky Wang    *     MyComponent newMyComponent(
359*f585d8a3SJacky Wang    *         BackendModule bm, FrontendModule fm, {@literal @}BindsInstance Foo foo);
360*f585d8a3SJacky Wang    *   }
361*f585d8a3SJacky Wang    * }</code></pre>
362*f585d8a3SJacky Wang    *
363*f585d8a3SJacky Wang    * <p>For a root component, if a {@code @Component.Factory} is defined, the generated component
364*f585d8a3SJacky Wang    * type will have a {@code static} method named {@code factory()} that returns an instance of that
365*f585d8a3SJacky Wang    * factory.
366*f585d8a3SJacky Wang    *
367*f585d8a3SJacky Wang    * @since 2.22
368*f585d8a3SJacky Wang    */
369*f585d8a3SJacky Wang   @Retention(RUNTIME) // Allows runtimes to have specialized behavior interoperating with Dagger.
370*f585d8a3SJacky Wang   @Target(TYPE)
371*f585d8a3SJacky Wang   @Documented
372*f585d8a3SJacky Wang   @interface Factory {}
373*f585d8a3SJacky Wang }
374