Skip to content

Commit

Permalink
Issue checkstyle#5023: Prohibited the usage of hardcoded encoding in …
Browse files Browse the repository at this point in the history
…Checkstyle source code;
  • Loading branch information
Artem Dudkin authored and Artem Dudkin committed Oct 16, 2017
1 parent 2a5690c commit 2d836ca
Show file tree
Hide file tree
Showing 21 changed files with 56 additions and 32 deletions.
6 changes: 5 additions & 1 deletion config/checkstyle_checks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,11 @@
<property name="tokens" value="LITERAL_VOLATILE"/>
<property name="tokens" value="LITERAL_ASSERT"/>
</module>
<module name="IllegalTokenText"/>
<module name="IllegalTokenText">
<property name="tokens" value="STRING_LITERAL"/>
<property name="format" value="^(US-ASCII|ISO-8859-1|UTF-8|UTF-16BE|UTF-16LE|UTF-16)$"/>
<property name="ignoreCase" value="true"/>
</module>
<module name="IllegalType"/>
<module name="InnerAssignment"/>
<module name="MagicNumber"/>
Expand Down
3 changes: 2 additions & 1 deletion config/import-control.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
</subpackage>
<subpackage name="header">
<allow class="java.nio.charset.Charset" local-only="true"/>

<allow class="java.nio.charset.StandardCharsets" local-only="true" />
<!-- allowed till https://github.com/checkstyle/checkstyle/issues/3455 -->
<allow class="com.google.common.io.Closeables" local-only="true"/>
</subpackage>
Expand Down Expand Up @@ -170,6 +170,7 @@
local-only="true"/>
<allow class="com.puppycrawl.tools.checkstyle.JavadocDetailNodeParser" local-only="true"/>
<allow class="com.google.common.collect.ImmutableList" local-only="true"/>
<allow class="java.nio.charset.StandardCharsets" local-only="true" />
</subpackage>

<subpackage name="xpath">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -234,7 +235,7 @@ private static String escapeAllControlChars(String text) {
private static DetailAST parseFile(File file, PrintOptions withComments)
throws IOException, CheckstyleException {
final FileText text = new FileText(file.getAbsoluteFile(),
System.getProperty("file.encoding", "UTF-8"));
System.getProperty("file.encoding", StandardCharsets.UTF_8.name()));
return parseFileText(text, withComments);
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/puppycrawl/tools/checkstyle/Checker.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -124,7 +125,7 @@ public class Checker extends AutomaticBean implements MessageDispatcher, RootMod
private SeverityLevel severityLevel = SeverityLevel.ERROR;

/** Name of a charset. */
private String charset = System.getProperty("file.encoding", "UTF-8");
private String charset = System.getProperty("file.encoding", StandardCharsets.UTF_8.name());

/** Cache file. **/
private PropertyCacheFile cache;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

import com.puppycrawl.tools.checkstyle.JavadocDetailNodeParser.ParseErrorMessage;
import com.puppycrawl.tools.checkstyle.JavadocDetailNodeParser.ParseStatus;
Expand Down Expand Up @@ -169,7 +170,7 @@ private static String getIndentation(DetailNode node) {
*/
private static DetailNode parseFile(File file) throws IOException {
final FileText text = new FileText(file.getAbsoluteFile(),
System.getProperty("file.encoding", "UTF-8"));
System.getProperty("file.encoding", StandardCharsets.UTF_8.name()));
return parseJavadocAsDetailNode(text.getFullText().toString());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.io.Serializable;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -519,7 +520,8 @@ public ResourceBundle newBundle(String aBaseName, Locale aLocale, String aFormat
}
ResourceBundle resourceBundle = null;
if (stream != null) {
final Reader streamReader = new InputStreamReader(stream, "UTF-8");
final Reader streamReader = new InputStreamReader(stream,
StandardCharsets.UTF_8.name());
try {
// Only this line is changed to make it to read properties files as UTF-8.
resourceBundle = new PropertyResourceBundle(streamReader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -57,7 +58,7 @@ public abstract class AbstractHeaderCheck extends AbstractFileSetCheck
private URI headerFile;

/** Name of a charset to use for loading the header from a file. */
private String charset = System.getProperty("file.encoding", "UTF-8");
private String charset = System.getProperty("file.encoding", StandardCharsets.UTF_8.name());

/**
* Hook method for post processing header lines.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -267,6 +268,6 @@ private static DetailAST parseFileWithComments(File file) throws IOException, AN
*/
private static FileText getFileText(File file) throws IOException {
return new FileText(file.getAbsoluteFile(),
System.getProperty("file.encoding", "UTF-8"));
System.getProperty("file.encoding", StandardCharsets.UTF_8.name()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ protected DefaultConfiguration createTreeWalkerConfig(Configuration config) {
new DefaultConfiguration("configuration");
final DefaultConfiguration twConf = createModuleConfig(TreeWalker.class);
// make sure that the tests always run with this charset
dc.addAttribute("charset", "UTF-8");
dc.addAttribute("charset", StandardCharsets.UTF_8.name());
dc.addChild(twConf);
twConf.addChild(config);
return dc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void testParseFile() throws Exception {
public void testPrintAst() throws Exception {
final FileText text = new FileText(
new File(getPath("InputAstTreeStringPrinterComments.java")).getAbsoluteFile(),
System.getProperty("file.encoding", "UTF-8"));
System.getProperty("file.encoding", StandardCharsets.UTF_8.name()));
final String actual = AstTreeStringPrinter.printAst(text,
AstTreeStringPrinter.PrintOptions.WITHOUT_COMMENTS);
final String expected = new String(Files.readAllBytes(Paths.get(
Expand Down
11 changes: 6 additions & 5 deletions src/test/java/com/puppycrawl/tools/checkstyle/CheckerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public void testRemoveFilter() {
@Test
public void testFileExtensions() throws Exception {
final DefaultConfiguration checkerConfig = new DefaultConfiguration("configuration");
checkerConfig.addAttribute("charset", "UTF-8");
checkerConfig.addAttribute("charset", StandardCharsets.UTF_8.name());
checkerConfig.addAttribute("cacheFile", temporaryFolder.newFile().getPath());

final Checker checker = new Checker();
Expand Down Expand Up @@ -309,7 +309,7 @@ public void testFileExtensions() throws Exception {
@Test
public void testIgnoredFileExtensions() throws Exception {
final DefaultConfiguration checkerConfig = new DefaultConfiguration("configuration");
checkerConfig.addAttribute("charset", "UTF-8");
checkerConfig.addAttribute("charset", StandardCharsets.UTF_8.name());
checkerConfig.addAttribute("cacheFile", temporaryFolder.newFile().getPath());

final Checker checker = new Checker();
Expand Down Expand Up @@ -406,7 +406,8 @@ public void testFinishLocalSetupFullyInitialized() throws Exception {

final Context context = (Context) Whitebox.getInternalState(checker, "childContext");
assertEquals("Charset was different than expected",
System.getProperty("file.encoding", "UTF-8"), context.get("charset"));
System.getProperty("file.encoding", StandardCharsets.UTF_8.name()),
context.get("charset"));
assertEquals("Was used unsufficient classloader",
contextClassLoader, context.get("classLoader"));
assertEquals("Severity is set to unexpected value",
Expand Down Expand Up @@ -493,7 +494,7 @@ public void testCacheAndCheckWhichDoesNotImplementExternalResourceHolderInterfac
treeWalkerConfig.addChild(checkConfig);

final DefaultConfiguration checkerConfig = new DefaultConfiguration("checkstyleConfig");
checkerConfig.addAttribute("charset", "UTF-8");
checkerConfig.addAttribute("charset", StandardCharsets.UTF_8.name());
checkerConfig.addChild(treeWalkerConfig);

final File cacheFile = temporaryFolder.newFile();
Expand Down Expand Up @@ -562,7 +563,7 @@ public void testWithCacheWithNoViolation() throws Exception {
@Test
public void testClearExistingCache() throws Exception {
final DefaultConfiguration checkerConfig = new DefaultConfiguration("myConfig");
checkerConfig.addAttribute("charset", "UTF-8");
checkerConfig.addAttribute("charset", StandardCharsets.UTF_8.name());
final File cacheFile = temporaryFolder.newFile();
checkerConfig.addAttribute("cacheFile", cacheFile.getPath());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

import org.junit.Test;

Expand All @@ -44,7 +45,7 @@ public void testCtor() throws UnsupportedEncodingException {
final DefaultLogger dl = new DefaultLogger(infoStream, true, errorStream, true);
dl.addException(new AuditEvent(5000, "myfile"), new IllegalStateException("upsss"));
dl.auditFinished(new AuditEvent(6000, "myfile"));
final String output = errorStream.toString("UTF-8");
final String output = errorStream.toString(StandardCharsets.UTF_8.name());
final LocalizedMessage addExceptionMessage = new LocalizedMessage(0,
Definitions.CHECKSTYLE_BUNDLE, DefaultLogger.ADD_EXCEPTION_MESSAGE,
new String[] {"myfile"}, null,
Expand Down Expand Up @@ -76,7 +77,7 @@ public void testNewCtor() throws Exception {
dl.auditStarted(null);
dl.addException(new AuditEvent(5000, "myfile"), new IllegalStateException("upsss"));
dl.auditFinished(new AuditEvent(6000, "myfile"));
final String output = errorStream.toString("UTF-8");
final String output = errorStream.toString(StandardCharsets.UTF_8.name());
final LocalizedMessage addExceptionMessage = new LocalizedMessage(0,
Definitions.CHECKSTYLE_BUNDLE, DefaultLogger.ADD_EXCEPTION_MESSAGE,
new String[] {"myfile"}, null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
Expand Down Expand Up @@ -572,7 +571,7 @@ public void testCacheWhenFileExternalResourceContentDoesNotChange() throws Excep

assertTrue("External resource is not present in cache",
new String(Files.readAllBytes(cacheFile.toPath()),
Charset.forName("UTF-8")).contains(
StandardCharsets.UTF_8).contains(
"InputTreeWalkerSuppressionXpathFilter.xml"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ else if (file.isDirectory()) {

private static void checkFile(String filename) throws Exception {
final FileText text = new FileText(new File(filename),
System.getProperty("file.encoding", "UTF-8"));
System.getProperty("file.encoding", StandardCharsets.UTF_8.name()));
final FileContents contents = new FileContents(text);
final DetailAST rootAST = TreeWalker.parse(contents);
if (rootAST != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.Locale;

import org.junit.Test;
Expand Down Expand Up @@ -70,7 +71,7 @@ public void testSupportedCharset() throws IOException {
doNothing().when(Closeables.class);
Closeables.closeQuietly(any(Reader.class));

final String charsetName = "ISO-8859-1";
final String charsetName = StandardCharsets.ISO_8859_1.name();
final FileText fileText = new FileText(new File(getPath("InputFileTextImportControl.xml")),
charsetName);
assertEquals("Invalid charset name", charsetName, fileText.getCharset().name());
Expand All @@ -81,7 +82,7 @@ public void testSupportedCharset() throws IOException {

@Test
public void testLineColumnBeforeCopyConstructor() throws IOException {
final String charsetName = "ISO-8859-1";
final String charsetName = StandardCharsets.ISO_8859_1.name();
final FileText fileText = new FileText(new File(getPath("InputFileTextImportControl.xml")),
charsetName);
final LineColumn lineColumn = fileText.lineColumn(100);
Expand All @@ -91,7 +92,7 @@ public void testLineColumnBeforeCopyConstructor() throws IOException {

@Test
public void testLineColumnAfterCopyConstructor() throws IOException {
final String charsetName = "ISO-8859-1";
final String charsetName = StandardCharsets.ISO_8859_1.name();
final FileText fileText = new FileText(new File(getPath("InputFileTextImportControl.xml")),
charsetName);
final FileText copy = new FileText(fileText);
Expand All @@ -107,7 +108,7 @@ public void testLineColumnAfterCopyConstructor() throws IOException {

@Test
public void testLineColumnAtTheStartOfFile() throws IOException {
final String charsetName = "ISO-8859-1";
final String charsetName = StandardCharsets.ISO_8859_1.name();
final FileText fileText = new FileText(new File(getPath("InputFileTextImportControl.xml")),
charsetName);
final FileText copy = new FileText(fileText);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static com.puppycrawl.tools.checkstyle.checks.coding.IllegalInstantiationCheck.MSG_KEY;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.SortedSet;

import org.junit.Assert;
Expand Down Expand Up @@ -152,7 +153,8 @@ public void testNullClassLoader() throws Exception {

final IllegalInstantiationCheck check = new IllegalInstantiationCheck();
final File inputFile = new File(getNonCompilablePath("InputIllegalInstantiationLang.java"));
check.setFileContents(new FileContents(new FileText(inputFile, "UTF-8")));
check.setFileContents(new FileContents(new FileText(inputFile,
StandardCharsets.UTF_8.name())));
check.configure(createModuleConfig(IllegalInstantiationCheck.class));
check.setClasses("java.lang.Boolean");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import static org.junit.Assert.fail;

import java.io.File;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;

import org.junit.Rule;
Expand Down Expand Up @@ -351,7 +351,7 @@ public void testCacheWhenFileExternalResourceContentDoesNotChange() throws Excep

assertTrue("External resourse is not present in cache",
new String(Files.readAllBytes(cacheFile.toPath()),
Charset.forName("UTF-8")).contains("InputImportControlOneRegExp.xml"));
StandardCharsets.UTF_8).contains("InputImportControlOneRegExp.xml"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ private static RootDoc getRootDoc(JavadocTool javadocTool, ListBuffer<String[]>
final Method getRootDocImpl = getMethodGetRootDocImplByReflection();
final RootDoc rootDoc;
if (System.getProperty("java.version").startsWith("1.7.")) {
rootDoc = (RootDoc) getRootDocImpl.invoke(javadocTool, "", "UTF-8",
rootDoc = (RootDoc) getRootDocImpl.invoke(javadocTool, "",
StandardCharsets.UTF_8.name(),
new ModifierFilter(ModifierFilter.ALL_ACCESS),
names.toList(),
options.toList(),
Expand All @@ -195,7 +196,8 @@ private static RootDoc getRootDoc(JavadocTool javadocTool, ListBuffer<String[]>
false, false, false);
}
else {
rootDoc = (RootDoc) getRootDocImpl.invoke(javadocTool, "", "UTF-8",
rootDoc = (RootDoc) getRootDocImpl.invoke(javadocTool, "",
StandardCharsets.UTF_8.name(),
new ModifierFilter(ModifierFilter.ALL_ACCESS),
names.toList(),
options.toList(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

import org.junit.Test;
Expand Down Expand Up @@ -217,7 +218,8 @@ private static void verifyAstRaw(String expectedTextPrintFileName, String actual
AstTreeStringPrinter.PrintOptions withComments) throws Exception {
final File expectedFile = new File(expectedTextPrintFileName);
final String expectedContents = new FileText(expectedFile, System.getProperty(
"file.encoding", "UTF-8")).getFullText().toString().replace("\r", "");
"file.encoding", StandardCharsets.UTF_8.name()))
.getFullText().toString().replace("\r", "");

final FileText actualFileContents = new FileText(new File(""),
Arrays.asList(actualJava.split("\\n|\\r\\n?")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package com.puppycrawl.tools.checkstyle.gui;

import java.io.File;
import java.nio.charset.StandardCharsets;

import org.junit.Assert;
import org.junit.Before;
Expand Down Expand Up @@ -48,7 +49,9 @@ protected String getPackageLocation() {

private static DetailAST parseFile(File file) throws Exception {
final FileContents contents = new FileContents(
new FileText(file.getAbsoluteFile(), System.getProperty("file.encoding", "UTF-8")));
new FileText(file.getAbsoluteFile(),
System.getProperty("file.encoding",
StandardCharsets.UTF_8.name())));
return TreeWalker.parseWithComments(contents);
}

Expand Down
Loading

0 comments on commit 2d836ca

Please sign in to comment.