• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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.validation.BindingElementValidator.AllowsMultibindings.ALLOWS_MULTIBINDINGS;
20 import static dagger.internal.codegen.validation.BindingElementValidator.AllowsScoping.ALLOWS_SCOPING;
21 import static dagger.internal.codegen.validation.BindingMethodValidator.Abstractness.MUST_BE_CONCRETE;
22 import static dagger.internal.codegen.validation.BindingMethodValidator.ExceptionSuperclass.RUNTIME_EXCEPTION;
23 
24 import androidx.room.compiler.processing.XMethodElement;
25 import androidx.room.compiler.processing.XProcessingEnv;
26 import androidx.room.compiler.processing.XVariableElement;
27 import com.google.common.collect.ImmutableSet;
28 import dagger.internal.codegen.binding.InjectionAnnotations;
29 import dagger.internal.codegen.javapoet.TypeNames;
30 import javax.inject.Inject;
31 
32 /** A validator for {@link dagger.Provides} methods. */
33 final class ProvidesMethodValidator extends BindingMethodValidator {
34 
35   private final DependencyRequestValidator dependencyRequestValidator;
36 
37   @Inject
ProvidesMethodValidator( XProcessingEnv processingEnv, DependencyRequestValidator dependencyRequestValidator, InjectionAnnotations injectionAnnotations)38   ProvidesMethodValidator(
39       XProcessingEnv processingEnv,
40       DependencyRequestValidator dependencyRequestValidator,
41       InjectionAnnotations injectionAnnotations) {
42     super(
43         TypeNames.PROVIDES,
44         ImmutableSet.of(TypeNames.MODULE, TypeNames.PRODUCER_MODULE),
45         MUST_BE_CONCRETE,
46         RUNTIME_EXCEPTION,
47         ALLOWS_MULTIBINDINGS,
48         ALLOWS_SCOPING,
49         processingEnv,
50         dependencyRequestValidator,
51         injectionAnnotations);
52     this.dependencyRequestValidator = dependencyRequestValidator;
53   }
54 
55   @Override
elementValidator(XMethodElement method)56   protected ElementValidator elementValidator(XMethodElement method) {
57     return new Validator(method);
58   }
59 
60   private class Validator extends MethodValidator {
Validator(XMethodElement method)61     Validator(XMethodElement method) {
62       super(method);
63     }
64 
65     /** Adds an error if a {@link dagger.Provides @Provides} method depends on a producer type. */
66     @Override
checkParameter(XVariableElement parameter)67     protected void checkParameter(XVariableElement parameter) {
68       super.checkParameter(parameter);
69       dependencyRequestValidator.checkNotProducer(report, parameter);
70     }
71   }
72 }
73