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.internal.codegen; 18 19 import static com.google.common.base.Preconditions.checkNotNull; 20 21 import com.squareup.javapoet.ClassName; 22 import com.squareup.javapoet.CodeBlock; 23 import dagger.internal.Preconditions; 24 import dagger.internal.codegen.javapoet.Expression; 25 26 /** A binding expression for component provision methods. */ 27 final class ComponentProvisionBindingExpression extends SimpleInvocationBindingExpression { 28 private final ProvisionBinding binding; 29 private final BindingGraph bindingGraph; 30 private final ComponentRequirementExpressions componentRequirementExpressions; 31 private final CompilerOptions compilerOptions; 32 ComponentProvisionBindingExpression( ResolvedBindings resolvedBindings, BindingGraph bindingGraph, ComponentRequirementExpressions componentRequirementExpressions, CompilerOptions compilerOptions)33 ComponentProvisionBindingExpression( 34 ResolvedBindings resolvedBindings, 35 BindingGraph bindingGraph, 36 ComponentRequirementExpressions componentRequirementExpressions, 37 CompilerOptions compilerOptions) { 38 super(resolvedBindings); 39 this.binding = (ProvisionBinding) resolvedBindings.contributionBinding(); 40 this.bindingGraph = checkNotNull(bindingGraph); 41 this.componentRequirementExpressions = checkNotNull(componentRequirementExpressions); 42 this.compilerOptions = checkNotNull(compilerOptions); 43 } 44 45 @Override getDependencyExpression(ClassName requestingClass)46 Expression getDependencyExpression(ClassName requestingClass) { 47 CodeBlock invocation = 48 CodeBlock.of( 49 "$L.$L()", 50 componentRequirementExpressions.getExpression(componentRequirement(), requestingClass), 51 binding.bindingElement().get().getSimpleName()); 52 return Expression.create( 53 binding.contributedPrimitiveType().orElse(binding.key().type()), 54 maybeCheckForNull(binding, compilerOptions, invocation)); 55 } 56 componentRequirement()57 private ComponentRequirement componentRequirement() { 58 return bindingGraph 59 .componentDescriptor() 60 .getDependencyThatDefinesMethod(binding.bindingElement().get()); 61 } 62 maybeCheckForNull( ProvisionBinding binding, CompilerOptions compilerOptions, CodeBlock invocation)63 static CodeBlock maybeCheckForNull( 64 ProvisionBinding binding, CompilerOptions compilerOptions, CodeBlock invocation) { 65 return binding.shouldCheckForNull(compilerOptions) 66 ? CodeBlock.of( 67 "$T.checkNotNull($L, $S)", 68 Preconditions.class, 69 invocation, 70 "Cannot return null from a non-@Nullable component method") 71 : invocation; 72 } 73 } 74