efegokdemir commented on code in PR #1160:
URL: 
https://github.com/apache/maven-compiler-plugin/pull/1160#discussion_r4212587946


##########
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);

Review Comment:
   Updated the debug call to attach the exception to the same log record, and 
formatting failures are now logged only once per diagnostic code.



##########
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:
   Added a nested fallback: if both getMessage() and toString() throw, the 
logger reports the diagnostic kind and code. Added a regression for both 
failures.



##########
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:
   Changed the catch and regression to RuntimeException, matching javac 
CompletionFailure rather than using LinkageError.



##########
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:
   Added the double-failure regression; it verifies report() completes and 
emits the kind/code fallback when toString() also throws.



##########
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) {

Review Comment:
   Narrowed the handler to RuntimeException, the actual CompletionFailure type, 
and added RuntimeException-based regression coverage.



##########
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.

Review Comment:
   Clarified the fixture: invoking the deprecated constructor triggers the 
diagnostic; the compiler formatting that diagnostic is what exposes the missing 
CLASS-retained annotation type.



-- 
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]

Reply via email to