1 /* 2 * Copyright (C) 2018 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.bindinggraphvalidation; 18 19 import static dagger.internal.codegen.model.BindingKind.INJECTION; 20 21 import dagger.internal.codegen.model.Binding; 22 import dagger.internal.codegen.model.BindingGraph; 23 import dagger.internal.codegen.model.DiagnosticReporter; 24 import dagger.internal.codegen.validation.InjectValidator; 25 import dagger.internal.codegen.validation.ValidationBindingGraphPlugin; 26 import dagger.internal.codegen.validation.ValidationReport; 27 import dagger.internal.codegen.validation.ValidationReport.Item; 28 import javax.inject.Inject; 29 30 /** Validates bindings from {@code @Inject}-annotated constructors. */ 31 final class InjectBindingValidator extends ValidationBindingGraphPlugin { 32 private final InjectValidator injectValidator; 33 34 @Inject InjectBindingValidator(InjectValidator injectValidator)35 InjectBindingValidator(InjectValidator injectValidator) { 36 this.injectValidator = injectValidator; 37 } 38 39 @Override pluginName()40 public String pluginName() { 41 return "Dagger/InjectBinding"; 42 } 43 44 @Override visitGraph(BindingGraph bindingGraph, DiagnosticReporter diagnosticReporter)45 public void visitGraph(BindingGraph bindingGraph, DiagnosticReporter diagnosticReporter) { 46 bindingGraph.bindings().stream() 47 .filter(binding -> binding.kind().equals(INJECTION)) // TODO(dpb): Move to BindingGraph 48 .forEach(binding -> validateInjectionBinding(binding, diagnosticReporter)); 49 } 50 validateInjectionBinding(Binding node, DiagnosticReporter diagnosticReporter)51 private void validateInjectionBinding(Binding node, DiagnosticReporter diagnosticReporter) { 52 ValidationReport typeReport = 53 injectValidator.validateWhenGeneratingCode( 54 node.key().type().xprocessing().getTypeElement()); 55 for (Item item : typeReport.allItems()) { 56 diagnosticReporter.reportBinding(item.kind(), node, item.message()); 57 } 58 } 59 } 60