xref: /aosp_15_r20/external/dagger2/java/dagger/spi/model/DependencyRequest.java (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
1 /*
2  * Copyright (C) 2021 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.spi.model;
18 
19 import com.google.auto.value.AutoValue;
20 import dagger.Provides;
21 import java.util.Optional;
22 import javax.inject.Inject;
23 
24 /**
25  * Represents a request for a {@link Key} at an injection point. For example, parameters to {@link
26  * Inject} constructors, {@link Provides} methods, and component methods are all dependency
27  * requests.
28  *
29  * <p id="synthetic">A dependency request is considered to be <em>synthetic</em> if it does not have
30  * an {@link DaggerElement} in code that requests the key directly. For example, an {@link
31  * java.util.concurrent.Executor} is required for all {@code @Produces} methods to run
32  * asynchronously even though it is not directly specified as a parameter to the binding method.
33  */
34 @AutoValue
35 public abstract class DependencyRequest {
36   /** The kind of this request. */
kind()37   public abstract RequestKind kind();
38 
39   /** The key of this request. */
key()40   public abstract Key key();
41 
42   /**
43    * The element that declares this dependency request. Absent for <a href="#synthetic">synthetic
44    * </a> requests.
45    */
requestElement()46   public abstract Optional<DaggerElement> requestElement();
47 
48   /**
49    * Returns {@code true} if this request allows null objects. A request is nullable if it is
50    * has an annotation with "Nullable" as its simple name.
51    */
isNullable()52   public abstract boolean isNullable();
53 
54   /** Returns a new builder of dependency requests. */
builder()55   public static DependencyRequest.Builder builder() {
56     return new AutoValue_DependencyRequest.Builder().isNullable(false);
57   }
58 
59   /** A builder of {@link DependencyRequest}s. */
60   @AutoValue.Builder
61   public abstract static class Builder {
kind(RequestKind kind)62     public abstract Builder kind(RequestKind kind);
63 
key(Key key)64     public abstract Builder key(Key key);
65 
requestElement(DaggerElement element)66     public abstract Builder requestElement(DaggerElement element);
67 
isNullable(boolean isNullable)68     public abstract Builder isNullable(boolean isNullable);
69 
build()70     public abstract DependencyRequest build();
71   }
72 }
73