• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.internal.codegen.binding;
18 
19 import static dagger.internal.codegen.binding.BindingType.PRODUCTION;
20 
21 import com.google.auto.value.AutoValue;
22 import com.google.common.collect.ImmutableSet;
23 import com.google.common.collect.Iterables;
24 import dagger.internal.codegen.model.BindingKind;
25 import dagger.internal.codegen.model.ComponentPath;
26 import dagger.internal.codegen.model.DaggerElement;
27 import dagger.internal.codegen.model.DaggerTypeElement;
28 import dagger.internal.codegen.model.DependencyRequest;
29 import dagger.internal.codegen.model.Key;
30 import dagger.internal.codegen.model.Scope;
31 import java.util.Optional;
32 import javax.inject.Inject;
33 
34 /**
35  * An implementation of {@link dagger.internal.codegen.model.Binding} that also exposes {@link
36  * Declaration}s associated with the binding.
37  */
38 // TODO(dpb): Consider a supertype of dagger.internal.codegen.model.Binding that
39 // dagger.internal.codegen.binding.Binding
40 // could also implement.
41 @AutoValue
42 public abstract class BindingNode implements dagger.internal.codegen.model.Binding {
43   private DeclarationFormatter declarationFormatter;
44 
delegate()45   public abstract Binding delegate();
46 
multibindingDeclarations()47   public abstract ImmutableSet<MultibindingDeclaration> multibindingDeclarations();
48 
optionalBindingDeclarations()49   public abstract ImmutableSet<OptionalBindingDeclaration> optionalBindingDeclarations();
50 
subcomponentDeclarations()51   public abstract ImmutableSet<SubcomponentDeclaration> subcomponentDeclarations();
52 
53   /**
54    * The elements (other than the binding's {@link #bindingElement()}) that are associated with the
55    * binding.
56    *
57    * <ul>
58    *   <li>{@linkplain BindsOptionalOf optional binding} declarations
59    *   <li>{@linkplain Module#subcomponents() module subcomponent} declarations
60    *   <li>{@linkplain Multibinds multibinding} declarations
61    * </ul>
62    */
associatedDeclarations()63   public final Iterable<Declaration> associatedDeclarations() {
64     return Iterables.concat(
65         multibindingDeclarations(), optionalBindingDeclarations(), subcomponentDeclarations());
66   }
67 
68   @Override
key()69   public Key key() {
70     return delegate().key();
71   }
72 
73   @Override
dependencies()74   public ImmutableSet<DependencyRequest> dependencies() {
75     return delegate().dependencies();
76   }
77 
78   @Override
bindingElement()79   public Optional<DaggerElement> bindingElement() {
80     return delegate().bindingElement().map(DaggerElement::from);
81   }
82 
83   @Override
contributingModule()84   public Optional<DaggerTypeElement> contributingModule() {
85     return delegate().contributingModule().map(DaggerTypeElement::from);
86   }
87 
88   @Override
requiresModuleInstance()89   public boolean requiresModuleInstance() {
90     return delegate().requiresModuleInstance();
91   }
92 
93   @Override
scope()94   public Optional<Scope> scope() {
95     return delegate().scope();
96   }
97 
98   @Override
isNullable()99   public boolean isNullable() {
100     return delegate().isNullable();
101   }
102 
103   @Override
isProduction()104   public boolean isProduction() {
105     return delegate().bindingType().equals(PRODUCTION);
106   }
107 
108   @Override
kind()109   public BindingKind kind() {
110     return delegate().kind();
111   }
112 
113   @Override
toString()114   public final String toString() {
115     return declarationFormatter.format(delegate());
116   }
117 
withBindingType(BindingType bindingType)118   public BindingNode withBindingType(BindingType bindingType) {
119     return create(
120         componentPath(),
121         ((ContributionBinding) delegate()).withBindingType(bindingType),
122         multibindingDeclarations(),
123         optionalBindingDeclarations(),
124         subcomponentDeclarations(),
125         declarationFormatter);
126   }
127 
128   static final class Factory {
129     private final DeclarationFormatter declarationFormatter;
130 
131     @Inject
Factory(DeclarationFormatter declarationFormatter)132     Factory(DeclarationFormatter declarationFormatter) {
133       this.declarationFormatter = declarationFormatter;
134     }
135 
forContributionBindings( ComponentPath component, ContributionBinding delegate, Iterable<MultibindingDeclaration> multibindingDeclarations, Iterable<OptionalBindingDeclaration> optionalBindingDeclarations, Iterable<SubcomponentDeclaration> subcomponentDeclarations)136     public BindingNode forContributionBindings(
137         ComponentPath component,
138         ContributionBinding delegate,
139         Iterable<MultibindingDeclaration> multibindingDeclarations,
140         Iterable<OptionalBindingDeclaration> optionalBindingDeclarations,
141         Iterable<SubcomponentDeclaration> subcomponentDeclarations) {
142       return create(
143           component,
144           delegate,
145           ImmutableSet.copyOf(multibindingDeclarations),
146           ImmutableSet.copyOf(optionalBindingDeclarations),
147           ImmutableSet.copyOf(subcomponentDeclarations));
148     }
149 
forMembersInjectionBinding( ComponentPath component, MembersInjectionBinding delegate)150     public BindingNode forMembersInjectionBinding(
151         ComponentPath component, MembersInjectionBinding delegate) {
152       return create(component, delegate, ImmutableSet.of(), ImmutableSet.of(), ImmutableSet.of());
153     }
154 
create( ComponentPath component, Binding delegate, ImmutableSet<MultibindingDeclaration> multibindingDeclarations, ImmutableSet<OptionalBindingDeclaration> optionalBindingDeclarations, ImmutableSet<SubcomponentDeclaration> subcomponentDeclarations)155     private BindingNode create(
156         ComponentPath component,
157         Binding delegate,
158         ImmutableSet<MultibindingDeclaration> multibindingDeclarations,
159         ImmutableSet<OptionalBindingDeclaration> optionalBindingDeclarations,
160         ImmutableSet<SubcomponentDeclaration> subcomponentDeclarations) {
161       return BindingNode.create(
162           component,
163           delegate,
164           multibindingDeclarations,
165           optionalBindingDeclarations,
166           subcomponentDeclarations,
167           declarationFormatter);
168     }
169   }
170 
create( ComponentPath component, Binding delegate, ImmutableSet<MultibindingDeclaration> multibindingDeclarations, ImmutableSet<OptionalBindingDeclaration> optionalBindingDeclarations, ImmutableSet<SubcomponentDeclaration> subcomponentDeclarations, DeclarationFormatter declarationFormatter)171   private static BindingNode create(
172       ComponentPath component,
173       Binding delegate,
174       ImmutableSet<MultibindingDeclaration> multibindingDeclarations,
175       ImmutableSet<OptionalBindingDeclaration> optionalBindingDeclarations,
176       ImmutableSet<SubcomponentDeclaration> subcomponentDeclarations,
177       DeclarationFormatter declarationFormatter) {
178     BindingNode node =
179         new AutoValue_BindingNode(
180             component,
181             delegate,
182             multibindingDeclarations,
183             optionalBindingDeclarations,
184             subcomponentDeclarations);
185     node.declarationFormatter = declarationFormatter;
186     return node;
187   }
188 }
189