• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 Google Inc. All Rights Reserved.
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 com.google.turbine.diag;
18 
19 import com.google.common.collect.ImmutableList;
20 import com.google.turbine.diag.TurbineError.ErrorKind;
21 import java.util.LinkedHashSet;
22 import java.util.Set;
23 import javax.tools.Diagnostic;
24 
25 /** A log that collects diagnostics. */
26 public class TurbineLog {
27 
28   private final Set<TurbineDiagnostic> diagnostics = new LinkedHashSet<>();
29 
withSource(SourceFile source)30   public TurbineLogWithSource withSource(SourceFile source) {
31     return new TurbineLogWithSource(source);
32   }
33 
diagnostics()34   public ImmutableList<TurbineDiagnostic> diagnostics() {
35     return ImmutableList.copyOf(diagnostics);
36   }
37 
maybeThrow()38   public void maybeThrow() {
39     if (anyErrors()) {
40       throw new TurbineError(diagnostics());
41     }
42   }
43 
anyErrors()44   public boolean anyErrors() {
45     for (TurbineDiagnostic error : diagnostics) {
46       if (error.severity().equals(Diagnostic.Kind.ERROR)) {
47         return true;
48       }
49     }
50     return false;
51   }
52 
53   /**
54    * Returns true if a non-deferrable error was raised during annotation processing, i.e. an error
55    * reported by an annotation processor.
56    *
57    * <p>Errors reported by turbine (e.g. missing symbols) are non-fatal, since they may be fixed by
58    * code generated in later processing rounds.
59    */
errorRaised()60   public boolean errorRaised() {
61     for (TurbineDiagnostic error : diagnostics) {
62       if (error.kind().equals(ErrorKind.PROC) && error.severity().equals(Diagnostic.Kind.ERROR)) {
63         return true;
64       }
65     }
66     return false;
67   }
68 
69   /** Reset the log between annotation processing rounds. */
clear()70   public void clear() {
71     diagnostics.removeIf(TurbineDiagnostic::isError);
72   }
73 
74   /** Reports an annotation processing diagnostic with no position information. */
diagnostic(Diagnostic.Kind severity, String message)75   public void diagnostic(Diagnostic.Kind severity, String message) {
76     diagnostics.add(TurbineDiagnostic.format(severity, ErrorKind.PROC, message));
77   }
78 
79   /** A log for a specific source file. */
80   public class TurbineLogWithSource {
81 
82     private final SourceFile source;
83 
TurbineLogWithSource(SourceFile source)84     private TurbineLogWithSource(SourceFile source) {
85       this.source = source;
86     }
87 
diagnostic(Diagnostic.Kind severity, int position, ErrorKind kind, Object... args)88     public void diagnostic(Diagnostic.Kind severity, int position, ErrorKind kind, Object... args) {
89       diagnostics.add(TurbineDiagnostic.format(severity, source, position, kind, args));
90     }
91 
error(int position, ErrorKind kind, Object... args)92     public void error(int position, ErrorKind kind, Object... args) {
93       diagnostic(Diagnostic.Kind.ERROR, position, kind, args);
94     }
95   }
96 }
97