1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package tools.javac.combo;
  25 
  26 import javax.tools.Diagnostic;
  27 import javax.tools.JavaFileObject;
  28 import java.util.ArrayList;
  29 import java.util.List;
  30 
  31 import static java.util.stream.Collectors.toList;
  32 
  33 /**
  34 * A container for compiler diagnostics, separated into errors and warnings,
  35  * used by JavacTemplateTestBase.
  36  *
  37  * @author Brian Goetz
  38 */
  39 public class Diagnostics implements javax.tools.DiagnosticListener<JavaFileObject> {
  40 
  41     protected List<Diagnostic<? extends JavaFileObject>> diags = new ArrayList<>();
  42     protected boolean foundErrors = false;
  43 
  44     public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
  45         diags.add(diagnostic);
  46         foundErrors = foundErrors || diagnostic.getKind() == Diagnostic.Kind.ERROR;
  47     }
  48 
  49     /** Were there any errors found? */
  50     public boolean errorsFound() {
  51         return foundErrors;
  52     }
  53 
  54     /** Get all diagnostic keys */
  55     public List<String> keys() {
  56         return diags.stream()
  57                     .map(Diagnostic::getCode)
  58                     .collect(toList());
  59     }
  60 
  61     /** Do the diagnostics contain the specified error key? */
  62     public boolean containsErrorKey(String key) {
  63         return diags.stream()
  64                     .filter(d -> d.getKind() == Diagnostic.Kind.ERROR)
  65                     .anyMatch(d -> d.getCode().equals(key));
  66     }
  67 
  68     /** Get the error keys */
  69     public List<String> errorKeys() {
  70         return diags.stream()
  71                     .filter(d -> d.getKind() == Diagnostic.Kind.ERROR)
  72                     .map(Diagnostic::getCode)
  73                     .collect(toList());
  74     }
  75 
  76     public String toString() { return keys().toString(); }
  77 
  78     /** Clear all diagnostic state */
  79     public void reset() {
  80         diags.clear();
  81         foundErrors = false;
  82     }
  83 }