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 static com.google.common.collect.Iterables.getOnlyElement; 20 21 import androidx.room.compiler.processing.XExecutableParameterElement; 22 import androidx.room.compiler.processing.XMethodElement; 23 import com.squareup.javapoet.ClassName; 24 import com.squareup.javapoet.CodeBlock; 25 import dagger.assisted.Assisted; 26 import dagger.assisted.AssistedFactory; 27 import dagger.assisted.AssistedInject; 28 import dagger.internal.codegen.binding.ComponentDescriptor.ComponentMethodDescriptor; 29 import dagger.internal.codegen.binding.MembersInjectionBinding; 30 import dagger.internal.codegen.javapoet.Expression; 31 32 /** 33 * A binding expression for members injection component methods. See {@link 34 * MembersInjectionMethods}. 35 */ 36 final class MembersInjectionRequestRepresentation extends RequestRepresentation { 37 private final MembersInjectionBinding binding; 38 private final MembersInjectionMethods membersInjectionMethods; 39 40 @AssistedInject MembersInjectionRequestRepresentation( @ssisted MembersInjectionBinding binding, MembersInjectionMethods membersInjectionMethods)41 MembersInjectionRequestRepresentation( 42 @Assisted MembersInjectionBinding binding, MembersInjectionMethods membersInjectionMethods) { 43 this.binding = binding; 44 this.membersInjectionMethods = membersInjectionMethods; 45 } 46 47 @Override getDependencyExpression(ClassName requestingClass)48 Expression getDependencyExpression(ClassName requestingClass) { 49 throw new UnsupportedOperationException(binding.toString()); 50 } 51 52 @Override getDependencyExpressionForComponentMethod( ComponentMethodDescriptor componentMethod, ComponentImplementation component)53 protected Expression getDependencyExpressionForComponentMethod( 54 ComponentMethodDescriptor componentMethod, ComponentImplementation component) { 55 XMethodElement methodElement = componentMethod.methodElement(); 56 XExecutableParameterElement parameter = getOnlyElement(methodElement.getParameters()); 57 return membersInjectionMethods.getInjectExpression( 58 binding.key(), CodeBlock.of("$L", parameter.getJvmName()), component.name()); 59 } 60 61 // TODO(bcorso): Consider making this a method on all RequestRepresentations. 62 /** Returns the binding associated with this {@link RequestRepresentation}. */ binding()63 MembersInjectionBinding binding() { 64 return binding; 65 } 66 67 @AssistedFactory 68 static interface Factory { create(MembersInjectionBinding binding)69 MembersInjectionRequestRepresentation create(MembersInjectionBinding binding); 70 } 71 } 72