Class TestDiagnosticUtils
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final PatternA compiled version ofDIAGNOSTIC_FILE_REGEX.static final StringHow the diagnostics appear in diagnostic files (.goalfiles), each of which contains one expected diagnostic per line.static final PatternA compiled version ofDIAGNOSTIC_FILE_WARNING_REGEX.static final StringHow a warning appears in diagnostic files (.goalfiles).static final PatternA compiled version ofDIAGNOSTIC_IN_JAVA_REGEX.static final StringHow the diagnostics appear in Java source files: the text of an expected-diagnostic comment, without the leading "// ::".static final PatternA compiled version ofDIAGNOSTIC_REGEX.static final StringHow the diagnostics appear in javax tools diagnostics from the compiler, after the file name has been stripped off.static final PatternA compiled version ofDIAGNOSTIC_WARNING_IN_JAVA_REGEX.static final StringHow a warning appears in Java source files.static final PatternA compiled version ofDIAGNOSTIC_WARNING_REGEX.static final StringHow a warning appears in javax tools diagnostics from the compiler, after the file name has been stripped off. -
Method Summary
Modifier and TypeMethodDescriptionstatic StringcontinuationPart(String originalLine) Returns the continuation part.diagnosticsToString(List<TestDiagnostic> diagnostics) Converts the given diagnostics to strings (as they would appear in a source file individually).static TestDiagnosticLinefromDiagnosticFileLine(String diagnosticLine) Convert a line in a DiagnosticFile to a TestDiagnosticLine.static TestDiagnosticfromDiagnosticFileString(String stringFromDiagnosticFile) Instantiate the diagnostic based on a string that would appear in diagnostic files (i.e.static TestDiagnosticfromJavaFileComment(String filename, long lineNumber, String stringFromJavaFile) Instantiate the diagnostic from a string that would appear in a Java test file, e.g.: "error: (error-message-key)"static TestDiagnosticLinefromJavaSourceLine(String filename, String line, long lineNumber) Convert a line in a Java source file to a TestDiagnosticLine.static Set<TestDiagnostic> fromJavaxDiagnosticList(List<Diagnostic<? extends JavaFileObject>> javaxDiagnostics) Convert Java compiler output into a set ofTestDiagnostics.static TestDiagnosticfromJavaxToolsDiagnostic(String diagnosticString) Instantiate a diagnostic from output produced by the Java compiler.static TestDiagnosticfromJSpecifyFileComment(String filename, long lineNumber, String stringFromJavaFile) Instantiate the diagnostic from a JSpecify string that would appear in a Java file, e.g.: "jspecify_some_category".static StringhandleEndOfLineJavaDiagnostic(String originalLine) Convert an end-of-line diagnostic message to a beginning-of-line one.static booleanisJavaDiagnosticLineContinuation(@Nullable String originalLine) Return true if this line in a Java file continues an expected diagnostic.static booleanisJavaDiagnosticLineStart(String originalLine) Returns true if this line in a Java file indicates an expected diagnostic that might be continued on the next line.messageAndFilename(String original) Given a javax diagnostic, return a pair of (trimmed, filename), where "trimmed" is the first line of the message, without the leading filename.static voidremoveDiagnosticsOfKind(DiagnosticKind kind, List<TestDiagnostic> expectedDiagnostics)
-
Field Details
-
DIAGNOSTIC_IN_JAVA_REGEX
How the diagnostics appear in Java source files: the text of an expected-diagnostic comment, without the leading "// ::". An example is "error: [assignment]". Capturing group 1 is the diagnostic kind and group 2 is the rest of the diagnostic (its message key, its message, or both).- See Also:
-
DIAGNOSTIC_IN_JAVA_PATTERN
A compiled version ofDIAGNOSTIC_IN_JAVA_REGEX. -
DIAGNOSTIC_WARNING_IN_JAVA_REGEX
How a warning appears in Java source files. An example is "warning: [unchecked] unchecked cast". Capturing group 1 is the warning message, which (unlike a message matched byDIAGNOSTIC_IN_JAVA_REGEX) may contain a line separator.- See Also:
-
DIAGNOSTIC_WARNING_IN_JAVA_PATTERN
A compiled version ofDIAGNOSTIC_WARNING_IN_JAVA_REGEX. -
DIAGNOSTIC_REGEX
How the diagnostics appear in javax tools diagnostics from the compiler, after the file name has been stripped off. An example is ":12: error: [assignment]". Capturing group 1 is the line number, group 2 is the diagnostic kind, and group 3 is the rest of the diagnostic.- See Also:
-
DIAGNOSTIC_PATTERN
A compiled version ofDIAGNOSTIC_REGEX. -
DIAGNOSTIC_WARNING_REGEX
How a warning appears in javax tools diagnostics from the compiler, after the file name has been stripped off. An example is ":12: warning: [unchecked] unchecked cast". Capturing group 1 is the line number and group 2 is the warning message.- See Also:
-
DIAGNOSTIC_WARNING_PATTERN
A compiled version ofDIAGNOSTIC_WARNING_REGEX. -
DIAGNOSTIC_FILE_REGEX
How the diagnostics appear in diagnostic files (.goalfiles), each of which contains one expected diagnostic per line. An example line is "MyTest.java:12: error: [assignment]". Capturing group 1 is the line number, group 2 is the diagnostic kind, and group 3 is the rest of the diagnostic.- See Also:
-
DIAGNOSTIC_FILE_PATTERN
A compiled version ofDIAGNOSTIC_FILE_REGEX. -
DIAGNOSTIC_FILE_WARNING_REGEX
How a warning appears in diagnostic files (.goalfiles). An example line is"MyTest.java:12: warning: [unchecked] unchecked cast". Capturing group 1 is the line number and group 2 is the warning message.- See Also:
-
DIAGNOSTIC_FILE_WARNING_PATTERN
A compiled version ofDIAGNOSTIC_FILE_WARNING_REGEX.
-
-
Method Details
-
fromDiagnosticFileString
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(String filename, long lineNumber, String stringFromJavaFile) Instantiate the diagnostic from a string that would appear in a Java test file, e.g.: "error: (error-message-key)"- 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 filestringFromJavaFile- the string containing the diagnostic- Returns:
- a new TestDiagnostic
-
fromJavaxToolsDiagnostic
Instantiate a diagnostic from output produced by the Java compiler. The resulting diagnostic is never fixable and always has square brackets.- Parameters:
diagnosticString- a diagnostic produced by the Java compiler- Returns:
- a TestDiagnostic for the string
-
fromJSpecifyFileComment
public static TestDiagnostic fromJSpecifyFileComment(String filename, long lineNumber, 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 filestringFromJavaFile- the string containing the diagnostic- Returns:
- a new TestDiagnostic
-
messageAndFilename
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- Returns:
- the diagnostic, split into message and filename
-
isJavaDiagnosticLineStart
Returns true if this line in a Java file indicates an expected diagnostic that might be continued on the next line. -
handleEndOfLineJavaDiagnostic
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 String originalLine) Return true if this line in a Java file continues an expected diagnostic. -
continuationPart
Returns the continuation part. The argument is such thatisJavaDiagnosticLineContinuation(java.lang.String)returns true. -
fromJavaSourceLine
Convert a line in a Java source file to a TestDiagnosticLine.The input
lineis possibly the concatenation of multiple source lines, if the diagnostic was split across lines in the source code. -
fromDiagnosticFileLine
Convert a line in a DiagnosticFile to a TestDiagnosticLine. -
fromJavaxDiagnosticList
public static Set<TestDiagnostic> fromJavaxDiagnosticList(List<Diagnostic<? extends JavaFileObject>> javaxDiagnostics) Convert Java compiler output into a set ofTestDiagnostics.- Parameters:
javaxDiagnostics- output of the Java compiler- Returns:
- a set of
TestDiagnostics
-
diagnosticsToString
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, List<TestDiagnostic> expectedDiagnostics)
-