• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.model;
18 
19 import static com.google.common.collect.Lists.asList;
20 
21 import com.google.errorprone.annotations.FormatMethod;
22 import dagger.spi.model.BindingGraph.ChildFactoryMethodEdge;
23 import dagger.spi.model.BindingGraph.ComponentNode;
24 import dagger.spi.model.BindingGraph.DependencyEdge;
25 import dagger.spi.model.BindingGraph.MaybeBinding;
26 import javax.tools.Diagnostic;
27 
28 // TODO(bcorso): Move this into dagger/spi?
29 /**
30  * An object that {@link BindingGraphPlugin}s can use to report diagnostics while visiting a {@link
31  * BindingGraph}.
32  *
33  * <p>Note: This API is still experimental and will change.
34  */
35 public abstract class DiagnosticReporter {
36   /**
37    * Reports a diagnostic for a component. For non-root components, includes information about the
38    * path from the root component.
39    */
reportComponent( Diagnostic.Kind diagnosticKind, ComponentNode componentNode, String message)40   public abstract void reportComponent(
41       Diagnostic.Kind diagnosticKind, ComponentNode componentNode, String message);
42 
43   /**
44    * Reports a diagnostic for a component. For non-root components, includes information about the
45    * path from the root component.
46    */
47   @FormatMethod
reportComponent( Diagnostic.Kind diagnosticKind, ComponentNode componentNode, String messageFormat, Object firstArg, Object... moreArgs)48   public final void reportComponent(
49       Diagnostic.Kind diagnosticKind,
50       ComponentNode componentNode,
51       String messageFormat,
52       Object firstArg,
53       Object... moreArgs) {
54     reportComponent(
55         diagnosticKind, componentNode, formatMessage(messageFormat, firstArg, moreArgs));
56   }
57 
58   /**
59    * Reports a diagnostic for a binding or missing binding. Includes information about how the
60    * binding is reachable from entry points.
61    */
reportBinding( Diagnostic.Kind diagnosticKind, MaybeBinding binding, String message)62   public abstract void reportBinding(
63       Diagnostic.Kind diagnosticKind, MaybeBinding binding, String message);
64 
65   /**
66    * Reports a diagnostic for a binding or missing binding. Includes information about how the
67    * binding is reachable from entry points.
68    */
69   @FormatMethod
reportBinding( Diagnostic.Kind diagnosticKind, MaybeBinding binding, String messageFormat, Object firstArg, Object... moreArgs)70   public final void reportBinding(
71       Diagnostic.Kind diagnosticKind,
72       MaybeBinding binding,
73       String messageFormat,
74       Object firstArg,
75       Object... moreArgs) {
76     reportBinding(diagnosticKind, binding, formatMessage(messageFormat, firstArg, moreArgs));
77   }
78 
79   /**
80    * Reports a diagnostic for a dependency. Includes information about how the dependency is
81    * reachable from entry points.
82    */
reportDependency( Diagnostic.Kind diagnosticKind, DependencyEdge dependencyEdge, String message)83   public abstract void reportDependency(
84       Diagnostic.Kind diagnosticKind, DependencyEdge dependencyEdge, String message);
85 
86   /**
87    * Reports a diagnostic for a dependency. Includes information about how the dependency is
88    * reachable from entry points.
89    */
90   @FormatMethod
reportDependency( Diagnostic.Kind diagnosticKind, DependencyEdge dependencyEdge, String messageFormat, Object firstArg, Object... moreArgs)91   public final void reportDependency(
92       Diagnostic.Kind diagnosticKind,
93       DependencyEdge dependencyEdge,
94       String messageFormat,
95       Object firstArg,
96       Object... moreArgs) {
97     reportDependency(
98         diagnosticKind, dependencyEdge, formatMessage(messageFormat, firstArg, moreArgs));
99   }
100 
101   /** Reports a diagnostic for a subcomponent factory method. */
reportSubcomponentFactoryMethod( Diagnostic.Kind diagnosticKind, ChildFactoryMethodEdge childFactoryMethodEdge, String message)102   public abstract void reportSubcomponentFactoryMethod(
103       Diagnostic.Kind diagnosticKind,
104       ChildFactoryMethodEdge childFactoryMethodEdge,
105       String message);
106 
107   /** Reports a diagnostic for a subcomponent factory method. */
108   @FormatMethod
reportSubcomponentFactoryMethod( Diagnostic.Kind diagnosticKind, ChildFactoryMethodEdge childFactoryMethodEdge, String messageFormat, Object firstArg, Object... moreArgs)109   public final void reportSubcomponentFactoryMethod(
110       Diagnostic.Kind diagnosticKind,
111       ChildFactoryMethodEdge childFactoryMethodEdge,
112       String messageFormat,
113       Object firstArg,
114       Object... moreArgs) {
115     reportSubcomponentFactoryMethod(
116         diagnosticKind, childFactoryMethodEdge, formatMessage(messageFormat, firstArg, moreArgs));
117   }
118 
formatMessage(String messageFormat, Object firstArg, Object[] moreArgs)119   private String formatMessage(String messageFormat, Object firstArg, Object[] moreArgs) {
120     return String.format(messageFormat, asList(firstArg, moreArgs).toArray());
121   }
122 }
123