Class TestDiagnosticUtils


  • public class TestDiagnosticUtils
    extends java.lang.Object
    A set of utilities and factory methods useful for working with TestDiagnostics.
    • Field Detail

      • DIAGNOSTIC_IN_JAVA_REGEX

        public static final java.lang.String DIAGNOSTIC_IN_JAVA_REGEX
        How the diagnostics appear in Java source files.
        See Also:
        Constant Field Values
      • DIAGNOSTIC_IN_JAVA_PATTERN

        public static final java.util.regex.Pattern DIAGNOSTIC_IN_JAVA_PATTERN
        How the diagnostics appear in Java source files.
      • DIAGNOSTIC_WARNING_IN_JAVA_REGEX

        public static final java.lang.String DIAGNOSTIC_WARNING_IN_JAVA_REGEX
        See Also:
        Constant Field Values
      • DIAGNOSTIC_WARNING_IN_JAVA_PATTERN

        public static final java.util.regex.Pattern DIAGNOSTIC_WARNING_IN_JAVA_PATTERN
      • DIAGNOSTIC_REGEX

        public static final java.lang.String DIAGNOSTIC_REGEX
        See Also:
        Constant Field Values
      • DIAGNOSTIC_PATTERN

        public static final java.util.regex.Pattern DIAGNOSTIC_PATTERN
      • DIAGNOSTIC_WARNING_REGEX

        public static final java.lang.String DIAGNOSTIC_WARNING_REGEX
        See Also:
        Constant Field Values
      • DIAGNOSTIC_WARNING_PATTERN

        public static final java.util.regex.Pattern DIAGNOSTIC_WARNING_PATTERN
      • DIAGNOSTIC_FILE_REGEX

        public static final java.lang.String DIAGNOSTIC_FILE_REGEX
        See Also:
        Constant Field Values
      • DIAGNOSTIC_FILE_PATTERN

        public static final java.util.regex.Pattern DIAGNOSTIC_FILE_PATTERN
      • DIAGNOSTIC_FILE_WARNING_REGEX

        public static final java.lang.String DIAGNOSTIC_FILE_WARNING_REGEX
        See Also:
        Constant Field Values
      • DIAGNOSTIC_FILE_WARNING_PATTERN

        public static final java.util.regex.Pattern DIAGNOSTIC_FILE_WARNING_PATTERN
    • Constructor Detail

      • TestDiagnosticUtils

        public TestDiagnosticUtils()
    • Method Detail

      • fromDiagnosticFileString

        public static TestDiagnostic fromDiagnosticFileString​(java.lang.String stringFromDiagnosticFile)
        Instantiate the diagnostic based on a string that would appear in diagnostic files (i.e. files that only contain line after line of expected diagnostics).
        Parameters:
        stringFromDiagnosticFile - a single diagnostic string to parse
        Returns:
        a new TestDiagnostic
      • fromJavaFileComment

        public static TestDiagnostic fromJavaFileComment​(java.lang.String filename,
                                                         long lineNumber,
                                                         java.lang.String stringFromJavaFile)
        Instantiate the diagnostic from a string that would appear in a Java file, e.g.: "error: (message)"
        Parameters:
        filename - the file containing the diagnostic (and the error)
        lineNumber - the line number of the line immediately below the diagnostic comment in the Java file
        stringFromJavaFile - the string containing the diagnostic
        Returns:
        a new TestDiagnostic
      • fromJavaxToolsDiagnostic

        public static TestDiagnostic fromJavaxToolsDiagnostic​(java.lang.String diagnosticString,
                                                              boolean noMsgText)
        Instantiate a diagnostic from output produced by the Java compiler. The resulting diagnostic is never fixable and always has parentheses.
      • fromJSpecifyFileComment

        public static TestDiagnostic fromJSpecifyFileComment​(java.lang.String filename,
                                                             long lineNumber,
                                                             java.lang.String stringFromJavaFile)
        Instantiate the diagnostic from a JSpecify string that would appear in a Java file, e.g.: "jspecify_some_category".
        Parameters:
        filename - the file containing the diagnostic (and the error)
        lineNumber - the line number of the line immediately below the diagnostic comment in the Java file
        stringFromJavaFile - the string containing the diagnostic
        Returns:
        a new TestDiagnostic
      • fromPatternMatching

        protected static TestDiagnostic fromPatternMatching​(java.util.regex.Pattern diagnosticPattern,
                                                            java.util.regex.Pattern warningPattern,
                                                            java.lang.String filename,
                                                            @Nullable java.lang.Long lineNumber,
                                                            java.lang.String diagnosticString)
        Instantiate the diagnostic via pattern-matching against patterns.
        Parameters:
        diagnosticPattern - a pattern that matches any diagnostic
        warningPattern - a pattern that matches a warning diagnostic
        filename - the file name
        lineNumber - the line number
        diagnosticString - the string to parse
        Returns:
        a diagnostic parsed from the given string
      • formatJavaxToolString

        public static org.plumelib.util.IPair<java.lang.String,​java.lang.String> formatJavaxToolString​(java.lang.String original,
                                                                                                             boolean noMsgText)
        Given a javax diagnostic, return a pair of (trimmed, filename), where "trimmed" is the first line of the message, without the leading filename.
        Parameters:
        original - a javax diagnostic
        noMsgText - whether to do work; if false, this returns a pair of (argument, "")
        Returns:
        the diagnostic, split into message and filename
      • isJavaDiagnosticLineStart

        public static boolean isJavaDiagnosticLineStart​(java.lang.String originalLine)
        Return true if this line in a Java file indicates an expected diagnostic that might be continued on the next line.
      • handleEndOfLineJavaDiagnostic

        public static java.lang.String handleEndOfLineJavaDiagnostic​(java.lang.String originalLine)
        Convert an end-of-line diagnostic message to a beginning-of-line one. Returns the argument unchanged if it does not contain an end-of-line diagnostic message.

        Most diagnostics in Java files start at the beginning of a line. Occasionally, javac issues a warning about implicit code, such as an implicit constructor, on the line immediately after a curly brace. The only place to put the expected diagnostic message is on the line with the curly brace.

        This implementation replaces "{ // ::" by "// ::", converting the end-of-line diagnostic message to a beginning-of-line one that the rest of the code can handle. It is rather specific (to avoid false positive matches, such as when "// ::" is commented out in source code). It could be extended in the future if such an extension is necessary.

      • isJavaDiagnosticLineContinuation

        @EnsuresNonNullIf(result=true,
                          expression="#1")
        public static boolean isJavaDiagnosticLineContinuation​(@Nullable java.lang.String originalLine)
        Return true if this line in a Java file continues an expected diagnostic.
      • fromJavaSourceLine

        public static TestDiagnosticLine fromJavaSourceLine​(java.lang.String filename,
                                                            java.lang.String line,
                                                            long lineNumber)
        Convert a line in a Java source file to a TestDiagnosticLine.

        The input line is possibly the concatenation of multiple source lines, if the diagnostic was split across lines in the source code.

      • fromDiagnosticFileLine

        public static TestDiagnosticLine fromDiagnosticFileLine​(java.lang.String diagnosticLine)
        Convert a line in a DiagnosticFile to a TestDiagnosticLine.
      • fromJavaxDiagnosticList

        public static java.util.Set<TestDiagnostic> fromJavaxDiagnosticList​(java.util.List<javax.tools.Diagnostic<? extends javax.tools.JavaFileObject>> javaxDiagnostics,
                                                                            boolean noMsgText)
      • diagnosticsToString

        public static java.util.List<java.lang.String> diagnosticsToString​(java.util.List<TestDiagnostic> diagnostics)
        Converts the given diagnostics to strings (as they would appear in a source file individually).
        Parameters:
        diagnostics - a list of diagnostics
        Returns:
        a list of the diagnastics as they would appear in a source file
      • removeDiagnosticsOfKind

        public static void removeDiagnosticsOfKind​(DiagnosticKind kind,
                                                   java.util.List<TestDiagnostic> expectedDiagnostics)