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