xref: /aosp_15_r20/external/dagger2/javatests/dagger/spi/FailingPlugin.java (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
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.io.IOException;
25 import java.io.UncheckedIOException;
26 import java.util.Map;
27 import java.util.Set;
28 import javax.annotation.processing.Filer;
29 import javax.tools.StandardLocation;
30 
31 @AutoService(BindingGraphPlugin.class)
32 public final class FailingPlugin implements BindingGraphPlugin {
33   private Map<String, String> options;
34   private Filer filer;
35 
36   @Override
supportedOptions()37   public Set<String> supportedOptions() {
38     return ImmutableSet.of(
39         "error_on_binding",
40         "error_on_dependency",
41         "error_on_component",
42         "error_on_subcomponents");
43   }
44 
45   @Override
initFiler(Filer filer)46   public void initFiler(Filer filer) {
47     this.filer = filer;
48   }
49 
50   @Override
initOptions(Map<String, String> options)51   public void initOptions(Map<String, String> options) {
52     this.options = options;
53   }
54 
55   @Override
visitGraph(BindingGraph bindingGraph, DiagnosticReporter diagnosticReporter)56   public void visitGraph(BindingGraph bindingGraph, DiagnosticReporter diagnosticReporter) {
57     if (options.containsKey("error_on_binding")) {
58       String key = options.get("error_on_binding");
59       bindingGraph.bindings().stream()
60           .filter(binding -> binding.key().toString().equals(key))
61           .forEach(
62               binding ->
63                   diagnosticReporter.reportBinding(ERROR, binding, "Bad Binding: %s", binding));
64     }
65 
66     if (options.containsKey("error_on_component")) {
67       diagnosticReporter.reportComponent(
68           ERROR,
69           bindingGraph.rootComponentNode(),
70           "Bad Component: %s",
71           bindingGraph.rootComponentNode());
72     }
73 
74     if (options.containsKey("error_on_subcomponents")) {
75       bindingGraph.componentNodes().stream()
76           .filter(componentNode -> !componentNode.componentPath().atRoot())
77           .forEach(
78               componentNode ->
79                   diagnosticReporter.reportComponent(
80                       ERROR, componentNode, "Bad Subcomponent: %s", componentNode));
81     }
82 
83     if (options.containsKey("error_on_dependency")) {
84       String dependency = options.get("error_on_dependency");
85       bindingGraph.dependencyEdges().stream()
86           .filter(
87               edge ->
88                   edge.dependencyRequest()
89                       .requestElement()
90                       .get()
91                       .getSimpleName()
92                       .contentEquals(dependency))
93           .forEach(
94               edge -> diagnosticReporter.reportDependency(ERROR, edge, "Bad Dependency: %s", edge));
95     }
96   }
97 
98   @Override
pluginName()99   public String pluginName() {
100     return "FailingPlugin";
101   }
102 
103   @Override
onPluginEnd()104   public void onPluginEnd() {
105     try {
106       filer.createResource(StandardLocation.SOURCE_OUTPUT, "", "onPluginEndTest.txt");
107     } catch (IOException e) {
108       throw new UncheckedIOException("Failed to create txt file", e);
109     }
110   }
111 }
112