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