• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
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 /**
37  * Represents the full members injection of a particular type.
38  */
39 @AutoValue
40 abstract class MembersInjectionBinding extends Binding {
41   @Override
bindingElement()42   public final Optional<Element> bindingElement() {
43     return Optional.of(membersInjectedType());
44   }
45 
membersInjectedType()46   abstract TypeElement membersInjectedType();
47 
48   @Override
unresolved()49   abstract Optional<MembersInjectionBinding> unresolved();
50 
51   @Override
contributingModule()52   public Optional<TypeElement> contributingModule() {
53     return Optional.empty();
54   }
55 
56   /** The set of individual sites where {@link Inject} is applied. */
injectionSites()57   abstract ImmutableSortedSet<InjectionSite> injectionSites();
58 
59   @Override
bindingType()60   BindingType bindingType() {
61     return BindingType.MEMBERS_INJECTION;
62   }
63 
64   @Override
kind()65   public BindingKind kind() {
66     return BindingKind.MEMBERS_INJECTION;
67   }
68 
69   @Override
isNullable()70   public boolean isNullable() {
71     return false;
72   }
73 
74   /**
75    * Returns {@code true} if any of this binding's injection sites are directly on the bound type.
76    */
hasLocalInjectionSites()77   boolean hasLocalInjectionSites() {
78     return injectionSites()
79         .stream()
80         .anyMatch(
81             injectionSite ->
82                 injectionSite.element().getEnclosingElement().equals(membersInjectedType()));
83   }
84 
85   @Override
requiresModuleInstance()86   boolean requiresModuleInstance() {
87     return false;
88   }
89 
90   @Memoized
91   @Override
hashCode()92   public abstract int hashCode();
93 
94   // TODO(ronshapiro,dpb): simplify the equality semantics
95   @Override
equals(Object obj)96   public abstract boolean equals(Object obj);
97 
98   @AutoValue
99   abstract static class InjectionSite {
100     enum Kind {
101       FIELD,
102       METHOD,
103     }
104 
kind()105     abstract Kind kind();
106 
element()107     abstract Element element();
108 
dependencies()109     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     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     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     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