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