• 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 com.google.auto.value.AutoValue;
22 import dagger.internal.codegen.langmodel.DaggerTypes;
23 import dagger.model.DependencyRequest;
24 import dagger.model.Key;
25 import dagger.model.RequestKind;
26 import java.util.Optional;
27 import javax.lang.model.type.TypeMirror;
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 ComponentBindingExpressions create a
51     // BindingExpression for the RequestKind that simply delegates to the BindingExpression for the
52     // FrameworkType. Then there are separate BindingExpressions, but we don't end up doing weird
53     // things like creating two fields when there should only be one.
54     return new AutoValue_BindingRequest(
55         key, Optional.of(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, if any. */
requestKind()71   public abstract Optional<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().orElse(null));
79   }
80 
requestedType(TypeMirror contributedType, DaggerTypes types)81   public final TypeMirror requestedType(TypeMirror contributedType, DaggerTypes types) {
82     if (requestKind().isPresent()) {
83       return requestType(requestKind().get(), contributedType, types);
84     }
85     return types.wrapType(contributedType, frameworkType().get().frameworkClass());
86   }
87 
88   /** Returns a name that can be used for the kind of request this is. */
kindName()89   public final String kindName() {
90     Object requestKindObject =
91         requestKind().isPresent()
92             ? requestKind().get()
93             : frameworkType().get().frameworkClass().getSimpleName();
94     return requestKindObject.toString();
95   }
96 }
97