gnodet-bot commented on code in PR #1160:
URL:
https://github.com/apache/maven-compiler-plugin/pull/1160#discussion_r4212024670
##########
src/main/java/org/apache/maven/plugin/compiler/DiagnosticLogger.java:
##########
@@ -114,7 +114,19 @@ private String relativize(String file) {
*/
@Override
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
- String message = diagnostic.getMessage(locale);
+ String message;
+ try {
+ message = diagnostic.getMessage(locale);
+ } catch (Throwable e) {
+ /*
+ * Some JDK versions may fail while formatting a diagnostic if a
referenced class has an
+ * annotation type that is not on the classpath (for example, a
CLASS-retained annotation
+ * on a deprecated API). The compiler can still emit a useful
fallback representation.
+ */
+ logger.debug("Cannot format compiler diagnostic; falling back to
its string representation.");
+ logger.debug(e);
+ message = diagnostic.toString();
Review Comment:
⚠️ **`toString()` shares the same failure path as `getMessage()`**
`JCDiagnostic.toString()` calls `defaultFormatter.format(this,
Locale.getDefault())`, which delegates to `formatDiagnostic()` →
`formatMeta('m')` → `formatMessage()` → `formatArguments()`. This is the same
code path that throws. If a `CompletionFailure` fires during argument
formatting, both `getMessage()` and `toString()` will throw, leaving `message`
unset and the exception propagating out of `report()`.
Consider a nested try/catch with a safe-last-resort fallback:
```suggestion
try {
message = diagnostic.toString();
} catch (Throwable e2) {
message = diagnostic.getKind() + ": " + diagnostic.getCode();
}
```
##########
src/test/java/org/apache/maven/plugin/compiler/DiagnosticLoggerTest.java:
##########
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.plugin.compiler;
+
+import javax.tools.Diagnostic;
+import javax.tools.JavaFileObject;
+
+import java.util.Locale;
+
+import org.apache.maven.api.plugin.Log;
+import org.apache.maven.impl.DefaultMessageBuilderFactory;
+import org.junit.jupiter.api.Test;
+
+import static org.mockito.ArgumentMatchers.argThat;
+import static org.mockito.ArgumentMatchers.nullable;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+class DiagnosticLoggerTest {
+ @Test
+ void fallsBackWhenDiagnosticMessageCannotBeFormatted() {
+ var logger = mock(Log.class);
+ @SuppressWarnings("unchecked")
+ Diagnostic<JavaFileObject> diagnostic = mock(Diagnostic.class);
+ when(diagnostic.getMessage(nullable(Locale.class))).thenThrow(new
LinkageError("missing type"));
Review Comment:
💡 **Consider using `RuntimeException` instead of `LinkageError`**
`CompletionFailure` (the actual exception thrown by the JDK in this
scenario) extends `RuntimeException`, not `Error`. Using `LinkageError` (an
`Error` subtype) still exercises the `catch (Throwable)` block correctly, but
it's a less faithful proxy for the real failure mode. Swapping it for
`RuntimeException` would better document why `Throwable` (rather than
`Exception`) is caught here.
##########
src/test/java/org/apache/maven/plugin/compiler/DiagnosticLoggerTest.java:
##########
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.plugin.compiler;
+
+import javax.tools.Diagnostic;
+import javax.tools.JavaFileObject;
+
+import java.util.Locale;
+
+import org.apache.maven.api.plugin.Log;
+import org.apache.maven.impl.DefaultMessageBuilderFactory;
+import org.junit.jupiter.api.Test;
+
+import static org.mockito.ArgumentMatchers.argThat;
+import static org.mockito.ArgumentMatchers.nullable;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+class DiagnosticLoggerTest {
+ @Test
+ void fallsBackWhenDiagnosticMessageCannotBeFormatted() {
+ var logger = mock(Log.class);
+ @SuppressWarnings("unchecked")
+ Diagnostic<JavaFileObject> diagnostic = mock(Diagnostic.class);
+ when(diagnostic.getMessage(nullable(Locale.class))).thenThrow(new
LinkageError("missing type"));
+ when(diagnostic.toString()).thenReturn("deprecated API used");
Review Comment:
💡 **Add a second test for the double-failure path**
This test stubs `toString()` to succeed, but as noted above, `toString()`
can throw the same way `getMessage()` does. Adding a second test case where
`diagnostic.toString()` also throws would verify that `report()` handles the
double-failure gracefully (rather than propagating the exception). That test
would fail today — which is exactly why it should exist.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]