xref: /aosp_15_r20/external/dagger2/java/dagger/Binds.java (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
1*f585d8a3SJacky Wang /*
2*f585d8a3SJacky Wang  * Copyright (C) 2016 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.METHOD;
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 
26*f585d8a3SJacky Wang /**
27*f585d8a3SJacky Wang  * Annotates <em>abstract</em> methods of a {@link Module} that delegate bindings. For example, to
28*f585d8a3SJacky Wang  * bind {@link java.util.Random} to {@link java.security.SecureRandom} a module could declare the
29*f585d8a3SJacky Wang  * following: {@code @Binds abstract Random bindRandom(SecureRandom secureRandom);}
30*f585d8a3SJacky Wang  *
31*f585d8a3SJacky Wang  * <p>{@code @Binds} methods are a drop-in replacement for {@link Provides} methods that simply
32*f585d8a3SJacky Wang  * return an injected parameter. Prefer {@code @Binds} because the generated implementation is
33*f585d8a3SJacky Wang  * likely to be more efficient.
34*f585d8a3SJacky Wang  *
35*f585d8a3SJacky Wang  * <p>A {@code @Binds} method:
36*f585d8a3SJacky Wang  *
37*f585d8a3SJacky Wang  * <ul>
38*f585d8a3SJacky Wang  *   <li>Must be {@code abstract}.
39*f585d8a3SJacky Wang  *   <li>May be {@linkplain javax.inject.Scope scoped}.
40*f585d8a3SJacky Wang  *   <li>May be {@linkplain javax.inject.Qualifier qualified}.
41*f585d8a3SJacky Wang  *   <li>Must have a single parameter whose type is assignable to the return type. The return type
42*f585d8a3SJacky Wang  *       declares the bound type (just as it would for a {@literal @}{@link dagger.Provides} method)
43*f585d8a3SJacky Wang  *       and the parameter is the type to which it is bound.
44*f585d8a3SJacky Wang  *       <p>For {@linkplain dagger.multibindings multibindings}, assignability is checked in similar
45*f585d8a3SJacky Wang  *       ways:
46*f585d8a3SJacky Wang  *       <dl>
47*f585d8a3SJacky Wang  *         <dt>{@link dagger.multibindings.IntoSet}
48*f585d8a3SJacky Wang  *         <dd>The parameter must be assignable to the only parameter of {@link java.util.Set#add}
49*f585d8a3SJacky Wang  *             when viewed as a member of the return type — the parameter must be assignable to the
50*f585d8a3SJacky Wang  *             return type.
51*f585d8a3SJacky Wang  *         <dt>{@link dagger.multibindings.ElementsIntoSet}
52*f585d8a3SJacky Wang  *         <dd>The parameter must be assignable to the only parameter of {@link
53*f585d8a3SJacky Wang  *             java.util.Set#addAll} when viewed as a member of the return type — if the return type
54*f585d8a3SJacky Wang  *             is {@code Set<E>}, the parameter must be assignable to {@code Collection<? extends
55*f585d8a3SJacky Wang  *             E>}.
56*f585d8a3SJacky Wang  *         <dt>{@link dagger.multibindings.IntoMap}
57*f585d8a3SJacky Wang  *         <dd>The parameter must be assignable to the {@code value} parameter of {@link
58*f585d8a3SJacky Wang  *             java.util.Map#put} when viewed as a member of a {@link java.util.Map} in which {@code
59*f585d8a3SJacky Wang  *             V} is bound to the return type — the parameter must be assignable to the return type
60*f585d8a3SJacky Wang  *       </dl>
61*f585d8a3SJacky Wang  * </ul>
62*f585d8a3SJacky Wang  */
63*f585d8a3SJacky Wang @Documented
64*f585d8a3SJacky Wang @Retention(RUNTIME)
65*f585d8a3SJacky Wang @Target(METHOD)
66*f585d8a3SJacky Wang public @interface Binds {}
67