• 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.xprocessing.XTypes.isDeclared;
20 import static dagger.internal.codegen.xprocessing.XTypes.isTypeVariable;
21 
22 import androidx.room.compiler.processing.XExecutableParameterElement;
23 import androidx.room.compiler.processing.XMethodElement;
24 import androidx.room.compiler.processing.XType;
25 import dagger.internal.codegen.binding.InjectionAnnotations;
26 import java.util.Optional;
27 import javax.inject.Inject;
28 
29 /** Validates {@link BindsInstance} usages on factory method parameters. */
30 public final class BindsInstanceParameterValidator
31     extends BindsInstanceElementValidator<XExecutableParameterElement> {
32   @Inject
BindsInstanceParameterValidator(InjectionAnnotations injectionAnnotations)33   BindsInstanceParameterValidator(InjectionAnnotations injectionAnnotations) {
34     super(injectionAnnotations);
35   }
36 
37   @Override
elementValidator(XExecutableParameterElement parameter)38   protected ElementValidator elementValidator(XExecutableParameterElement parameter) {
39     return new Validator(parameter);
40   }
41 
42   private class Validator extends ElementValidator {
43     private final XExecutableParameterElement parameter;
44 
Validator(XExecutableParameterElement parameter)45     Validator(XExecutableParameterElement parameter) {
46       super(parameter);
47       this.parameter = parameter;
48     }
49 
50     @Override
checkAdditionalProperties()51     protected void checkAdditionalProperties() {
52       if (!parameter.getEnclosingElement().isAbstract()) {
53         report.addError("@BindsInstance parameters may only be used in abstract methods");
54       }
55 
56       // The above check should rule out constructors since constructors cannot be abstract, so we
57       // know the XExecutableElement enclosing the parameter has to be an XMethodElement.
58       XMethodElement method = (XMethodElement) parameter.getEnclosingElement();
59       if (!(isDeclared(method.getReturnType()) || isTypeVariable(method.getReturnType()))) {
60         report.addError(
61             "@BindsInstance parameters may not be used in methods with a void, array or primitive "
62                 + "return type");
63       }
64     }
65 
66     @Override
bindingElementType()67     protected Optional<XType> bindingElementType() {
68       return Optional.of(parameter.getType());
69     }
70   }
71 }
72