• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2014 Google LLC
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 package com.google.auto.value.processor;
17 
18 import com.google.errorprone.annotations.FormatMethod;
19 import javax.annotation.processing.Messager;
20 import javax.annotation.processing.ProcessingEnvironment;
21 import javax.lang.model.element.Element;
22 import javax.tools.Diagnostic;
23 
24 /**
25  * Handle error reporting for an annotation processor.
26  *
27  * @author Éamonn McManus
28  */
29 class ErrorReporter {
30   private final Messager messager;
31   private int errorCount;
32 
ErrorReporter(ProcessingEnvironment processingEnv)33   ErrorReporter(ProcessingEnvironment processingEnv) {
34     this.messager = processingEnv.getMessager();
35   }
36 
37   /**
38    * Issue a compilation note.
39    *
40    * @param e the element to which it pertains
41    * @param format the format string for the text of the note
42    * @param args arguments for the format string
43    */
44   @FormatMethod
reportNote(Element e, String format, Object... args)45   void reportNote(Element e, String format, Object... args) {
46     messager.printMessage(Diagnostic.Kind.NOTE, String.format(format, args), e);
47   }
48 
49   /**
50    * Issue a compilation warning.
51    *
52    * @param e the element to which it pertains
53    * @param format the format string for the text of the warning
54    * @param args arguments for the format string
55    */
56   @FormatMethod
reportWarning(Element e, String format, Object... args)57   void reportWarning(Element e, String format, Object... args) {
58     messager.printMessage(Diagnostic.Kind.WARNING, String.format(format, args), e);
59   }
60 
61   /**
62    * Issue a compilation error. This method does not throw an exception, since we want to continue
63    * processing and perhaps report other errors. It is a good idea to introduce a test case in
64    * CompilationTest for any new call to reportError(...) to ensure that we continue correctly after
65    * an error.
66    *
67    * @param e the element to which it pertains
68    * @param format the format string for the text of the warning
69    * @param args arguments for the format string
70    */
71   @FormatMethod
reportError(Element e, String format, Object... args)72   void reportError(Element e, String format, Object... args) {
73     messager.printMessage(Diagnostic.Kind.ERROR, String.format(format, args), e);
74     errorCount++;
75   }
76 
77   /**
78    * Issue a compilation error and abandon the processing of this class. This does not prevent the
79    * processing of other classes.
80    *
81    * @param e the element to which it pertains
82    * @param format the format string for the text of the error
83    * @param args arguments for the format string
84    * @return This method does not return, but is declared with an exception return type so you
85    *     can write {@code throw abortWithError(...)} to tell the compiler that.
86    * @throws AbortProcessingException always
87    */
88   @FormatMethod
abortWithError(Element e, String format, Object... args)89   AbortProcessingException abortWithError(Element e, String format, Object... args) {
90     reportError(e, format, args);
91     throw new AbortProcessingException();
92   }
93 
94   /** The number of errors that have been output by calls to {@link #reportError}. */
errorCount()95   int errorCount() {
96     return errorCount;
97   }
98 
99   /** Abandon the processing of this class if any errors have been output. */
abortIfAnyError()100   void abortIfAnyError() {
101     if (errorCount > 0) {
102       throw new AbortProcessingException();
103     }
104   }
105 }
106