xref: /aosp_15_r20/external/dagger2/java/dagger/spi/model/Binding.java (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
1 /*
2  * Copyright (C) 2021 The Dagger Authors.
3  *
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  */
16 
17 package dagger.spi.model;
18 
19 import com.google.common.collect.ImmutableSet;
20 import dagger.spi.model.BindingGraph.MaybeBinding;
21 import java.util.Optional;
22 
23 /**
24  * The association between a {@link Key} and the way in which instances of the key are provided.
25  * Includes any {@linkplain DependencyRequest dependencies} that are needed in order to provide the
26  * instances.
27  *
28  * <p>If a binding is owned by more than one component, there is one {@code Binding} for every
29  * owning component.
30  */
31 public interface Binding extends MaybeBinding {
32   @Override
componentPath()33   ComponentPath componentPath();
34 
35   /** @deprecated This always returns {@code Optional.of(this)}. */
36   @Override
37   @Deprecated
binding()38   default Optional<Binding> binding() {
39     return Optional.of(this);
40   }
41   /**
42    * The dependencies of this binding. The order of the dependencies corresponds to the order in
43    * which they will be injected when the binding is requested.
44    */
dependencies()45   ImmutableSet<DependencyRequest> dependencies();
46 
47   /**
48    * The {@link DaggerElement} that declares this binding. Absent for
49    * {@linkplain BindingKind binding kinds} that are not always declared by exactly one element.
50    *
51    * <p>For example, consider {@link BindingKind#MULTIBOUND_SET}. A component with many
52    * {@code @IntoSet} bindings for the same key will have a synthetic binding that depends on all
53    * contributions, but with no identifiying binding element. A {@code @Multibinds} method will also
54    * contribute a synthetic binding, but since multiple {@code @Multibinds} methods can coexist in
55    * the same component (and contribute to one single binding), it has no binding element.
56    */
bindingElement()57   Optional<DaggerElement> bindingElement();
58 
59   /**
60    * The {@link DaggerTypeElement} of the module which contributes this binding. Absent for bindings
61    * that have no {@link #bindingElement() binding element}.
62    */
contributingModule()63   Optional<DaggerTypeElement> contributingModule();
64 
65   /**
66    * Returns {@code true} if using this binding requires an instance of the {@link
67    * #contributingModule()}.
68    */
requiresModuleInstance()69   boolean requiresModuleInstance();
70 
71   /** The scope of this binding if it has one. */
scope()72   Optional<Scope> scope();
73 
74   /**
75    * Returns {@code true} if this binding may provide {@code null} instead of an instance of {@link
76    * #key()}. Nullable bindings cannot be requested from {@linkplain DependencyRequest#isNullable()
77    * non-nullable dependency requests}.
78    */
isNullable()79   boolean isNullable();
80 
81   /** Returns {@code true} if this is a production binding, e.g. an {@code @Produces} method. */
isProduction()82   boolean isProduction();
83 
84   /** The kind of binding this instance represents. */
kind()85   BindingKind kind();
86 
87 }
88