• 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 androidx.room.compiler.processing.compat.XConverters.toJavac;
20 import static com.google.common.base.Preconditions.checkArgument;
21 import static dagger.internal.codegen.base.MoreAnnotationMirrors.wrapOptionalInEquivalence;
22 import static dagger.internal.codegen.binding.MapKeys.getMapKey;
23 
24 import androidx.room.compiler.processing.XElement;
25 import androidx.room.compiler.processing.XMethodElement;
26 import androidx.room.compiler.processing.XMethodType;
27 import androidx.room.compiler.processing.XTypeElement;
28 import com.google.auto.value.AutoValue;
29 import com.google.auto.value.extension.memoized.Memoized;
30 import com.google.common.base.Equivalence;
31 import com.google.common.collect.Iterables;
32 import dagger.Binds;
33 import dagger.internal.codegen.base.ContributionType;
34 import dagger.internal.codegen.base.ContributionType.HasContributionType;
35 import dagger.internal.codegen.javapoet.TypeNames;
36 import dagger.spi.model.DependencyRequest;
37 import java.util.Optional;
38 import javax.inject.Inject;
39 import javax.lang.model.element.AnnotationMirror;
40 
41 /** The declaration for a delegate binding established by a {@link Binds} method. */
42 @AutoValue
43 public abstract class DelegateDeclaration extends BindingDeclaration
44     implements HasContributionType {
delegateRequest()45   abstract DependencyRequest delegateRequest();
46 
wrappedMapKey()47   abstract Optional<Equivalence.Wrapper<AnnotationMirror>> wrappedMapKey();
48 
49   @Memoized
50   @Override
hashCode()51   public abstract int hashCode();
52 
53   @Override
equals(Object obj)54   public abstract boolean equals(Object obj);
55 
56   /** A {@link DelegateDeclaration} factory. */
57   public static final class Factory {
58     private final KeyFactory keyFactory;
59     private final DependencyRequestFactory dependencyRequestFactory;
60 
61     @Inject
Factory( KeyFactory keyFactory, DependencyRequestFactory dependencyRequestFactory)62     Factory(
63         KeyFactory keyFactory,
64         DependencyRequestFactory dependencyRequestFactory) {
65       this.keyFactory = keyFactory;
66       this.dependencyRequestFactory = dependencyRequestFactory;
67     }
68 
create(XMethodElement bindsMethod, XTypeElement contributingModule)69     public DelegateDeclaration create(XMethodElement bindsMethod, XTypeElement contributingModule) {
70       checkArgument(bindsMethod.hasAnnotation(TypeNames.BINDS));
71       XMethodType resolvedMethod = bindsMethod.asMemberOf(contributingModule.getType());
72       DependencyRequest delegateRequest =
73           dependencyRequestFactory.forRequiredResolvedVariable(
74               Iterables.getOnlyElement(bindsMethod.getParameters()),
75               Iterables.getOnlyElement(resolvedMethod.getParameterTypes()));
76       return new AutoValue_DelegateDeclaration(
77           ContributionType.fromBindingElement(bindsMethod),
78           keyFactory.forBindsMethod(bindsMethod, contributingModule),
79           Optional.<XElement>of(bindsMethod),
80           Optional.of(contributingModule),
81           delegateRequest,
82           wrapOptionalInEquivalence(getMapKey(toJavac(bindsMethod))));
83     }
84   }
85 }
86