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