• 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.binding;
18 
19 import static com.google.common.base.Preconditions.checkArgument;
20 import static dagger.internal.codegen.base.MoreAnnotationMirrors.wrapOptionalInEquivalence;
21 import static dagger.internal.codegen.binding.MapKeys.getMapKey;
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.base.ContributionType;
31 import dagger.internal.codegen.base.ContributionType.HasContributionType;
32 import dagger.internal.codegen.langmodel.DaggerTypes;
33 import dagger.model.DependencyRequest;
34 import java.util.Optional;
35 import javax.inject.Inject;
36 import javax.lang.model.element.AnnotationMirror;
37 import javax.lang.model.element.Element;
38 import javax.lang.model.element.ExecutableElement;
39 import javax.lang.model.element.TypeElement;
40 import javax.lang.model.type.ExecutableType;
41 
42 /** The declaration for a delegate binding established by a {@link Binds} method. */
43 @AutoValue
44 public abstract class DelegateDeclaration extends BindingDeclaration
45     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   /** A {@link DelegateDeclaration} factory. */
58   public static final class Factory {
59     private final DaggerTypes types;
60     private final KeyFactory keyFactory;
61     private final DependencyRequestFactory dependencyRequestFactory;
62 
63     @Inject
Factory( DaggerTypes types, KeyFactory keyFactory, DependencyRequestFactory dependencyRequestFactory)64     Factory(
65         DaggerTypes types,
66         KeyFactory keyFactory,
67         DependencyRequestFactory dependencyRequestFactory) {
68       this.types = types;
69       this.keyFactory = keyFactory;
70       this.dependencyRequestFactory = dependencyRequestFactory;
71     }
72 
create( ExecutableElement bindsMethod, TypeElement contributingModule)73     public DelegateDeclaration create(
74         ExecutableElement bindsMethod, TypeElement contributingModule) {
75       checkArgument(MoreElements.isAnnotationPresent(bindsMethod, Binds.class));
76       ExecutableType resolvedMethod =
77           MoreTypes.asExecutable(
78               types.asMemberOf(MoreTypes.asDeclared(contributingModule.asType()), bindsMethod));
79       DependencyRequest delegateRequest =
80           dependencyRequestFactory.forRequiredResolvedVariable(
81               Iterables.getOnlyElement(bindsMethod.getParameters()),
82               Iterables.getOnlyElement(resolvedMethod.getParameterTypes()));
83       return new AutoValue_DelegateDeclaration(
84           ContributionType.fromBindingElement(bindsMethod),
85           keyFactory.forBindsMethod(bindsMethod, contributingModule),
86           Optional.<Element>of(bindsMethod),
87           Optional.of(contributingModule),
88           delegateRequest,
89           wrapOptionalInEquivalence(getMapKey(bindsMethod)));
90     }
91   }
92 }
93