Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use StandardCharsets instead of Charset.forName() #1603

Merged
merged 1 commit into from
Nov 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.sonar.sslr.api.Grammar;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -52,7 +53,7 @@
public class ReservedNamesCheck extends SquidCheck<Grammar> implements CxxCharsetAwareVisitor {

private static final String[] keywords = CxxKeyword.keywordValues();
private Charset charset = Charset.forName("UTF-8");
private Charset charset = StandardCharsets.UTF_8;
private static final Pattern DEFINE_DECLARATION_PATTERN = Pattern.compile("^\\s*#define\\s+([^\\s(]+).*$");


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.sonar.sslr.api.Grammar;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.regex.Pattern;
import org.sonar.check.Priority;
Expand All @@ -49,7 +50,7 @@ public class UseCorrectIncludeCheck extends SquidCheck<Grammar> implements CxxCh

private static final String REGULAR_EXPRESSION = "#include\\s+(?>\"|\\<)[\\\\/\\.]+";
private Pattern pattern = null;
private Charset charset = Charset.forName("UTF-8");
private Charset charset = StandardCharsets.UTF_8;

@Override
public void init() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.sonar.sslr.api.Grammar;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
Expand All @@ -45,7 +46,7 @@
public class FileEncodingCheck extends SquidCheck<Grammar> implements CxxCharsetAwareVisitor {

private static final Logger LOG = Loggers.get(FileEncodingCheck.class);
private Charset charset = Charset.forName("UTF-8");
private Charset charset = StandardCharsets.UTF_8;

@Override
public void setCharset(Charset charset) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.sonar.sslr.api.Grammar;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;
import org.sonar.check.Priority;
import org.sonar.check.Rule;
Expand All @@ -48,7 +49,7 @@
public class TabCharacterCheck extends SquidCheck<Grammar> implements CxxCharsetAwareVisitor {

private static final boolean DEFAULT_CREATE_LINE_VIOLATION = false;
private Charset charset = Charset.forName("UTF-8");
private Charset charset = StandardCharsets.UTF_8;

/**
* createLineViolation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.sonar.sslr.api.Grammar;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;
import org.sonar.check.Priority;
import org.sonar.check.Rule;
Expand Down Expand Up @@ -68,7 +69,7 @@ public class TooLongLineCheck extends SquidCheck<Grammar> implements CxxCharsetA
defaultValue = "" + DEFAULT_TAB_WIDTH)
public int tabWidth = DEFAULT_TAB_WIDTH;

private Charset charset = Charset.forName("UTF-8");
private Charset charset = StandardCharsets.UTF_8;

@Override
public void setCharset(Charset charset) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.sonar.sslr.api.Grammar;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -72,7 +73,7 @@ public class FileHeaderCheck extends SquidCheck<Grammar> implements CxxCharsetAw
defaultValue = "false")
public boolean isRegularExpression;

private Charset charset = Charset.forName("UTF-8");
private Charset charset = StandardCharsets.UTF_8;
private String[] expectedLines = null;
private Pattern searchPattern = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
Expand Down Expand Up @@ -60,7 +61,7 @@ public class FileRegularExpressionCheck extends SquidCheck<Grammar> implements C
private static final boolean DEFAULT_INVERT_REGULAR_EXPRESSION = false;
private static final String DEFAULT_MESSAGE = "The regular expression matches this file";

private Charset charset = Charset.forName("UTF-8");
private Charset charset = StandardCharsets.UTF_8;
private CharsetDecoder decoder = null;
private Pattern pattern = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.sonar.sslr.api.Grammar;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -56,7 +57,7 @@ public class LineRegularExpressionCheck extends SquidCheck<Grammar> implements C
private static final boolean DEFAULT_INVERT_REGULAR_EXPRESSION = false;
private static final String DEFAULT_MESSAGE = "The regular expression matches this line";

private Charset charset = Charset.forName("UTF-8");
private Charset charset = StandardCharsets.UTF_8;
private Pattern pattern = null;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import org.junit.Test;
import org.sonar.cxx.CxxAstScanner;
Expand All @@ -38,9 +39,9 @@ public class FileEncodingCheckTest {
@Test
@SuppressWarnings("squid:S2699") // ... verify contains the assertion
public void testAsciiFileAsciiEncoding() throws UnsupportedEncodingException, IOException {
Charset charset = Charset.forName("US-ASCII");
Charset charset = StandardCharsets.US_ASCII;
CxxConfiguration cxxConfig = new CxxConfiguration(charset);
CxxFileTester tester = CxxFileTesterHelper.CreateCxxFileTester("src/test/resources/checks/TabCharacter.cc", ".", Charset.forName("US-ASCII"));
CxxFileTester tester = CxxFileTesterHelper.CreateCxxFileTester("src/test/resources/checks/TabCharacter.cc", ".", StandardCharsets.US_ASCII);
SourceFile file = CxxAstScanner.scanSingleFileConfig(CxxFileTesterHelper.mockCxxLanguage(), tester.cxxFile, cxxConfig, check);

CheckMessagesVerifier.verify(file.getCheckMessages())
Expand All @@ -50,9 +51,9 @@ public void testAsciiFileAsciiEncoding() throws UnsupportedEncodingException, IO
@Test
@SuppressWarnings("squid:S2699") // ... verify contains the assertion
public void testAsciiFileUtf8Encoding() throws UnsupportedEncodingException, IOException {
Charset charset = Charset.forName("UTF-8");
Charset charset = StandardCharsets.UTF_8;
CxxConfiguration cxxConfig = new CxxConfiguration(charset);
CxxFileTester tester = CxxFileTesterHelper.CreateCxxFileTester("src/test/resources/checks/TabCharacter.cc", ".", Charset.forName("UTF-8"));
CxxFileTester tester = CxxFileTesterHelper.CreateCxxFileTester("src/test/resources/checks/TabCharacter.cc", ".", StandardCharsets.UTF_8);
SourceFile file = CxxAstScanner.scanSingleFileConfig(CxxFileTesterHelper.mockCxxLanguage(), tester.cxxFile, cxxConfig, check);

CheckMessagesVerifier.verify(file.getCheckMessages())
Expand All @@ -62,9 +63,9 @@ public void testAsciiFileUtf8Encoding() throws UnsupportedEncodingException, IOE
@Test
@SuppressWarnings("squid:S2699") // ... verify contains the assertion
public void testUnicodeFileUtf16Encoding() throws UnsupportedEncodingException, IOException {
Charset charset = Charset.forName("UTF-16");
Charset charset = StandardCharsets.UTF_16;
CxxConfiguration cxxConfig = new CxxConfiguration(charset);
CxxFileTester tester = CxxFileTesterHelper.CreateCxxFileTester("src/test/resources/checks/Unicode.cc", ".", Charset.forName("UTF-16"));
CxxFileTester tester = CxxFileTesterHelper.CreateCxxFileTester("src/test/resources/checks/Unicode.cc", ".", StandardCharsets.UTF_16);
SourceFile file = CxxAstScanner.scanSingleFileConfig(CxxFileTesterHelper.mockCxxLanguage(), tester.cxxFile, cxxConfig, check);
CheckMessagesVerifier.verify(file.getCheckMessages())
.noMore();
Expand All @@ -73,9 +74,9 @@ public void testUnicodeFileUtf16Encoding() throws UnsupportedEncodingException,
@Test
@SuppressWarnings("squid:S2699") // ... verify contains the assertion
public void testUnicodeFileAsciiEncoding() throws IOException {
Charset charset = Charset.forName("US-ASCII");
Charset charset = StandardCharsets.US_ASCII;
CxxConfiguration cxxConfig = new CxxConfiguration(charset);
CxxFileTester tester = CxxFileTesterHelper.CreateCxxFileTester("src/test/resources/checks/Unicode.cc", ".", Charset.forName("US-ASCII"));
CxxFileTester tester = CxxFileTesterHelper.CreateCxxFileTester("src/test/resources/checks/Unicode.cc", ".", StandardCharsets.US_ASCII);
SourceFile file = CxxAstScanner.scanSingleFileConfig(CxxFileTesterHelper.mockCxxLanguage(), tester.cxxFile, cxxConfig, check);
CheckMessagesVerifier.verify(file.getCheckMessages())
.next().withMessage("Not all characters of the file can be encoded with the predefined charset " + charset.name() + ".")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import org.junit.Test;
import org.sonar.cxx.CxxAstScanner;
Expand Down Expand Up @@ -51,7 +52,7 @@ public void fileRegExWithoutFilePattern() throws UnsupportedEncodingException, I
@SuppressWarnings("squid:S2699") // ... verify contains the assertion
public void fileRegExInvertWithoutFilePattern() throws UnsupportedEncodingException, IOException {
FileRegularExpressionCheck check = new FileRegularExpressionCheck();
Charset charset = Charset.forName("UTF-8");
Charset charset = StandardCharsets.UTF_8;
CxxConfiguration cxxConfig = new CxxConfiguration(charset);
check.regularExpression = "stdafx\\.h";
check.invertRegularExpression = true;
Expand All @@ -69,12 +70,12 @@ public void fileRegExInvertWithoutFilePattern() throws UnsupportedEncodingExcept
@SuppressWarnings("squid:S2699") // ... verify contains the assertion
public void fileRegExCodingErrorActionReplace() throws UnsupportedEncodingException, IOException {
FileRegularExpressionCheck check = new FileRegularExpressionCheck();
Charset charset = Charset.forName("US-ASCII");
Charset charset = StandardCharsets.US_ASCII;
CxxConfiguration cxxConfig = new CxxConfiguration(charset);
check.regularExpression = "stdafx\\.h";
check.message = "Found 'stdafx.h' in file!";

CxxFileTester tester = CxxFileTesterHelper.CreateCxxFileTester("src/test/resources/checks/FileRegEx.cc", ".", Charset.forName("US-ASCII"));
CxxFileTester tester = CxxFileTesterHelper.CreateCxxFileTester("src/test/resources/checks/FileRegEx.cc", ".", StandardCharsets.US_ASCII);

SourceFile file = CxxAstScanner.scanSingleFileConfig(CxxFileTesterHelper.mockCxxLanguage(), tester.cxxFile, cxxConfig, check);
CheckMessagesVerifier.verify(file.getCheckMessages())
Expand All @@ -86,7 +87,7 @@ public void fileRegExCodingErrorActionReplace() throws UnsupportedEncodingExcept
@SuppressWarnings("squid:S2699") // ... verify contains the assertion
public void fileRegExWithFilePattern() throws UnsupportedEncodingException, IOException {
FileRegularExpressionCheck check = new FileRegularExpressionCheck();
Charset charset = Charset.forName("UTF-8");
Charset charset = StandardCharsets.UTF_8;
CxxConfiguration cxxConfig = new CxxConfiguration(charset);
check.matchFilePattern = "/**/*.cc"; // all files with .cc file extension
check.regularExpression = "#include\\s+\"stdafx\\.h\"";
Expand All @@ -104,7 +105,7 @@ public void fileRegExWithFilePattern() throws UnsupportedEncodingException, IOEx
@SuppressWarnings("squid:S2699") // ... verify contains the assertion
public void fileRegExInvertWithFilePatternInvert() throws UnsupportedEncodingException, IOException {
FileRegularExpressionCheck check = new FileRegularExpressionCheck();
Charset charset = Charset.forName("UTF-8");
Charset charset = StandardCharsets.UTF_8;
CxxConfiguration cxxConfig = new CxxConfiguration(charset);
check.matchFilePattern = "/**/*.h"; // all files with not .h file extension
check.invertFilePattern = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
package org.sonar.cxx.sensors.drmemory;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import static org.assertj.core.api.Assertions.assertThat;

import org.assertj.core.api.SoftAssertions;
Expand Down Expand Up @@ -50,7 +52,7 @@ public void setUp() {
public void shouldIgnoreAViolationWhenTheResourceCouldntBeFoundV1() {

DefaultInputFile inputFile = TestInputFileBuilder.create("ProjectKey", "sources/utils/code_chunks.cpp")
.initMetadata("asd\nasdas\nasda\n").setCharset(Charset.forName("UTF-8")).build();
.initMetadata("asd\nasdas\nasda\n").setCharset(StandardCharsets.UTF_8).build();

SensorContextTester context = SensorContextTester.create(fs.baseDir());
context.settings().setProperty(language.getPluginProperty(CxxDrMemorySensor.REPORT_PATH_KEY),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -89,7 +90,7 @@ public static DefaultFileSystem mockFileSystem(File baseDir,
List<File> sourceDirs,
List<File> testDirs) {
DefaultFileSystem fs = new DefaultFileSystem(baseDir);
fs.setEncoding(Charset.forName("UTF-8"));
fs.setEncoding(StandardCharsets.UTF_8);
scanDirs(fs, sourceDirs, Type.MAIN);
scanDirs(fs, testDirs, Type.TEST);
return fs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -52,7 +53,7 @@ public void scanFile() throws UnsupportedEncodingException, IOException {

String content = new String(Files.readAllBytes(target.toPath()), "UTF-8");
inputFile = TestInputFileBuilder.create("moduleKey", baseDir, target).setType(InputFile.Type.MAIN)
.setContents(content).setCharset(Charset.forName("UTF-8")).build();
.setContents(content).setCharset(StandardCharsets.UTF_8).build();

context = SensorContextTester.create(baseDir);
context.fileSystem().add(inputFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.HashSet;
import java.util.Set;
Expand Down Expand Up @@ -76,7 +77,7 @@ public void setUp() {
public void TestLinesOfCode() throws UnsupportedEncodingException, IOException {
String content = new String(Files.readAllBytes(target.toPath()), "UTF-8");
DefaultInputFile inputFile = TestInputFileBuilder.create("ProjectKey", baseDir, target).setContents(content)
.setCharset(Charset.forName("UTF-8")).setLanguage(language.getKey())
.setCharset(StandardCharsets.UTF_8).setLanguage(language.getKey())
.setType(InputFile.Type.MAIN).build();

SensorContextTester sensorContext = SensorContextTester.create(baseDir);
Expand All @@ -97,7 +98,7 @@ public void TestLinesOfCode() throws UnsupportedEncodingException, IOException {
public void TestLinesOfComments() throws UnsupportedEncodingException, IOException {
String content = new String(Files.readAllBytes(target.toPath()), "UTF-8");
DefaultInputFile inputFile = TestInputFileBuilder.create("ProjectKey", baseDir, target).setContents(content)
.setCharset(Charset.forName("UTF-8")).setLanguage(language.getKey())
.setCharset(StandardCharsets.UTF_8).setLanguage(language.getKey())
.setType(InputFile.Type.MAIN).build();

SensorContextTester sensorContext = SensorContextTester.create(baseDir);
Expand All @@ -117,7 +118,7 @@ public void TestExecutableLinesOfCode() throws UnsupportedEncodingException, IOE

String content = new String(Files.readAllBytes(target.toPath()), "UTF-8");
DefaultInputFile inputFile = TestInputFileBuilder.create("ProjectKey", baseDir, target).setContents(content)
.setCharset(Charset.forName("UTF-8")).setLanguage(language.getKey())
.setCharset(StandardCharsets.UTF_8).setLanguage(language.getKey())
.setType(InputFile.Type.MAIN).build();

SensorContextTester sensorContext = SensorContextTester.create(baseDir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -47,7 +48,7 @@ public void scanFile() throws IOException {

String content = new String(Files.readAllBytes(target.toPath()), "UTF-8");
DefaultInputFile inputFile = TestInputFileBuilder.create("ProjectKey", baseDir, target)
.setContents(content).setCharset(Charset.forName("UTF-8")).build();
.setContents(content).setCharset(StandardCharsets.UTF_8).build();

context = SensorContextTester.create(baseDir);
context.fileSystem().add(inputFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Optional;

Expand Down Expand Up @@ -78,7 +79,7 @@ private DefaultInputFile buildTestInputFile(File baseDir, String fileName) throw
File target = new File(baseDir, fileName);
String content = new String(Files.readAllBytes(target.toPath()), "UTF-8");
DefaultInputFile inputFile = TestInputFileBuilder.create("ProjectKey", baseDir, target).setContents(content)
.setCharset(Charset.forName("UTF-8")).setLanguage(language.getKey())
.setCharset(StandardCharsets.UTF_8).setLanguage(language.getKey())
.setType(InputFile.Type.MAIN).build();
return inputFile;
}
Expand Down