You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by sl...@apache.org on 2025/10/21 05:50:45 UTC

(maven-javadoc-plugin) branch master updated: Use consistently the encoding expected by Javadoc for reading and writing of data (#1278)

This is an automated email from the ASF dual-hosted git repository.

slachiewicz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-javadoc-plugin.git


The following commit(s) were added to refs/heads/master by this push:
     new 8cb7c5cb Use consistently the encoding expected by Javadoc for reading and writing of data (#1278)
8cb7c5cb is described below

commit 8cb7c5cbeae606951e5fb8050d7855996e0be179
Author: Fridrich Strba <fr...@users.noreply.github.com>
AuthorDate: Tue Oct 21 07:50:39 2025 +0200

    Use consistently the encoding expected by Javadoc for reading and writing of data (#1278)
    
    * Revert "Fix MalformedInputException on Windows with Java 8 due to charset mis… (#1274)"
    
    This reverts commit b453602ab37ab339b887d39b2efc8ebdd35f8dab.
    
    * For reading and writing of data use the encoding expected by Javadoc
    
    For reading and writing of data, Javadoc does expect a certain
    encoding dependending on which version of JDK we use. So we have no
    choice but to actually use that one. Writing only in UTF-8, will
    always fail in some corner cases.
---
 .../maven/plugins/javadoc/AbstractJavadocMojo.java  | 21 ++++-----------------
 .../apache/maven/plugins/javadoc/StaleHelper.java   | 13 +++----------
 .../apache/maven/plugins/javadoc/SystemUtils.java   | 19 +++++++++++++++++++
 3 files changed, 26 insertions(+), 27 deletions(-)

diff --git a/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java b/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java
index d00714cf..1981bed2 100644
--- a/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java
+++ b/src/main/java/org/apache/maven/plugins/javadoc/AbstractJavadocMojo.java
@@ -4149,14 +4149,8 @@ public abstract class AbstractJavadocMojo extends AbstractMojo {
         StringBuilder options = new StringBuilder();
         options.append(StringUtils.join(arguments.iterator(), SystemUtils.LINE_SEPARATOR));
 
-        Charset outputFileEncoding;
-        if (JAVA_VERSION.isAtLeast("9") && JAVA_VERSION.isBefore("12")) {
-            outputFileEncoding = StandardCharsets.UTF_8;
-        } else {
-            outputFileEncoding = Charset.defaultCharset();
-        }
         try {
-            Files.write(optionsFile.toPath(), Collections.singleton(options), outputFileEncoding);
+            Files.write(optionsFile.toPath(), Collections.singleton(options), SystemUtils.getExpectedEncoding());
         } catch (IOException e) {
             throw new MavenReportException(
                     "Unable to write '" + optionsFile.getName() + "' temporary file for command execution", e);
@@ -4194,16 +4188,8 @@ public abstract class AbstractJavadocMojo extends AbstractMojo {
             quotedFiles.add(JavadocUtil.quotedPathArgument(file));
         }
 
-        Charset cs;
-        if (JavaVersion.JAVA_SPECIFICATION_VERSION.isAtLeast("9")
-                && JavaVersion.JAVA_SPECIFICATION_VERSION.isBefore("12")) {
-            cs = StandardCharsets.UTF_8;
-        } else {
-            cs = Charset.defaultCharset();
-        }
-
         try {
-            Files.write(argfileFile.toPath(), quotedFiles, cs);
+            Files.write(argfileFile.toPath(), quotedFiles, SystemUtils.getExpectedEncoding());
         } catch (IOException e) {
             throw new MavenReportException(
                     "Unable to write '" + argfileFile.getName() + "' temporary file for command execution", e);
@@ -5005,7 +4991,8 @@ public abstract class AbstractJavadocMojo extends AbstractMojo {
             Path cacheData = staleDataPath.toPath();
             List<String> prvdata;
             if (Files.isRegularFile(cacheData)) {
-                prvdata = Files.lines(cacheData, StandardCharsets.UTF_8).collect(Collectors.toList());
+                prvdata = Files.lines(cacheData, SystemUtils.getExpectedEncoding())
+                        .collect(Collectors.toList());
             } else {
                 prvdata = null;
             }
diff --git a/src/main/java/org/apache/maven/plugins/javadoc/StaleHelper.java b/src/main/java/org/apache/maven/plugins/javadoc/StaleHelper.java
index 4cf8187c..0f84e72f 100644
--- a/src/main/java/org/apache/maven/plugins/javadoc/StaleHelper.java
+++ b/src/main/java/org/apache/maven/plugins/javadoc/StaleHelper.java
@@ -20,8 +20,6 @@ package org.apache.maven.plugins.javadoc;
 
 import java.io.File;
 import java.io.IOException;
-import java.nio.charset.CharacterCodingException;
-import java.nio.charset.Charset;
 import java.nio.file.DirectoryStream;
 import java.nio.file.Files;
 import java.nio.file.Path;
@@ -33,13 +31,12 @@ import java.util.List;
 import org.apache.maven.reporting.MavenReportException;
 import org.codehaus.plexus.util.cli.Commandline;
 
-import static java.nio.charset.StandardCharsets.UTF_8;
-
 /**
  * Helper class to compute and write data used to detect a
  * stale javadoc.
  */
 public class StaleHelper {
+
     /**
      * Compute the data used to detect a stale javadoc
      *
@@ -58,12 +55,8 @@ public class StaleHelper {
             for (String arg : args) {
                 if (arg.startsWith("@")) {
                     String name = arg.substring(1);
+                    options.addAll(Files.readAllLines(dir.resolve(name), SystemUtils.getExpectedEncoding()));
                     ignored.add(name);
-                    try {
-                        options.addAll(Files.readAllLines(dir.resolve(name), UTF_8));
-                    } catch (CharacterCodingException e) {
-                        options.addAll(Files.readAllLines(dir.resolve(name), Charset.defaultCharset()));
-                    }
                 }
             }
             List<String> state = new ArrayList<>(options);
@@ -113,7 +106,7 @@ public class StaleHelper {
         try {
             List<String> curdata = getStaleData(cmd);
             Files.createDirectories(path.getParent());
-            Files.write(path, curdata, UTF_8);
+            Files.write(path, curdata, SystemUtils.getExpectedEncoding());
         } catch (IOException e) {
             throw new MavenReportException("Error checking stale data", e);
         }
diff --git a/src/main/java/org/apache/maven/plugins/javadoc/SystemUtils.java b/src/main/java/org/apache/maven/plugins/javadoc/SystemUtils.java
index d17addba..a10c0b13 100644
--- a/src/main/java/org/apache/maven/plugins/javadoc/SystemUtils.java
+++ b/src/main/java/org/apache/maven/plugins/javadoc/SystemUtils.java
@@ -19,6 +19,10 @@
 package org.apache.maven.plugins.javadoc;
 
 import java.io.File;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+
+import org.codehaus.plexus.languages.java.version.JavaVersion;
 
 /**
  * Contains several OS-specific methods from Commons-Lang3's SystemUtils. We don't want to use that class because it
@@ -146,6 +150,21 @@ class SystemUtils {
         return new File(System.getProperty(JAVA_HOME_KEY));
     }
 
+    /**
+     * Compute the encoding that Javadoc expects for reading and writing of data
+     *
+     * @return the expected encoding
+     * @since 3.12.1
+     */
+    public static Charset getExpectedEncoding() {
+        if (JavaVersion.JAVA_SPECIFICATION_VERSION.isAtLeast("9")
+                && JavaVersion.JAVA_SPECIFICATION_VERSION.isBefore("12")) {
+            return StandardCharsets.UTF_8;
+        } else {
+            return Charset.defaultCharset();
+        }
+    }
+
     /**
      * <p>
      * Gets a System property, defaulting to {@code null} if the property cannot be read.