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