• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.common.base.Preconditions.checkArgument;
20 import static dagger.internal.codegen.MapKeys.getMapKey;
21 import static dagger.internal.codegen.MoreAnnotationMirrors.wrapOptionalInEquivalence;
22 
23 import com.google.auto.common.MoreElements;
24 import com.google.auto.common.MoreTypes;
25 import com.google.auto.value.AutoValue;
26 import com.google.auto.value.extension.memoized.Memoized;
27 import com.google.common.base.Equivalence;
28 import com.google.common.collect.Iterables;
29 import dagger.Binds;
30 import dagger.internal.codegen.ContributionType.HasContributionType;
31 import dagger.internal.codegen.langmodel.DaggerTypes;
32 import dagger.model.DependencyRequest;
33 import java.util.Optional;
34 import javax.inject.Inject;
35 import javax.lang.model.element.AnnotationMirror;
36 import javax.lang.model.element.Element;
37 import javax.lang.model.element.ExecutableElement;
38 import javax.lang.model.element.TypeElement;
39 import javax.lang.model.type.ExecutableType;
40 
41 /**
42  * The declaration for a delegate binding established by a {@link Binds} method.
43  */
44 @AutoValue
45 abstract class DelegateDeclaration extends BindingDeclaration implements HasContributionType {
delegateRequest()46   abstract DependencyRequest delegateRequest();
47 
wrappedMapKey()48   abstract Optional<Equivalence.Wrapper<AnnotationMirror>> wrappedMapKey();
49 
50   @Memoized
51   @Override
hashCode()52   public abstract int hashCode();
53 
54   @Override
equals(Object obj)55   public abstract boolean equals(Object obj);
56 
57   static final class Factory {
58     private final DaggerTypes types;
59     private final KeyFactory keyFactory;
60     private final DependencyRequestFactory dependencyRequestFactory;
61 
62     @Inject
Factory( DaggerTypes types, KeyFactory keyFactory, DependencyRequestFactory dependencyRequestFactory)63     Factory(
64         DaggerTypes types,
65         KeyFactory keyFactory,
66         DependencyRequestFactory dependencyRequestFactory) {
67       this.types = types;
68       this.keyFactory = keyFactory;
69       this.dependencyRequestFactory = dependencyRequestFactory;
70     }
71 
create( ExecutableElement bindsMethod, TypeElement contributingModule)72     DelegateDeclaration create(
73         ExecutableElement bindsMethod, TypeElement contributingModule) {
74       checkArgument(MoreElements.isAnnotationPresent(bindsMethod, Binds.class));
75       ExecutableType resolvedMethod =
76           MoreTypes.asExecutable(
77               types.asMemberOf(MoreTypes.asDeclared(contributingModule.asType()), bindsMethod));
78       DependencyRequest delegateRequest =
79           dependencyRequestFactory.forRequiredResolvedVariable(
80               Iterables.getOnlyElement(bindsMethod.getParameters()),
81               Iterables.getOnlyElement(resolvedMethod.getParameterTypes()));
82       return new AutoValue_DelegateDeclaration(
83           ContributionType.fromBindingElement(bindsMethod),
84           keyFactory.forBindsMethod(bindsMethod, contributingModule),
85           Optional.<Element>of(bindsMethod),
86           Optional.of(contributingModule),
87           delegateRequest,
88           wrapOptionalInEquivalence(getMapKey(bindsMethod)));
89     }
90   }
91 }
92