• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.extension.DaggerStreams.instancesOf;
20 import static dagger.internal.codegen.extension.DaggerStreams.toImmutableList;
21 import static dagger.internal.codegen.extension.DaggerStreams.toImmutableSet;
22 
23 import com.google.common.annotations.VisibleForTesting;
24 import com.google.common.collect.ImmutableList;
25 import com.google.common.collect.ImmutableSet;
26 import dagger.internal.codegen.compileroption.CompilerOptions;
27 import dagger.model.BindingGraph;
28 import dagger.model.BindingGraph.DependencyEdge;
29 import dagger.spi.BindingGraphPlugin;
30 import dagger.spi.DiagnosticReporter;
31 import javax.inject.Inject;
32 
33 /**
34  * Reports errors or warnings (depending on the {@code -Adagger.nullableValidation} value) for each
35  * non-nullable dependency request that is satisfied by a nullable binding.
36  */
37 final class NullableBindingValidator implements BindingGraphPlugin {
38 
39   private final CompilerOptions compilerOptions;
40 
41   @Inject
NullableBindingValidator(CompilerOptions compilerOptions)42   NullableBindingValidator(CompilerOptions compilerOptions) {
43     this.compilerOptions = compilerOptions;
44   }
45 
46   @Override
visitGraph(BindingGraph bindingGraph, DiagnosticReporter diagnosticReporter)47   public void visitGraph(BindingGraph bindingGraph, DiagnosticReporter diagnosticReporter) {
48     for (dagger.model.Binding binding : nullableBindings(bindingGraph)) {
49       for (DependencyEdge dependencyEdge : nonNullableDependencies(bindingGraph, binding)) {
50         diagnosticReporter.reportDependency(
51             compilerOptions.nullableValidationKind(),
52             dependencyEdge,
53             nullableToNonNullable(
54                 binding.key().toString(),
55                 binding.toString())); // binding.toString() will include the @Nullable
56       }
57     }
58   }
59 
60   @Override
pluginName()61   public String pluginName() {
62     return "Dagger/Nullable";
63   }
64 
nullableBindings(BindingGraph bindingGraph)65   private ImmutableList<dagger.model.Binding> nullableBindings(BindingGraph bindingGraph) {
66     return bindingGraph.bindings().stream()
67         .filter(binding -> binding.isNullable())
68         .collect(toImmutableList());
69   }
70 
nonNullableDependencies( BindingGraph bindingGraph, dagger.model.Binding binding)71   private ImmutableSet<DependencyEdge> nonNullableDependencies(
72       BindingGraph bindingGraph, dagger.model.Binding binding) {
73     return bindingGraph.network().inEdges(binding).stream()
74         .flatMap(instancesOf(DependencyEdge.class))
75         .filter(edge -> !edge.dependencyRequest().isNullable())
76         .collect(toImmutableSet());
77   }
78 
79   @VisibleForTesting
nullableToNonNullable(String key, String binding)80   static String nullableToNonNullable(String key, String binding) {
81     return String.format("%s is not nullable, but is being provided by %s", key, binding);
82   }
83 }
84