1 /* 2 * Copyright (c) 2013, 2020, 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 43 public void report(Diagnostic<? extends JavaFileObject> diagnostic) { 44 diags.add(diagnostic); 45 } 46 47 /** Were there any errors found? */ 48 public boolean errorsFound() { 49 return diags.stream() 50 .anyMatch(d -> d.getKind() == Diagnostic.Kind.ERROR); 51 } 52 53 /** Get all diagnostic keys */ 54 public List<String> keys() { 55 return diags.stream() 56 .map(Diagnostic::getCode) 57 .collect(toList()); 58 } 59 60 public Diagnostic<?> getDiagWithKey(String key) { 61 for (Diagnostic<?> d : diags) { 62 if (d.getCode().equals(key)) { 63 return d; 64 } 65 } 66 return null; 67 } 68 69 public List<Diagnostic<?>> getAllDiags() { 70 return diags.stream().map(d -> (Diagnostic<?>)d).collect(toList()); 71 } 72 73 /** Do the diagnostics contain the specified error key? */ 74 public boolean containsErrorKey(String key) { 75 return diags.stream() 76 .filter(d -> d.getKind() == Diagnostic.Kind.ERROR) 77 .anyMatch(d -> d.getCode().equals(key)); 78 } 79 80 /** Do the diagnostics contain the specified warning key? */ 81 public boolean containsWarningKey(String key) { 82 return diags.stream() 83 .filter(d -> d.getKind() == Diagnostic.Kind.WARNING || d.getKind() == Diagnostic.Kind.MANDATORY_WARNING) 84 .anyMatch(d -> d.getCode().equals(key)); 85 } 86 87 public boolean containsWarningKey(String key, int numberOfWarnings) { 88 return diags.stream() 89 .filter(d -> d.getKind() == Diagnostic.Kind.WARNING || d.getKind() == Diagnostic.Kind.MANDATORY_WARNING) 90 .filter(d -> d.getCode().equals(key)).count() == numberOfWarnings; 91 } 92 93 /** Get the error keys */ 94 public List<String> errorKeys() { 95 return diags.stream() 96 .filter(d -> d.getKind() == Diagnostic.Kind.ERROR) 97 .map(Diagnostic::getCode) 98 .collect(toList()); 99 } 100 101 public String toString() { return keys().toString(); } 102 103 /** Clear all diagnostic state */ 104 public void reset() { 105 diags.clear(); 106 } 107 }