• 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.spi;
18 
19 import static javax.tools.Diagnostic.Kind.ERROR;
20 
21 import com.google.auto.service.AutoService;
22 import com.google.common.collect.ImmutableSet;
23 import dagger.model.BindingGraph;
24 import java.util.Map;
25 import java.util.Set;
26 
27 @AutoService(BindingGraphPlugin.class)
28 public final class FailingPlugin implements BindingGraphPlugin {
29   private Map<String, String> options;
30 
31   @Override
supportedOptions()32   public Set<String> supportedOptions() {
33     return ImmutableSet.of(
34         "error_on_binding",
35         "error_on_dependency",
36         "error_on_component",
37         "error_on_subcomponents");
38   }
39 
40   @Override
initOptions(Map<String, String> options)41   public void initOptions(Map<String, String> options) {
42     this.options = options;
43   }
44 
45   @Override
visitGraph(BindingGraph bindingGraph, DiagnosticReporter diagnosticReporter)46   public void visitGraph(BindingGraph bindingGraph, DiagnosticReporter diagnosticReporter) {
47     if (options.containsKey("error_on_binding")) {
48       String key = options.get("error_on_binding");
49       bindingGraph.bindings().stream()
50           .filter(binding -> binding.key().toString().equals(key))
51           .forEach(
52               binding ->
53                   diagnosticReporter.reportBinding(ERROR, binding, "Bad Binding: %s", binding));
54     }
55 
56     if (options.containsKey("error_on_component")) {
57       diagnosticReporter.reportComponent(
58           ERROR,
59           bindingGraph.rootComponentNode(),
60           "Bad Component: %s",
61           bindingGraph.rootComponentNode());
62     }
63 
64     if (options.containsKey("error_on_subcomponents")) {
65       bindingGraph.componentNodes().stream()
66           .filter(componentNode -> !componentNode.componentPath().atRoot())
67           .forEach(
68               componentNode ->
69                   diagnosticReporter.reportComponent(
70                       ERROR, componentNode, "Bad Subcomponent: %s", componentNode));
71     }
72 
73     if (options.containsKey("error_on_dependency")) {
74       String dependency = options.get("error_on_dependency");
75       bindingGraph.dependencyEdges().stream()
76           .filter(
77               edge ->
78                   edge.dependencyRequest()
79                       .requestElement()
80                       .get()
81                       .getSimpleName()
82                       .contentEquals(dependency))
83           .forEach(
84               edge -> diagnosticReporter.reportDependency(ERROR, edge, "Bad Dependency: %s", edge));
85     }
86   }
87 
88   @Override
pluginName()89   public String pluginName() {
90     return "FailingPlugin";
91   }
92 }
93