Repository: checkstyle/checkstyle

Problem statement:
Add maven modernizer plugin
Plugin link https://github.com/gaul/modernizer-maven-plugin

It may be useful, especially for migration to java 11.
Cons - some checks can be duplicated by other tools.

I have added plugin, exclusions (test inputs) and fixed 2 violations here - https://github.com/strkkk/checkstyle/commit/e3a3a041dc2547c035fe77151edcecb0bbfd5df5

Do we need this in checkstyle code?


Hints:
Sure, if project is configurable enough and it can find something , let's use it.
We need to catch up our goal to use all tools of static analysis, to show them in action, and actually benefit from them.
Please don't forget to add `<modernizer.skip>true</>` to [no-validations](https://github.com/checkstyle/checkstyle/blob/master/pom.xml#L1902) and [assembly](https://github.com/checkstyle/checkstyle/blob/master/pom.xml#L1921) profiles.
Output from modernizer:
````
[INFO] --- modernizer-maven-plugin:2.3.0:modernizer (modernizer) @ checkstyle ---
[ERROR] \checkstyle\src\main\java\com\puppycrawl\tools\checkstyle\api\Violation$Utf8Control.java:531: Prefer java.io.InputStreamReader(java.io.InputStream, java.nio.charset.Charset)
[ERROR] \checkstyle\src\main\java\com\puppycrawl\tools\checkstyle\checks\header\AbstractHeaderCheck.java:116: Prefer java.io.InputStreamReader(java.io.InputStream, java.nio.charset.Charset)
````

Patch:
diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/api/Violation.java b/src/main/java/com/puppycrawl/tools/checkstyle/api/Violation.java
--- a/src/main/java/com/puppycrawl/tools/checkstyle/api/Violation.java
+++ b/src/main/java/com/puppycrawl/tools/checkstyle/api/Violation.java
@@ -528,7 +528,7 @@ public ResourceBundle newBundle(String baseName, Locale locale, String format,
                 if (connection != null) {
                     connection.setUseCaches(!reload);
                     try (Reader streamReader = new InputStreamReader(connection.getInputStream(),
-                            StandardCharsets.UTF_8.name())) {
+                            StandardCharsets.UTF_8)) {
                         // Only this line is changed to make it read property files as UTF-8.
                         resourceBundle = new PropertyResourceBundle(streamReader);
                     }
diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/header/AbstractHeaderCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/header/AbstractHeaderCheck.java
--- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/header/AbstractHeaderCheck.java
+++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/header/AbstractHeaderCheck.java
@@ -25,10 +25,10 @@
 import java.io.LineNumberReader;
 import java.io.Reader;
 import java.io.StringReader;
-import java.io.UnsupportedEncodingException;
 import java.net.URI;
 import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
+import java.nio.charset.UnsupportedCharsetException;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
@@ -57,7 +57,8 @@ public abstract class AbstractHeaderCheck extends AbstractFileSetCheck
     private URI headerFile;
 
     /** Specify the character encoding to use when reading the headerFile. */
-    private String charset = System.getProperty("file.encoding", StandardCharsets.UTF_8.name());
+    private Charset charset = createCharset(System.getProperty("file.encoding",
+        StandardCharsets.UTF_8.name()));
 
     /**
      * Hook method for post processing header lines.
@@ -76,17 +77,12 @@ protected List<String> getHeaderLines() {
     }
 
     /**
-     * Setter to specify the character encoding to use when reading the headerFile.
+     * Setter to specify the charset to use when reading the headerFile.
      *
-     * @param charset the charset to use for loading the header from a file
-     * @throws UnsupportedEncodingException if charset is unsupported
+     * @param charset the charset name to use for loading the header from a file
      */
-    public void setCharset(String charset) throws UnsupportedEncodingException {
-        if (!Charset.isSupported(charset)) {
-            final String message = "unsupported charset: '" + charset + "'";
-            throw new UnsupportedEncodingException(message);
-        }
-        this.charset = charset;
+    public void setCharset(String charset) {
+        this.charset = createCharset(charset);
     }
 
     /**
@@ -135,6 +131,21 @@ private void checkHeaderNotInitialized() {
         }
     }
 
+    /**
+     * Creates charset by name.
+     *
+     * @param name charset name
+     * @return created charset
+     * @throws UnsupportedCharsetException if charset is unsupported
+     */
+    private static Charset createCharset(String name) {
+        if (!Charset.isSupported(name)) {
+            final String message = "unsupported charset: '" + name + "'";
+            throw new UnsupportedCharsetException(message);
+        }
+        return Charset.forName(name);
+    }
+
     /**
      * Set the header to check against. Individual lines in the header
      * must be separated by '\n' characters.


Test patch:
diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/ant/CheckstyleAntTaskTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/ant/CheckstyleAntTaskTest.java
--- a/src/test/java/com/puppycrawl/tools/checkstyle/ant/CheckstyleAntTaskTest.java
+++ b/src/test/java/com/puppycrawl/tools/checkstyle/ant/CheckstyleAntTaskTest.java
@@ -25,12 +25,12 @@
 import java.io.IOException;
 import java.net.URL;
 import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 
-import org.apache.commons.io.FileUtils;
 import org.apache.tools.ant.BuildException;
 import org.apache.tools.ant.Project;
 import org.apache.tools.ant.types.FileSet;
@@ -403,7 +403,7 @@ public final void testExecuteIgnoredModules() throws IOException {
                 null, null,
                 getClass(), null);
 
-        final List<String> output = FileUtils.readLines(outputFile, StandardCharsets.UTF_8);
+        final List<String> output = readWholeFile(outputFile);
         final String errorMessage = "Content of file with violations differs from expected";
         assertWithMessage(errorMessage)
                 .that(output.get(0))
@@ -444,7 +444,7 @@ public final void testConfigurationByUrl() throws IOException {
 
         antTask.execute();
 
-        final List<String> output = FileUtils.readLines(outputFile, StandardCharsets.UTF_8);
+        final List<String> output = readWholeFile(outputFile);
         final int sizeOfOutputWithNoViolations = 2;
         assertWithMessage("No violations expected")
                 .that(output.size())
@@ -469,7 +469,7 @@ public final void testConfigurationByResource() throws IOException {
 
         antTask.execute();
 
-        final List<String> output = FileUtils.readLines(outputFile, StandardCharsets.UTF_8);
+        final List<String> output = readWholeFile(outputFile);
         final int sizeOfOutputWithNoViolations = 2;
         assertWithMessage("No violations expected")
                 .that(output.size())
@@ -542,10 +542,9 @@ public final void testXmlOutput() throws IOException {
         antTask.addFormatter(formatter);
         antTask.execute();
 
-        final List<String> expected = FileUtils.readLines(
-                new File(getPath("ExpectedCheckstyleAntTaskXmlOutput.xml")),
-                        StandardCharsets.UTF_8);
-        final List<String> actual = FileUtils.readLines(outputFile, StandardCharsets.UTF_8);
+        final List<String> expected = readWholeFile(
+            new File(getPath("ExpectedCheckstyleAntTaskXmlOutput.xml")));
+        final List<String> actual = readWholeFile(outputFile);
         for (int i = 0; i < expected.size(); i++) {
             final String line = expected.get(i);
             if (!line.startsWith("<checkstyle version") && !line.startsWith("<file")) {
@@ -768,6 +767,10 @@ public void testMaxWarnings() throws IOException {
                 .isTrue();
     }
 
+    private static List<String> readWholeFile(File outputFile) throws IOException {
+        return Files.readAllLines(outputFile.toPath(), StandardCharsets.UTF_8);
+    }
+
     private static class CheckstyleAntTaskLogStub extends CheckstyleAntTask {
 
         private final List<String> loggedMessages = new ArrayList<>();
@@ -787,5 +790,4 @@ public List<String> getLoggedMessages() {
         }
 
     }
-
 }
diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/internal/XdocsPagesTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/internal/XdocsPagesTest.java
--- a/src/test/java/com/puppycrawl/tools/checkstyle/internal/XdocsPagesTest.java
+++ b/src/test/java/com/puppycrawl/tools/checkstyle/internal/XdocsPagesTest.java
@@ -41,6 +41,7 @@
 import java.lang.reflect.Field;
 import java.lang.reflect.ParameterizedType;
 import java.net.URI;
+import java.nio.charset.Charset;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
@@ -1004,7 +1005,7 @@ else if (fieldClass == int[].class) {
         else if (fieldClass == double[].class) {
             result = "double[]";
         }
-        else if (fieldClass == String.class) {
+        else if (fieldClass == String.class || fieldClass == Charset.class) {
             result = "String";
 
             if ("Checker".equals(sectionName) && "localeCountry".equals(propertyName)) {