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; 18 19 import static com.google.common.base.Preconditions.checkNotNull; 20 import static dagger.internal.codegen.BindingType.PRODUCTION; 21 22 import com.google.auto.value.AutoValue; 23 import com.google.common.collect.ImmutableSet; 24 import com.google.common.collect.Iterables; 25 import dagger.BindsOptionalOf; 26 import dagger.Module; 27 import dagger.model.BindingKind; 28 import dagger.model.ComponentPath; 29 import dagger.model.DependencyRequest; 30 import dagger.model.Key; 31 import dagger.model.Scope; 32 import dagger.multibindings.Multibinds; 33 import java.util.Optional; 34 import javax.lang.model.element.Element; 35 import javax.lang.model.element.TypeElement; 36 37 /** 38 * An implementation of {@link dagger.model.Binding} that also exposes {@link BindingDeclaration}s 39 * associated with the binding. 40 */ 41 // TODO(dpb): Consider a supertype of dagger.model.Binding that dagger.internal.codegen.Binding 42 // could also implement. 43 @AutoValue 44 abstract class BindingNode implements dagger.model.Binding { create( ComponentPath component, Binding delegate, ImmutableSet<MultibindingDeclaration> multibindingDeclarations, ImmutableSet<OptionalBindingDeclaration> optionalBindingDeclarations, ImmutableSet<SubcomponentDeclaration> subcomponentDeclarations, BindingDeclarationFormatter bindingDeclarationFormatter)45 static BindingNode create( 46 ComponentPath component, 47 Binding delegate, 48 ImmutableSet<MultibindingDeclaration> multibindingDeclarations, 49 ImmutableSet<OptionalBindingDeclaration> optionalBindingDeclarations, 50 ImmutableSet<SubcomponentDeclaration> subcomponentDeclarations, 51 BindingDeclarationFormatter bindingDeclarationFormatter) { 52 BindingNode node = 53 new AutoValue_BindingNode( 54 component, 55 delegate, 56 multibindingDeclarations, 57 optionalBindingDeclarations, 58 subcomponentDeclarations); 59 node.bindingDeclarationFormatter = checkNotNull(bindingDeclarationFormatter); 60 return node; 61 } 62 63 private BindingDeclarationFormatter bindingDeclarationFormatter; 64 delegate()65 abstract Binding delegate(); 66 multibindingDeclarations()67 abstract ImmutableSet<MultibindingDeclaration> multibindingDeclarations(); 68 optionalBindingDeclarations()69 abstract ImmutableSet<OptionalBindingDeclaration> optionalBindingDeclarations(); 70 subcomponentDeclarations()71 abstract ImmutableSet<SubcomponentDeclaration> subcomponentDeclarations(); 72 73 /** 74 * The {@link Element}s (other than the binding's {@link #bindingElement()}) that are associated 75 * with the binding. 76 * 77 * <ul> 78 * <li>{@linkplain BindsOptionalOf optional binding} declarations 79 * <li>{@linkplain Module#subcomponents() module subcomponent} declarations 80 * <li>{@linkplain Multibinds multibinding} declarations 81 * </ul> 82 */ associatedDeclarations()83 final Iterable<BindingDeclaration> associatedDeclarations() { 84 return Iterables.concat( 85 multibindingDeclarations(), optionalBindingDeclarations(), subcomponentDeclarations()); 86 } 87 88 @Override key()89 public Key key() { 90 return delegate().key(); 91 } 92 93 @Override dependencies()94 public ImmutableSet<DependencyRequest> dependencies() { 95 return delegate().dependencies(); 96 } 97 98 @Override bindingElement()99 public Optional<Element> bindingElement() { 100 return delegate().bindingElement(); 101 } 102 103 @Override contributingModule()104 public Optional<TypeElement> contributingModule() { 105 return delegate().contributingModule(); 106 } 107 108 @Override requiresModuleInstance()109 public boolean requiresModuleInstance() { 110 return delegate().requiresModuleInstance(); 111 } 112 113 @Override scope()114 public Optional<Scope> scope() { 115 return delegate().scope(); 116 } 117 118 @Override isNullable()119 public boolean isNullable() { 120 return delegate().isNullable(); 121 } 122 123 @Override isProduction()124 public boolean isProduction() { 125 return delegate().bindingType().equals(PRODUCTION); 126 } 127 128 @Override kind()129 public BindingKind kind() { 130 return delegate().kind(); 131 } 132 133 @Override toString()134 public final String toString() { 135 return bindingDeclarationFormatter.format(delegate()); 136 } 137 } 138