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.writing; 18 19 import com.squareup.javapoet.ClassName; 20 import com.squareup.javapoet.CodeBlock; 21 import dagger.internal.codegen.binding.ComponentRequirement; 22 23 /** 24 * A factory for expressions of {@link ComponentRequirement}s in the generated component. This is 25 * <em>not</em> a {@link RequestRepresentation}, since {@link ComponentRequirement}s do not have a 26 * {@link dagger.internal.codegen.model.Key}. See {@link ComponentRequirementRequestRepresentation} 27 * for binding expressions that are themselves a component requirement. 28 */ 29 interface ComponentRequirementExpression { 30 /** 31 * Returns an expression for the {@link ComponentRequirement} to be used when implementing a 32 * component method. This may add a field or method to the component in order to reference the 33 * component requirement outside of the {@code initialize()} methods. 34 */ getExpression(ClassName requestingClass)35 CodeBlock getExpression(ClassName requestingClass); 36 37 /** 38 * Returns an expression for the {@link ComponentRequirement} to be used only within {@code 39 * initialize()} methods, where the constructor parameters are available. 40 * 41 * <p>When accessing this expression from a subcomponent, this may cause a field to be initialized 42 * or a method to be added in the component that owns this {@link ComponentRequirement}. 43 */ getExpressionDuringInitialization(ClassName requestingClass)44 default CodeBlock getExpressionDuringInitialization(ClassName requestingClass) { 45 return getExpression(requestingClass); 46 } 47 48 /** 49 * Returns the expression for the {@link ComponentRequirement} to be used when reimplementing a 50 * modifiable module method. 51 */ getModifiableModuleMethodExpression(ClassName requestingClass)52 default CodeBlock getModifiableModuleMethodExpression(ClassName requestingClass) { 53 return CodeBlock.of("return $L", getExpression(requestingClass)); 54 } 55 } 56