• 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.validation;
18 
19 import static com.google.common.collect.Lists.asList;
20 import static dagger.internal.codegen.base.ElementFormatter.elementToString;
21 import static dagger.internal.codegen.langmodel.DaggerElements.elementEncloses;
22 import static javax.tools.Diagnostic.Kind.ERROR;
23 
24 import com.google.common.collect.ImmutableSet;
25 import com.google.errorprone.annotations.FormatMethod;
26 import dagger.model.BindingGraph;
27 import dagger.model.BindingGraph.ChildFactoryMethodEdge;
28 import dagger.model.BindingGraph.ComponentNode;
29 import dagger.model.BindingGraph.DependencyEdge;
30 import dagger.model.BindingGraph.MaybeBinding;
31 import dagger.spi.BindingGraphPlugin;
32 import dagger.spi.DiagnosticReporter;
33 import javax.annotation.processing.Messager;
34 import javax.inject.Inject;
35 import javax.lang.model.element.Element;
36 import javax.lang.model.element.TypeElement;
37 import javax.tools.Diagnostic;
38 import org.checkerframework.checker.nullness.compatqual.NullableDecl;
39 
40 /** A factory for {@link DiagnosticReporter}s. */
41 // TODO(ronshapiro): If multiple plugins print errors on the same node/edge, should we condense the
42 // messages and only print the dependency trace once?
43 final class DiagnosticReporterFactory {
44   private final Messager messager;
45   private final DiagnosticMessageGenerator.Factory diagnosticMessageGeneratorFactory;
46 
47   @Inject
DiagnosticReporterFactory( Messager messager, DiagnosticMessageGenerator.Factory diagnosticMessageGeneratorFactory)48   DiagnosticReporterFactory(
49       Messager messager, DiagnosticMessageGenerator.Factory diagnosticMessageGeneratorFactory) {
50     this.messager = messager;
51     this.diagnosticMessageGeneratorFactory = diagnosticMessageGeneratorFactory;
52   }
53 
54   /** Creates a reporter for a binding graph and a plugin. */
reporter( BindingGraph graph, BindingGraphPlugin plugin, boolean reportErrorsAsWarnings)55   DiagnosticReporterImpl reporter(
56       BindingGraph graph, BindingGraphPlugin plugin, boolean reportErrorsAsWarnings) {
57     return new DiagnosticReporterImpl(graph, plugin.pluginName(), reportErrorsAsWarnings);
58   }
59 
60   /**
61    * A {@link DiagnosticReporter} that keeps track of which {@linkplain Diagnostic.Kind kinds} of
62    * diagnostics were reported.
63    */
64   final class DiagnosticReporterImpl implements DiagnosticReporter {
65     private final String plugin;
66     private final TypeElement rootComponent;
67     private final boolean reportErrorsAsWarnings;
68     private final ImmutableSet.Builder<Diagnostic.Kind> reportedDiagnosticKinds =
69         ImmutableSet.builder();
70     private final DiagnosticMessageGenerator diagnosticMessageGenerator;
71 
DiagnosticReporterImpl(BindingGraph graph, String plugin, boolean reportErrorsAsWarnings)72     DiagnosticReporterImpl(BindingGraph graph, String plugin, boolean reportErrorsAsWarnings) {
73       this.plugin = plugin;
74       this.reportErrorsAsWarnings = reportErrorsAsWarnings;
75       this.rootComponent = graph.rootComponentNode().componentPath().currentComponent();
76       this.diagnosticMessageGenerator = diagnosticMessageGeneratorFactory.create(graph);
77     }
78 
79     /** Returns which {@linkplain Diagnostic.Kind kinds} of diagnostics were reported. */
reportedDiagnosticKinds()80     ImmutableSet<Diagnostic.Kind> reportedDiagnosticKinds() {
81       return reportedDiagnosticKinds.build();
82     }
83 
84     @Override
reportComponent( Diagnostic.Kind diagnosticKind, ComponentNode componentNode, String messageFormat)85     public void reportComponent(
86         Diagnostic.Kind diagnosticKind, ComponentNode componentNode, String messageFormat) {
87       StringBuilder message = new StringBuilder(messageFormat);
88       diagnosticMessageGenerator.appendComponentPathUnlessAtRoot(message, componentNode);
89       // TODO(dpb): Report at the component node component.
90       printMessage(diagnosticKind, message, rootComponent);
91     }
92 
93     @Override
94     @FormatMethod
reportComponent( Diagnostic.Kind diagnosticKind, ComponentNode componentNode, String messageFormat, Object firstArg, Object... moreArgs)95     public void reportComponent(
96         Diagnostic.Kind diagnosticKind,
97         ComponentNode componentNode,
98         String messageFormat,
99         Object firstArg,
100         Object... moreArgs) {
101       reportComponent(
102           diagnosticKind, componentNode, formatMessage(messageFormat, firstArg, moreArgs));
103     }
104 
105     // TODO(ronshapiro): should this also include the binding element?
106     @Override
reportBinding( Diagnostic.Kind diagnosticKind, MaybeBinding binding, String message)107     public void reportBinding(
108         Diagnostic.Kind diagnosticKind, MaybeBinding binding, String message) {
109       printMessage(
110           diagnosticKind, message + diagnosticMessageGenerator.getMessage(binding), rootComponent);
111     }
112 
113     @Override
reportBinding( Diagnostic.Kind diagnosticKind, MaybeBinding binding, String messageFormat, Object firstArg, Object... moreArgs)114     public void reportBinding(
115         Diagnostic.Kind diagnosticKind,
116         MaybeBinding binding,
117         String messageFormat,
118         Object firstArg,
119         Object... moreArgs) {
120       reportBinding(diagnosticKind, binding, formatMessage(messageFormat, firstArg, moreArgs));
121     }
122 
123     @Override
reportDependency( Diagnostic.Kind diagnosticKind, DependencyEdge dependencyEdge, String message)124     public void reportDependency(
125         Diagnostic.Kind diagnosticKind, DependencyEdge dependencyEdge, String message) {
126       printMessage(
127           diagnosticKind,
128           message + diagnosticMessageGenerator.getMessage(dependencyEdge),
129           rootComponent);
130     }
131 
132     @Override
reportDependency( Diagnostic.Kind diagnosticKind, DependencyEdge dependencyEdge, String messageFormat, Object firstArg, Object... moreArgs)133     public void reportDependency(
134         Diagnostic.Kind diagnosticKind,
135         DependencyEdge dependencyEdge,
136         String messageFormat,
137         Object firstArg,
138         Object... moreArgs) {
139       reportDependency(
140           diagnosticKind, dependencyEdge, formatMessage(messageFormat, firstArg, moreArgs));
141     }
142 
143     @Override
reportSubcomponentFactoryMethod( Diagnostic.Kind diagnosticKind, ChildFactoryMethodEdge childFactoryMethodEdge, String message)144     public void reportSubcomponentFactoryMethod(
145         Diagnostic.Kind diagnosticKind,
146         ChildFactoryMethodEdge childFactoryMethodEdge,
147         String message) {
148       printMessage(diagnosticKind, message, childFactoryMethodEdge.factoryMethod());
149     }
150 
151     @Override
reportSubcomponentFactoryMethod( Diagnostic.Kind diagnosticKind, ChildFactoryMethodEdge childFactoryMethodEdge, String messageFormat, Object firstArg, Object... moreArgs)152     public void reportSubcomponentFactoryMethod(
153         Diagnostic.Kind diagnosticKind,
154         ChildFactoryMethodEdge childFactoryMethodEdge,
155         String messageFormat,
156         Object firstArg,
157         Object... moreArgs) {
158       reportSubcomponentFactoryMethod(
159           diagnosticKind, childFactoryMethodEdge, formatMessage(messageFormat, firstArg, moreArgs));
160     }
161 
formatMessage(String messageFormat, Object firstArg, Object[] moreArgs)162     private String formatMessage(String messageFormat, Object firstArg, Object[] moreArgs) {
163       return String.format(messageFormat, asList(firstArg, moreArgs).toArray());
164     }
165 
printMessage( Diagnostic.Kind diagnosticKind, CharSequence message, @NullableDecl Element elementToReport)166     void printMessage(
167         Diagnostic.Kind diagnosticKind,
168         CharSequence message,
169         @NullableDecl Element elementToReport) {
170       if (diagnosticKind.equals(ERROR) && reportErrorsAsWarnings) {
171         diagnosticKind = Diagnostic.Kind.WARNING;
172       }
173       reportedDiagnosticKinds.add(diagnosticKind);
174       StringBuilder fullMessage = new StringBuilder();
175       appendBracketPrefix(fullMessage, plugin);
176 
177       // TODO(ronshapiro): should we create a HashSet out of elementEncloses() so we don't
178       // need to do an O(n) contains() each time?
179       if (elementToReport != null && !elementEncloses(rootComponent, elementToReport)) {
180         appendBracketPrefix(fullMessage, elementToString(elementToReport));
181         elementToReport = rootComponent;
182       }
183 
184       messager.printMessage(diagnosticKind, fullMessage.append(message), elementToReport);
185     }
186 
appendBracketPrefix(StringBuilder message, String prefix)187     private void appendBracketPrefix(StringBuilder message, String prefix) {
188       message.append(String.format("[%s] ", prefix));
189     }
190   }
191 }
192