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.internal.codegen.binding; 18 19 import static com.google.auto.common.MoreElements.isAnnotationPresent; 20 import static java.util.stream.Collectors.toList; 21 22 import com.google.auto.value.AutoValue; 23 import com.google.auto.value.extension.memoized.Memoized; 24 import com.google.common.collect.ImmutableSet; 25 import com.google.common.collect.ImmutableSortedSet; 26 import dagger.model.BindingKind; 27 import dagger.model.DependencyRequest; 28 import java.util.Optional; 29 import javax.inject.Inject; 30 import javax.lang.model.element.Element; 31 import javax.lang.model.element.ExecutableElement; 32 import javax.lang.model.element.Modifier; 33 import javax.lang.model.element.TypeElement; 34 import javax.lang.model.element.VariableElement; 35 36 /** Represents the full members injection of a particular type. */ 37 @AutoValue 38 public abstract class MembersInjectionBinding extends Binding { 39 @Override bindingElement()40 public final Optional<Element> bindingElement() { 41 return Optional.of(membersInjectedType()); 42 } 43 membersInjectedType()44 public abstract TypeElement membersInjectedType(); 45 46 @Override unresolved()47 public abstract Optional<MembersInjectionBinding> unresolved(); 48 49 @Override contributingModule()50 public Optional<TypeElement> contributingModule() { 51 return Optional.empty(); 52 } 53 54 /** The set of individual sites where {@link Inject} is applied. */ injectionSites()55 public abstract ImmutableSortedSet<InjectionSite> injectionSites(); 56 57 @Override bindingType()58 public BindingType bindingType() { 59 return BindingType.MEMBERS_INJECTION; 60 } 61 62 @Override kind()63 public BindingKind kind() { 64 return BindingKind.MEMBERS_INJECTION; 65 } 66 67 @Override isNullable()68 public boolean isNullable() { 69 return false; 70 } 71 72 /** 73 * Returns {@code true} if any of this binding's injection sites are directly on the bound type. 74 */ hasLocalInjectionSites()75 public boolean hasLocalInjectionSites() { 76 return injectionSites() 77 .stream() 78 .anyMatch( 79 injectionSite -> 80 injectionSite.element().getEnclosingElement().equals(membersInjectedType())); 81 } 82 83 @Override requiresModuleInstance()84 public boolean requiresModuleInstance() { 85 return false; 86 } 87 88 @Memoized 89 @Override hashCode()90 public abstract int hashCode(); 91 92 // TODO(ronshapiro,dpb): simplify the equality semantics 93 @Override equals(Object obj)94 public abstract boolean equals(Object obj); 95 96 /** Metadata about a field or method injection site. */ 97 @AutoValue 98 public abstract static class InjectionSite { 99 /** The type of injection site. */ 100 public enum Kind { 101 FIELD, 102 METHOD, 103 } 104 kind()105 public abstract Kind kind(); 106 element()107 public abstract Element element(); 108 dependencies()109 public abstract ImmutableSet<DependencyRequest> dependencies(); 110 111 /** 112 * Returns the index of {@link #element()} in its parents {@code @Inject} members that have the 113 * same simple name. This method filters out private elements so that the results will be 114 * consistent independent of whether the build system uses header jars or not. 115 */ 116 @Memoized indexAmongAtInjectMembersWithSameSimpleName()117 public int indexAmongAtInjectMembersWithSameSimpleName() { 118 return element() 119 .getEnclosingElement() 120 .getEnclosedElements() 121 .stream() 122 .filter(element -> isAnnotationPresent(element, Inject.class)) 123 .filter(element -> !element.getModifiers().contains(Modifier.PRIVATE)) 124 .filter(element -> element.getSimpleName().equals(this.element().getSimpleName())) 125 .collect(toList()) 126 .indexOf(element()); 127 } 128 field(VariableElement element, DependencyRequest dependency)129 public static InjectionSite field(VariableElement element, DependencyRequest dependency) { 130 return new AutoValue_MembersInjectionBinding_InjectionSite( 131 Kind.FIELD, element, ImmutableSet.of(dependency)); 132 } 133 method( ExecutableElement element, Iterable<DependencyRequest> dependencies)134 public static InjectionSite method( 135 ExecutableElement element, Iterable<DependencyRequest> dependencies) { 136 return new AutoValue_MembersInjectionBinding_InjectionSite( 137 Kind.METHOD, element, ImmutableSet.copyOf(dependencies)); 138 } 139 } 140 } 141