• 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 dagger.internal.codegen.base.ElementFormatter.elementToString;
20 import static dagger.internal.codegen.xprocessing.XElements.transitivelyEncloses;
21 import static javax.tools.Diagnostic.Kind.ERROR;
22 
23 import androidx.room.compiler.processing.XElement;
24 import androidx.room.compiler.processing.XMessager;
25 import androidx.room.compiler.processing.XTypeElement;
26 import com.google.common.collect.ImmutableSet;
27 import dagger.internal.codegen.model.BindingGraph;
28 import dagger.internal.codegen.model.BindingGraph.ChildFactoryMethodEdge;
29 import dagger.internal.codegen.model.BindingGraph.ComponentNode;
30 import dagger.internal.codegen.model.BindingGraph.DependencyEdge;
31 import dagger.internal.codegen.model.BindingGraph.MaybeBinding;
32 import dagger.internal.codegen.model.DiagnosticReporter;
33 import javax.inject.Inject;
34 import javax.tools.Diagnostic;
35 import org.checkerframework.checker.nullness.compatqual.NullableDecl;
36 
37 /** A factory for {@link DiagnosticReporter}s. */
38 // TODO(ronshapiro): If multiple plugins print errors on the same node/edge, should we condense the
39 // messages and only print the dependency trace once?
40 final class DiagnosticReporterFactory {
41   private final XMessager messager;
42   private final DiagnosticMessageGenerator.Factory diagnosticMessageGeneratorFactory;
43 
44   @Inject
DiagnosticReporterFactory( XMessager messager, DiagnosticMessageGenerator.Factory diagnosticMessageGeneratorFactory)45   DiagnosticReporterFactory(
46       XMessager messager, DiagnosticMessageGenerator.Factory diagnosticMessageGeneratorFactory) {
47     this.messager = messager;
48     this.diagnosticMessageGeneratorFactory = diagnosticMessageGeneratorFactory;
49   }
50 
51   /** Creates a reporter for a binding graph and a plugin. */
reporter(BindingGraph graph, String pluginName)52   DiagnosticReporterImpl reporter(BindingGraph graph, String pluginName) {
53     return new DiagnosticReporterImpl(graph, pluginName, /* reportErrorsAsWarnings= */ false);
54   }
55 
56   /** Creates a reporter for a binding graph and a plugin that treats errors as warnings. */
reporterWithErrorAsWarnings(BindingGraph graph, String pluginName)57   DiagnosticReporterImpl reporterWithErrorAsWarnings(BindingGraph graph, String pluginName) {
58     return new DiagnosticReporterImpl(graph, pluginName, /* reportErrorsAsWarnings= */ true);
59   }
60 
61   /**
62    * A {@link DiagnosticReporter} that keeps track of which {@linkplain Diagnostic.Kind kinds} of
63    * diagnostics were reported.
64    */
65   final class DiagnosticReporterImpl extends DiagnosticReporter {
66     private final String plugin;
67     private final XTypeElement rootComponent;
68     private final boolean reportErrorsAsWarnings;
69     private final ImmutableSet.Builder<Diagnostic.Kind> reportedDiagnosticKinds =
70         ImmutableSet.builder();
71     private final DiagnosticMessageGenerator diagnosticMessageGenerator;
72 
DiagnosticReporterImpl(BindingGraph graph, String plugin, boolean reportErrorsAsWarnings)73     DiagnosticReporterImpl(BindingGraph graph, String plugin, boolean reportErrorsAsWarnings) {
74       this.plugin = plugin;
75       this.reportErrorsAsWarnings = reportErrorsAsWarnings;
76       this.rootComponent =
77           graph.rootComponentNode().componentPath().currentComponent().xprocessing();
78       this.diagnosticMessageGenerator = diagnosticMessageGeneratorFactory.create(graph);
79     }
80 
81     /** Returns which {@linkplain Diagnostic.Kind kinds} of diagnostics were reported. */
reportedDiagnosticKinds()82     ImmutableSet<Diagnostic.Kind> reportedDiagnosticKinds() {
83       return reportedDiagnosticKinds.build();
84     }
85 
86     @Override
reportComponent( Diagnostic.Kind diagnosticKind, ComponentNode componentNode, String messageFormat)87     public void reportComponent(
88         Diagnostic.Kind diagnosticKind, ComponentNode componentNode, String messageFormat) {
89       StringBuilder message = new StringBuilder(messageFormat);
90       diagnosticMessageGenerator.appendComponentPathUnlessAtRoot(message, componentNode);
91       // TODO(dpb): Report at the component node component.
92       printMessage(diagnosticKind, message, rootComponent);
93     }
94 
95     // TODO(ronshapiro): should this also include the binding element?
96     @Override
reportBinding( Diagnostic.Kind diagnosticKind, MaybeBinding binding, String message)97     public void reportBinding(
98         Diagnostic.Kind diagnosticKind, MaybeBinding binding, String message) {
99       printMessage(
100           diagnosticKind, message + diagnosticMessageGenerator.getMessage(binding), rootComponent);
101     }
102 
103     @Override
reportDependency( Diagnostic.Kind diagnosticKind, DependencyEdge dependencyEdge, String message)104     public void reportDependency(
105         Diagnostic.Kind diagnosticKind, DependencyEdge dependencyEdge, String message) {
106       printMessage(
107           diagnosticKind,
108           message + diagnosticMessageGenerator.getMessage(dependencyEdge),
109           rootComponent);
110     }
111 
112     @Override
reportSubcomponentFactoryMethod( Diagnostic.Kind diagnosticKind, ChildFactoryMethodEdge childFactoryMethodEdge, String message)113     public void reportSubcomponentFactoryMethod(
114         Diagnostic.Kind diagnosticKind,
115         ChildFactoryMethodEdge childFactoryMethodEdge,
116         String message) {
117       printMessage(diagnosticKind, message, childFactoryMethodEdge.factoryMethod().xprocessing());
118     }
119 
printMessage( Diagnostic.Kind diagnosticKind, CharSequence message, @NullableDecl XElement elementToReport)120     private void printMessage(
121         Diagnostic.Kind diagnosticKind,
122         CharSequence message,
123         @NullableDecl XElement elementToReport) {
124       if (diagnosticKind.equals(ERROR) && reportErrorsAsWarnings) {
125         diagnosticKind = Diagnostic.Kind.WARNING;
126       }
127       reportedDiagnosticKinds.add(diagnosticKind);
128       StringBuilder fullMessage = new StringBuilder();
129       appendBracketPrefix(fullMessage, plugin);
130 
131       if (elementToReport == null) {
132         messager.printMessage(diagnosticKind, fullMessage.append(message).toString());
133       } else {
134         if (!transitivelyEncloses(rootComponent, elementToReport)) {
135           appendBracketPrefix(fullMessage, elementToString(elementToReport));
136           elementToReport = rootComponent;
137         }
138         messager.printMessage(
139             diagnosticKind, fullMessage.append(message).toString(), elementToReport);
140       }
141     }
142 
appendBracketPrefix(StringBuilder message, String prefix)143     private void appendBracketPrefix(StringBuilder message, String prefix) {
144       message.append(String.format("[%s] ", prefix));
145     }
146   }
147 }
148