1 /* 2 * Copyright (C) 2015 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; 18 19 import dagger.internal.codegen.langmodel.DaggerElements; 20 import dagger.model.BindingKind; 21 import dagger.model.Key; 22 import java.util.Optional; 23 import javax.lang.model.element.Element; 24 import javax.lang.model.element.TypeElement; 25 26 /** An object that declares or specifies a binding. */ 27 abstract class BindingDeclaration { 28 29 /** The {@link Key} of this declaration. */ key()30 abstract Key key(); 31 32 /** 33 * The {@link Element} that declares this binding. Absent for {@linkplain BindingKind binding 34 * kinds} that are not always declared by exactly one element. 35 * 36 * <p>For example, consider {@link BindingKind#MULTIBOUND_SET}. A component with many 37 * {@code @IntoSet} bindings for the same key will have a synthetic binding that depends on all 38 * contributions, but with no identifiying binding element. A {@code @Multibinds} method will also 39 * contribute a synthetic binding, but since multiple {@code @Multibinds} methods can coexist in 40 * the same component (and contribute to one single binding), it has no binding element. 41 */ 42 // TODO(ronshapiro): examine whether this wildcard+bound have any benefit. 43 // We never actually refer to the overridden bindingElement methods directly in a way which needs 44 // anything more than an Element. Removing the wildcard would allow for simpler user-written code 45 // when the binding element is passed to a method. bindingElement()46 abstract Optional<Element> bindingElement(); 47 48 /** 49 * The type enclosing the {@link #bindingElement()}, or {@link Optional#empty()} if {@link 50 * #bindingElement()} is empty. 51 */ bindingTypeElement()52 final Optional<TypeElement> bindingTypeElement() { 53 return bindingElement().map(DaggerElements::closestEnclosingTypeElement); 54 } 55 56 /** 57 * The installed module class that contributed the {@link #bindingElement()}. May be a subclass of 58 * the class that contains {@link #bindingElement()}. Absent if {@link #bindingElement()} is 59 * empty. 60 */ contributingModule()61 abstract Optional<TypeElement> contributingModule(); 62 } 63