• 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.validation;
18 
19 import static dagger.internal.codegen.base.Keys.isValidImplicitProvisionKey;
20 import static dagger.internal.codegen.binding.InjectionAnnotations.injectedConstructors;
21 import static dagger.internal.codegen.validation.BindingElementValidator.AllowsMultibindings.NO_MULTIBINDINGS;
22 import static dagger.internal.codegen.validation.BindingElementValidator.AllowsScoping.NO_SCOPING;
23 import static dagger.internal.codegen.validation.BindingMethodValidator.Abstractness.MUST_BE_ABSTRACT;
24 import static dagger.internal.codegen.validation.BindingMethodValidator.ExceptionSuperclass.NO_EXCEPTIONS;
25 
26 import androidx.room.compiler.processing.XMethodElement;
27 import androidx.room.compiler.processing.XProcessingEnv;
28 import androidx.room.compiler.processing.XType;
29 import com.google.common.collect.ImmutableSet;
30 import dagger.internal.codegen.binding.InjectionAnnotations;
31 import dagger.internal.codegen.javapoet.TypeNames;
32 import javax.inject.Inject;
33 
34 /** A validator for {@link dagger.BindsOptionalOf} methods. */
35 final class BindsOptionalOfMethodValidator extends BindingMethodValidator {
36   private final InjectionAnnotations injectionAnnotations;
37 
38   @Inject
BindsOptionalOfMethodValidator( XProcessingEnv processingEnv, DependencyRequestValidator dependencyRequestValidator, InjectionAnnotations injectionAnnotations)39   BindsOptionalOfMethodValidator(
40       XProcessingEnv processingEnv,
41       DependencyRequestValidator dependencyRequestValidator,
42       InjectionAnnotations injectionAnnotations) {
43     super(
44         TypeNames.BINDS_OPTIONAL_OF,
45         ImmutableSet.of(TypeNames.MODULE, TypeNames.PRODUCER_MODULE),
46         MUST_BE_ABSTRACT,
47         NO_EXCEPTIONS,
48         NO_MULTIBINDINGS,
49         NO_SCOPING,
50         processingEnv,
51         dependencyRequestValidator,
52         injectionAnnotations);
53     this.injectionAnnotations = injectionAnnotations;
54   }
55 
56   @Override
elementValidator(XMethodElement method)57   protected ElementValidator elementValidator(XMethodElement method) {
58     return new Validator(method);
59   }
60 
61   private class Validator extends MethodValidator {
62     private final XMethodElement method;
63 
Validator(XMethodElement method)64     Validator(XMethodElement method) {
65       super(method);
66       this.method = method;
67     }
68 
69     @Override
checkKeyType(XType keyType)70     protected void checkKeyType(XType keyType) {
71       super.checkKeyType(keyType);
72       if (isValidImplicitProvisionKey(
73               injectionAnnotations.getQualifiers(method).stream().findFirst(), keyType)
74           && !injectedConstructors(keyType.getTypeElement()).isEmpty()) {
75         report.addError(
76             "@BindsOptionalOf methods cannot return unqualified types that have an @Inject-"
77                 + "annotated constructor because those are always present");
78       }
79     }
80 
81     @Override
checkParameters()82     protected void checkParameters() {
83       if (!method.getParameters().isEmpty()) {
84         report.addError("@BindsOptionalOf methods cannot have parameters");
85       }
86     }
87   }
88 }
89