Class TestDiagnosticUtils
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Pattern
Pattern compiled fromDIAGNOSTIC_FILE_REGEX
.static final String
How the diagnostics appear in diagnostic files (.out).static final Pattern
Pattern compiled fromDIAGNOSTIC_FILE_WARNING_REGEX
.static final String
How the diagnostic warnings appear in diagnostic files (.out).static final Pattern
Pattern compiled fromDIAGNOSTIC_IN_JAVA_REGEX
.static final String
How the diagnostics appear in Java source files.static final Pattern
Pattern compiled fromDIAGNOSTIC_REGEX
.static final String
How the diagnostics appear in javax tools diagnostics from the compiler.static final Pattern
Pattern compiled fromDIAGNOSTIC_WARNING_IN_JAVA_REGEX
.static final String
How the diagnostic warnings appear in Java source files.static final Pattern
Pattern compiled fromDIAGNOSTIC_WARNING_REGEX
.static final String
How the diagnostic warnings appear in javax tools diagnostics from the compiler. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic String
continuationPart
(String originalLine) Return the continuation part.diagnosticsToString
(List<TestDiagnostic> diagnostics) Converts the given diagnostics to strings (as they would appear in a source file individually).formatJavaxToolString
(String original) Given a javax diagnostic, return a pair of (trimmed, file), where "trimmed" is the message without the leading filename and the file path.static TestDiagnosticLine
fromDiagnosticFileLine
(String diagnosticLine) Convert a line in a DiagnosticFile to a TestDiagnosticLine.static TestDiagnostic
fromDiagnosticFileString
(String stringFromDiagnosticFile) Instantiate the diagnostic based on a string that would appear in diagnostic files (i.e.static TestDiagnostic
fromJavaFileComment
(String filename, long lineNumber, String stringFromJavaFile) Instantiate the diagnostic from a string that would appear in a Java file, e.g.: "error: (message)"static TestDiagnosticLine
fromJavaSourceLine
(String filename, String line, long lineNumber) Convert a line in a Java source file to a TestDiagnosticLine.static TestDiagnostic
fromJavaxToolsDiagnostic
(String diagnosticString) Instantiate a diagnostic from output produced by the Java compiler.static Set
<TestDiagnostic> fromJavaxToolsDiagnosticList
(List<Diagnostic<? extends JavaFileObject>> javaxDiagnostics) Convert a list of compiler diagnostics into test diagnostics.protected static TestDiagnostic
fromPatternMatching
(Pattern diagnosticPattern, Pattern warningPattern, Path file, @Nullable Long lineNumber, String diagnosticString) Instantiate the diagnostic via pattern-matching against patterns.static String
handleEndOfLineJavaDiagnostic
(String originalLine) Convert an end-of-line diagnostic message to a beginning-of-line one.static boolean
isJavaDiagnosticLineContinuation
(@Nullable String originalLine) Return true if this line in a Java file continues an expected diagnostic.static boolean
isJavaDiagnosticLineStart
(String originalLine) Return true if this line in a Java file indicates an expected diagnostic that might be continued on the next line.
-
Field Details
-
DIAGNOSTIC_IN_JAVA_REGEX
How the diagnostics appear in Java source files.- See Also:
-
DIAGNOSTIC_IN_JAVA_PATTERN
Pattern compiled fromDIAGNOSTIC_IN_JAVA_REGEX
. -
DIAGNOSTIC_WARNING_IN_JAVA_REGEX
How the diagnostic warnings appear in Java source files.- See Also:
-
DIAGNOSTIC_WARNING_IN_JAVA_PATTERN
Pattern compiled fromDIAGNOSTIC_WARNING_IN_JAVA_REGEX
. -
DIAGNOSTIC_REGEX
How the diagnostics appear in javax tools diagnostics from the compiler.- See Also:
-
DIAGNOSTIC_PATTERN
Pattern compiled fromDIAGNOSTIC_REGEX
. -
DIAGNOSTIC_WARNING_REGEX
How the diagnostic warnings appear in javax tools diagnostics from the compiler.- See Also:
-
DIAGNOSTIC_WARNING_PATTERN
Pattern compiled fromDIAGNOSTIC_WARNING_REGEX
. -
DIAGNOSTIC_FILE_REGEX
How the diagnostics appear in diagnostic files (.out).- See Also:
-
DIAGNOSTIC_FILE_PATTERN
Pattern compiled fromDIAGNOSTIC_FILE_REGEX
. -
DIAGNOSTIC_FILE_WARNING_REGEX
How the diagnostic warnings appear in diagnostic files (.out).- See Also:
-
DIAGNOSTIC_FILE_WARNING_PATTERN
Pattern compiled fromDIAGNOSTIC_FILE_WARNING_REGEX
.
-
-
Constructor Details
-
TestDiagnosticUtils
public TestDiagnosticUtils()
-
-
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 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 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 parentheses.- Parameters:
diagnosticString
- the compiler diagnostics string- Returns:
- the corresponding test diagnostic
-
fromPatternMatching
protected static TestDiagnostic fromPatternMatching(Pattern diagnosticPattern, Pattern warningPattern, Path file, @Nullable Long lineNumber, String diagnosticString) Instantiate the diagnostic via pattern-matching against patterns.- Parameters:
diagnosticPattern
- a pattern that matches any diagnosticwarningPattern
- a pattern that matches a warning diagnosticfile
- the test filelineNumber
- the line numberdiagnosticString
- the string to parse- Returns:
- a diagnostic parsed from the given string
-
formatJavaxToolString
Given a javax diagnostic, return a pair of (trimmed, file), where "trimmed" is the message without the leading filename and the file path. As an example: "foo/bar/Baz.java:49: My error message" is turned intoIPair.of(":49: My error message", Path("foo/bar/Baz.java"))
. If the file path cannot be determined, it uses""
. This is necessary to make writing the expected warnings easy.- Parameters:
original
- a javax diagnostic- Returns:
- the diagnostic, split into message and file
-
isJavaDiagnosticLineStart
Return true if this line in a Java file indicates an expected diagnostic that might be continued on the next line.- Parameters:
originalLine
- the input line- Returns:
- whether the diagnostic 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
Return 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
line
is 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. -
fromJavaxToolsDiagnosticList
public static Set<TestDiagnostic> fromJavaxToolsDiagnosticList(List<Diagnostic<? extends JavaFileObject>> javaxDiagnostics) Convert a list of compiler diagnostics into test diagnostics.- Parameters:
javaxDiagnostics
- the list of compiler diagnostics- Returns:
- the corresponding test diagnostics
-
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
-