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