blob: 99b65e2897069d26ab947c92b1c7d12b01cd449e [file] [log] [blame]
gake1b68a22016-04-07 11:54:06 -07001/*
ronshapiro5dde42d2016-06-17 09:03:35 -07002 * Copyright (C) 2016 The Dagger Authors.
gake1b68a22016-04-07 11:54:06 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
dpb1b65b6a2016-07-11 12:11:24 -070016
gake1b68a22016-04-07 11:54:06 -070017package dagger;
18
gake55f0742016-07-12 15:36:42 -070019import static java.lang.annotation.ElementType.METHOD;
20import static java.lang.annotation.RetentionPolicy.RUNTIME;
21
gake1b68a22016-04-07 11:54:06 -070022import java.lang.annotation.Documented;
23import java.lang.annotation.Retention;
24import java.lang.annotation.Target;
25
gake1b68a22016-04-07 11:54:06 -070026/**
27 * Annotates <em>abstract</em> methods of a {@link Module} that delegate bindings. For example, to
28 * bind {@link java.util.Random} to {@link java.security.SecureRandom} a module could declare the
cgruber6b454b62016-04-20 10:06:06 -070029 * following: {@code @Binds abstract Random bindRandom(SecureRandom secureRandom);}
gake1b68a22016-04-07 11:54:06 -070030 *
cgruber6b454b62016-04-20 10:06:06 -070031 * <p>{@code @Binds} methods are a drop-in replacement for {@link Provides} methods that simply
gak43de0a42016-11-07 09:51:25 -080032 * return an injected parameter. Prefer {@code @Binds} because the generated implementation is
gake1b68a22016-04-07 11:54:06 -070033 * likely to be more efficient.
34 *
cgruber6b454b62016-04-20 10:06:06 -070035 * <p>A {@code @Binds} method:
gak43de0a42016-11-07 09:51:25 -080036 *
gake1b68a22016-04-07 11:54:06 -070037 * <ul>
gak43de0a42016-11-07 09:51:25 -080038 * <li>Must be {@code abstract}.
39 * <li>May be {@linkplain javax.inject.Scope scoped}.
40 * <li>May be {@linkplain javax.inject.Qualifier qualified}.
41 * <li>Must have a single parameter whose type is assignable to the return type. The return type
42 * declares the bound type (just as it would for a {@literal @}{@link dagger.Provides} method)
43 * and the parameter is the type to which it is bound.
44 * <p>For {@linkplain dagger.multibindings multibindings}, assignability is checked in similar
45 * ways:
46 * <dl>
47 * <dt>{@link dagger.multibindings.IntoSet}
48 * <dd>The parameter must be assignable to the only parameter of {@link java.util.Set#add}
49 * when viewed as a member of the return type — the parameter must be assignable to the
50 * return type.
51 * <dt>{@link dagger.multibindings.ElementsIntoSet}
ronshapiro9dd3da32018-01-30 11:42:41 -080052 * <dd>The parameter must be assignable to the only parameter of {@link
gak43de0a42016-11-07 09:51:25 -080053 * java.util.Set#addAll} when viewed as a member of the return type — if the return type
ronshapiro9dd3da32018-01-30 11:42:41 -080054 * is {@code Set<E>}, the parameter must be assignable to {@code Collection<? extends
55 * E>}.
gak43de0a42016-11-07 09:51:25 -080056 * <dt>{@link dagger.multibindings.IntoMap}
57 * <dd>The parameter must be assignable to the {@code value} parameter of {@link
58 * java.util.Map#put} when viewed as a member of a {@link java.util.Map} in which {@code
59 * V} is bound to the return type — the parameter must be assignable to the return type
60 * </dl>
gake1b68a22016-04-07 11:54:06 -070061 * </ul>
62 */
63@Documented
64@Retention(RUNTIME)
65@Target(METHOD)
gak24b02282016-04-11 15:51:14 -070066public @interface Binds {}