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 androidx.room.compiler.processing.compat.XConverters.toJavac; 20 import static dagger.internal.codegen.base.RequestKinds.requestType; 21 22 import androidx.room.compiler.processing.XType; 23 import com.google.auto.value.AutoValue; 24 import dagger.internal.codegen.langmodel.DaggerTypes; 25 import dagger.spi.model.DependencyRequest; 26 import dagger.spi.model.Key; 27 import dagger.spi.model.RequestKind; 28 import java.util.Optional; 29 import javax.lang.model.type.TypeMirror; 30 31 /** 32 * A request for a binding, which may be in the form of a request for a dependency to pass to a 33 * constructor or module method ({@link RequestKind}) or an internal request for a framework 34 * instance ({@link FrameworkType}). 35 */ 36 @AutoValue 37 public abstract class BindingRequest { 38 /** Creates a {@link BindingRequest} for the given {@link DependencyRequest}. */ bindingRequest(DependencyRequest dependencyRequest)39 public static BindingRequest bindingRequest(DependencyRequest dependencyRequest) { 40 return bindingRequest(dependencyRequest.key(), dependencyRequest.kind()); 41 } 42 43 /** 44 * Creates a {@link BindingRequest} for a normal dependency request for the given {@link Key} and 45 * {@link RequestKind}. 46 */ bindingRequest(Key key, RequestKind requestKind)47 public static BindingRequest bindingRequest(Key key, RequestKind requestKind) { 48 // When there's a request that has a 1:1 mapping to a FrameworkType, the request should be 49 // associated with that FrameworkType as well, because we want to ensure that if a request 50 // comes in for that as a dependency first and as a framework instance later, they resolve to 51 // the same binding expression. 52 // TODO(cgdecker): Instead of doing this, make ComponentRequestRepresentations create a 53 // RequestRepresentation for the RequestKind that simply delegates to the RequestRepresentation 54 // for the FrameworkType. Then there are separate RequestRepresentations, but we don't end up 55 // doing weird things like creating two fields when there should only be one. 56 return new AutoValue_BindingRequest( 57 key, requestKind, FrameworkType.forRequestKind(requestKind)); 58 } 59 60 /** 61 * Creates a {@link BindingRequest} for a request for a framework instance for the given {@link 62 * Key} with the given {@link FrameworkType}. 63 */ bindingRequest(Key key, FrameworkType frameworkType)64 public static BindingRequest bindingRequest(Key key, FrameworkType frameworkType) { 65 return new AutoValue_BindingRequest( 66 key, frameworkType.requestKind(), Optional.of(frameworkType)); 67 } 68 69 /** Returns the {@link Key} for the requested binding. */ key()70 public abstract Key key(); 71 72 /** Returns the request kind associated with this request. */ requestKind()73 public abstract RequestKind requestKind(); 74 75 /** Returns the framework type associated with this request, if any. */ frameworkType()76 public abstract Optional<FrameworkType> frameworkType(); 77 78 /** Returns whether this request is of the given kind. */ isRequestKind(RequestKind requestKind)79 public final boolean isRequestKind(RequestKind requestKind) { 80 return requestKind.equals(requestKind()); 81 } 82 requestedType(XType contributedType, DaggerTypes types)83 public final TypeMirror requestedType(XType contributedType, DaggerTypes types) { 84 return requestedType(toJavac(contributedType), types); 85 } 86 requestedType(TypeMirror contributedType, DaggerTypes types)87 public final TypeMirror requestedType(TypeMirror contributedType, DaggerTypes types) { 88 return requestType(requestKind(), contributedType, types); 89 } 90 91 /** Returns a name that can be used for the kind of request this is. */ kindName()92 public final String kindName() { 93 return requestKind().toString(); 94 } 95 } 96