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