Skip to content

Commit

Permalink
Replace "UTF8" with StandardCharsets.UTF_8
Browse files Browse the repository at this point in the history
Avoids NON-NLS and possible UnsupportedEncodingException
  • Loading branch information
EcljpseB0T committed Jul 2, 2024
1 parent 8d7be05 commit 679a887
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.BitSet;

import org.eclipse.jdt.bcoview.asm.DecompiledClass;
Expand Down Expand Up @@ -111,7 +111,7 @@ protected InputStream createStream() throws CoreException {
+ " is not supported by current JVM", //$NON-NLS-1$
e));
}
final byte[] bytes = decompiledClass.getText().getBytes(Charset.forName("UTF-8")); //$NON-NLS-1$
final byte[] bytes = decompiledClass.getText().getBytes(StandardCharsets.UTF_8);
// use internal buffering to prevent multiple calls to this method
Display.getDefault().syncExec(() -> setContent(bytes));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

import org.objectweb.asm.util.Printer;
Expand Down Expand Up @@ -108,7 +109,7 @@ private static String readFullSpec() {
URL helpResource = toUrl(SPECS_HTML);
StringBuilder sb = new StringBuilder();
boolean foundContentStart = false;
try (BufferedReader in = new BufferedReader(new InputStreamReader(helpResource.openStream(), "UTF-8"))) { //$NON-NLS-1$
try (BufferedReader in = new BufferedReader(new InputStreamReader(helpResource.openStream(), StandardCharsets.UTF_8))) {
String line;
while ((line = in.readLine()) != null) {
if (!foundContentStart) {
Expand Down
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.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Vector;

import org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestLoader;
Expand Down Expand Up @@ -386,7 +387,7 @@ public void setLoader(ITestLoader newInstance) {
}

private void readPackageNames(String pkgNameFile) throws IOException {
try(BufferedReader br= new BufferedReader(new InputStreamReader(new FileInputStream(new File(pkgNameFile)), "UTF-8"))) { //$NON-NLS-1$
try(BufferedReader br= new BufferedReader(new InputStreamReader(new FileInputStream(new File(pkgNameFile)), StandardCharsets.UTF_8))) {
String line;
Vector<String> list= new Vector<>();
while ((line= br.readLine()) != null) {
Expand All @@ -403,7 +404,7 @@ private void readPackageNames(String pkgNameFile) throws IOException {
}

private void readTestNames(String testNameFile) throws IOException {
try(BufferedReader br= new BufferedReader(new InputStreamReader(new FileInputStream(new File(testNameFile)), "UTF-8"))) { //$NON-NLS-1$
try(BufferedReader br= new BufferedReader(new InputStreamReader(new FileInputStream(new File(testNameFile)), StandardCharsets.UTF_8))) {
String line;
Vector<String> list= new Vector<>();
while ((line= br.readLine()) != null) {
Expand All @@ -420,7 +421,7 @@ private void readTestNames(String testNameFile) throws IOException {
}

private void readFailureNames(String testFailureFile) throws IOException {
try(BufferedReader br= new BufferedReader(new InputStreamReader(new FileInputStream(new File(testFailureFile)), "UTF-8"))) { //$NON-NLS-1$
try(BufferedReader br= new BufferedReader(new InputStreamReader(new FileInputStream(new File(testFailureFile)), StandardCharsets.UTF_8))) {
String line;
Vector<String> list= new Vector<>();
while ((line= br.readLine()) != null) {
Expand Down Expand Up @@ -649,12 +650,12 @@ protected boolean connect() {
try{
fClientSocket= new Socket(fHost, fPort);
try {
fWriter= new PrintWriter(new BufferedWriter(new OutputStreamWriter(fClientSocket.getOutputStream(), "UTF-8")), false/*true*/); //$NON-NLS-1$
fWriter= new PrintWriter(new BufferedWriter(new OutputStreamWriter(fClientSocket.getOutputStream(), StandardCharsets.UTF_8)), false/*true*/);
} catch (UnsupportedEncodingException e1) {
fWriter= new PrintWriter(new BufferedWriter(new OutputStreamWriter(fClientSocket.getOutputStream())), false/*true*/);
}
try {
fReader= new BufferedReader(new InputStreamReader(fClientSocket.getInputStream(), "UTF-8")); //$NON-NLS-1$
fReader= new BufferedReader(new InputStreamReader(fClientSocket.getInputStream(), StandardCharsets.UTF_8));
} catch (UnsupportedEncodingException e1) {
fReader= new BufferedReader(new InputStreamReader(fClientSocket.getInputStream()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -87,7 +88,7 @@ public void testHTML() throws Exception {
fworkspacePathLength= workspacePath.length();
File chkpiiResults= new File(hostWorkspace, "chkpiiResults");
File tidy= new File(chkpiiResults, "tidy.txt");
fTidyResults= new OutputStreamWriter(new FileOutputStream(tidy), "UTF-8");
fTidyResults= new OutputStreamWriter(new FileOutputStream(tidy), StandardCharsets.UTF_8);
String startTime= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
fTidyResults.write(getClass().getName() + " started at " + startTime + "\n");
fIgnores= getIgnores();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

import org.junit.After;
Expand Down Expand Up @@ -65,7 +66,7 @@ public void tearDown() throws Exception {

public IPath addFile(IPath root, String fileName, String contents) throws CoreException, IOException {
IPath filePath= root.append(fileName);
createFile(filePath, contents.getBytes("UTF8")); //$NON-NLS-1$
createFile(filePath, contents.getBytes(StandardCharsets.UTF_8));
return filePath;
}

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

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;

import org.junit.After;
Expand Down Expand Up @@ -249,7 +250,7 @@ class pack/age/MyMap
(TK;)T0V;
""";
ensureExists(annotationFile.getParent());
annotationFile.create(new ByteArrayInputStream(initialContent.getBytes("UTF-8")), 0, null);
annotationFile.create(new ByteArrayInputStream(initialContent.getBytes(StandardCharsets.UTF_8)), 0, null);

JavaEditor javaEditor= (JavaEditor) JavaUI.openInEditor(type);

Expand Down Expand Up @@ -325,7 +326,7 @@ class pack/age/X
([[ILjava/util/List<Ljava/lang/String;>;)Ljava/lang/String;
""";
ensureExists(annotationFile.getParent());
annotationFile.create(new ByteArrayInputStream(initialContent.getBytes("UTF-8")), 0, null);
annotationFile.create(new ByteArrayInputStream(initialContent.getBytes(StandardCharsets.UTF_8)), 0, null);

IType type= fJProject1.findType(X_PATH.replace('/', '.'));
JavaEditor javaEditor= (JavaEditor) JavaUI.openInEditor(type);
Expand Down Expand Up @@ -404,7 +405,7 @@ class pack/age/X
(Ljava/util/List<Ljava/lang/String;>;[I)Ljava/lang/String;
""";
ensureExists(annotationFile.getParent());
annotationFile.create(new ByteArrayInputStream(initialContent.getBytes("UTF-8")), 0, null);
annotationFile.create(new ByteArrayInputStream(initialContent.getBytes(StandardCharsets.UTF_8)), 0, null);

IType type= fJProject1.findType(X_PATH.replace('/', '.'));
JavaEditor javaEditor= (JavaEditor) JavaUI.openInEditor(type);
Expand Down Expand Up @@ -481,7 +482,7 @@ class pack/age/X
(Ljava/lang/String;)V
""";
ensureExists(annotationFile.getParent());
annotationFile.create(new ByteArrayInputStream(initialContent.getBytes("UTF-8")), 0, null);
annotationFile.create(new ByteArrayInputStream(initialContent.getBytes(StandardCharsets.UTF_8)), 0, null);

IType type= fJProject1.findType(X_PATH.replace('/', '.'));
JavaEditor javaEditor= (JavaEditor) JavaUI.openInEditor(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -49,6 +50,7 @@

import org.eclipse.jdt.internal.core.ClasspathAttribute;
import org.eclipse.jdt.internal.core.ClasspathEntry;
import org.eclipse.jdt.internal.core.JavaModelManager;

import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jdt.ui.examples.MyClasspathContainerInitializer;
Expand Down Expand Up @@ -193,7 +195,7 @@ class pack/age/X
([IL1java/util/List<Ljava/lang/String;>;)Ljava/lang/String;
""";
ensureExists(annotationFile.getParent());
annotationFile.create(new ByteArrayInputStream(initialContent.getBytes("UTF-8")), 0, null);
annotationFile.create(new ByteArrayInputStream(initialContent.getBytes(StandardCharsets.UTF_8)), 0, null);

IType type= fJProject1.findType(X_PATH.replace('/', '.'));
JavaEditor javaEditor= (JavaEditor) JavaUI.openInEditor(type);
Expand Down Expand Up @@ -260,7 +262,18 @@ public interface X {
"""
};
addLibrary(fJProject1, "lib.jar", "lib.zip", pathAndContents, ANNOTATION_PATH, JavaCore.VERSION_1_8, null);

IResource resource = fJProject1.getProject().getFile("lib.jar");
assertTrue(resource.exists());
IType type= fJProject1.findType(X_PATH.replace('/', '.'));
JavaCore.runReadOnly(()->{

// JavaCore.getClasspathContainer(resource.getFullPath(), fJProject1);
JavaModelManager manager = JavaModelManager.getJavaModelManager();
JavaCore.getClasspathContainer(resource.getFullPath(), fJProject1);
manager.containers = new HashMap(5);
JavaCore.getClasspathContainer(resource.getFullPath(), fJProject1);
});
JavaEditor javaEditor= (JavaEditor) JavaUI.openInEditor(type);

try {
Expand Down Expand Up @@ -306,7 +319,7 @@ class pack/age/X
([Ljava/lang/Object;L1java/util/List<+1Ljava/lang/Number;>;)Ljava/lang/String;
""";
ensureExists(annotationFile.getParent());
annotationFile.create(new ByteArrayInputStream(initialContent.getBytes("UTF-8")), 0, null);
annotationFile.create(new ByteArrayInputStream(initialContent.getBytes(StandardCharsets.UTF_8)), 0, null);

IType type= fJProject1.findType(X_PATH.replace('/', '.'));
JavaEditor javaEditor= (JavaEditor) JavaUI.openInEditor(type);
Expand Down Expand Up @@ -383,7 +396,7 @@ public interface X {
"class pack/age/X\n" +
"test\n";
ensureExists(annotationFile.getParent());
annotationFile.create(new ByteArrayInputStream(initialContent.getBytes("UTF-8")), 0, null);
annotationFile.create(new ByteArrayInputStream(initialContent.getBytes(StandardCharsets.UTF_8)), 0, null);

IType type= fJProject1.findType(X_PATH.replace('/', '.'));
JavaEditor javaEditor= (JavaEditor) JavaUI.openInEditor(type);
Expand Down Expand Up @@ -464,7 +477,7 @@ class pack/age/X
([1[ILjava/util/List<Ljava/lang/String;>;)Ljava/lang/String;
""";
ensureExists(annotationFile.getParent());
annotationFile.create(new ByteArrayInputStream(initialContent.getBytes("UTF-8")), 0, null);
annotationFile.create(new ByteArrayInputStream(initialContent.getBytes(StandardCharsets.UTF_8)), 0, null);

IType type= fJProject1.findType(X_PATH.replace('/', '.'));
JavaEditor javaEditor= (JavaEditor) JavaUI.openInEditor(type);
Expand Down Expand Up @@ -545,7 +558,7 @@ class pack/age/X
([1[ILjava/util/List<Ljava/lang/String;>;)Ljava/lang/String;
""";
ensureExists(annotationFile.getParent());
annotationFile.create(new ByteArrayInputStream(initialContent.getBytes("UTF-8")), 0, null);
annotationFile.create(new ByteArrayInputStream(initialContent.getBytes(StandardCharsets.UTF_8)), 0, null);

IType type= fJProject1.findType(X_PATH.replace('/', '.'));
JavaEditor javaEditor= (JavaEditor) JavaUI.openInEditor(type);
Expand Down Expand Up @@ -626,7 +639,7 @@ class pack/age/X
([[1[ILjava/util/List<Ljava/lang/String;>;)Ljava/lang/String;
""";
ensureExists(annotationFile.getParent());
annotationFile.create(new ByteArrayInputStream(initialContent.getBytes("UTF-8")), 0, null);
annotationFile.create(new ByteArrayInputStream(initialContent.getBytes(StandardCharsets.UTF_8)), 0, null);

IType type= fJProject1.findType(X_PATH.replace('/', '.'));
JavaEditor javaEditor= (JavaEditor) JavaUI.openInEditor(type);
Expand Down Expand Up @@ -705,7 +718,7 @@ class pack/age/X
()[Ljava/lang/String;
""";
ensureExists(annotationFile.getParent());
annotationFile.create(new ByteArrayInputStream(initialContent.getBytes("UTF-8")), 0, null);
annotationFile.create(new ByteArrayInputStream(initialContent.getBytes(StandardCharsets.UTF_8)), 0, null);

IType type= fJProject1.findType(X_PATH.replace('/', '.'));
JavaEditor javaEditor= (JavaEditor) JavaUI.openInEditor(type);
Expand Down Expand Up @@ -784,7 +797,7 @@ class pack/age/X
()[Ljava/lang/String;
""";
ensureExists(annotationFile.getParent());
annotationFile.create(new ByteArrayInputStream(initialContent.getBytes("UTF-8")), 0, null);
annotationFile.create(new ByteArrayInputStream(initialContent.getBytes(StandardCharsets.UTF_8)), 0, null);

IType type= fJProject1.findType(X_PATH.replace('/', '.'));
JavaEditor javaEditor= (JavaEditor) JavaUI.openInEditor(type);
Expand Down Expand Up @@ -863,7 +876,7 @@ class pack/age/X
(Ljava/util/List<Ljava/lang/String;>;[I)Ljava/lang/String;
""";
ensureExists(annotationFile.getParent());
annotationFile.create(new ByteArrayInputStream(initialContent.getBytes("UTF-8")), 0, null);
annotationFile.create(new ByteArrayInputStream(initialContent.getBytes(StandardCharsets.UTF_8)), 0, null);

IType type= fJProject1.findType(X_PATH.replace('/', '.'));
JavaEditor javaEditor= (JavaEditor) JavaUI.openInEditor(type);
Expand Down Expand Up @@ -944,7 +957,7 @@ class pack/age/X
(Ljava/util/List<Ljava/lang/String;>;[[1I)Ljava/lang/String;
""";
ensureExists(annotationFile.getParent());
annotationFile.create(new ByteArrayInputStream(initialContent.getBytes("UTF-8")), 0, null);
annotationFile.create(new ByteArrayInputStream(initialContent.getBytes(StandardCharsets.UTF_8)), 0, null);

IType type= fJProject1.findType(X_PATH.replace('/', '.'));
JavaEditor javaEditor= (JavaEditor) JavaUI.openInEditor(type);
Expand Down Expand Up @@ -1023,7 +1036,7 @@ class pack/age/X
(Ljava/util/List<Ljava/lang/String;>;[Ljava/lang/String;)Ljava/lang/String;
""";
ensureExists(annotationFile.getParent());
annotationFile.create(new ByteArrayInputStream(initialContent.getBytes("UTF-8")), 0, null);
annotationFile.create(new ByteArrayInputStream(initialContent.getBytes(StandardCharsets.UTF_8)), 0, null);

IType type= fJProject1.findType(X_PATH.replace('/', '.'));
JavaEditor javaEditor= (JavaEditor) JavaUI.openInEditor(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;
Expand Down Expand Up @@ -168,7 +169,7 @@ public synchronized void load() {
IPath stateLocation= JavaPlugin.getDefault().getStateLocation().append(fFileName);
File file= stateLocation.toFile();
if (file.exists()) {
try (InputStreamReader reader= new InputStreamReader(new FileInputStream(file), "utf-8")) {//$NON-NLS-1$
try (InputStreamReader reader= new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8)) {
load(new InputSource(reader));
} catch (IOException | CoreException e) {
JavaPlugin.log(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
Expand Down Expand Up @@ -196,7 +197,7 @@ private void xmlReadRefactoring(JarPackageData jarPackage, Element element) thro
service.connect();
value= element.getAttribute("refactoring" + count); //$NON-NLS-1$
while (value != null && !"".equals(value)) { //$NON-NLS-1$
final ByteArrayInputStream stream= new ByteArrayInputStream(value.getBytes("UTF-8")); //$NON-NLS-1$
final ByteArrayInputStream stream= new ByteArrayInputStream(value.getBytes(StandardCharsets.UTF_8));
try {
final RefactoringHistory history= service.readRefactoringHistory(stream, RefactoringDescriptor.NONE);
if (history != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
Expand Down Expand Up @@ -173,11 +173,9 @@ private void xmlWriteRefactoring(JarPackageData jarPackage, Document document, E
try {
final ByteArrayOutputStream stream= new ByteArrayOutputStream();
service.writeRefactoringDescriptors(new RefactoringDescriptorProxy[] { proxies[index]}, stream, RefactoringDescriptor.NONE, true, null);
refactoring.setAttribute("refactoring" + count, stream.toString("UTF-8")); //$NON-NLS-1$ //$NON-NLS-2$
refactoring.setAttribute("refactoring" + count, stream.toString(StandardCharsets.UTF_8)); //$NON-NLS-1$
} catch (CoreException exception) {
JavaPlugin.log(exception);
} catch (UnsupportedEncodingException exception) {
Assert.isTrue(false);
}
}
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -950,7 +951,7 @@ protected int getIndent(PreferenceTreeNode<?> parent) {
protected void saveState() {
try (ByteArrayOutputStream out= new ByteArrayOutputStream()) {
writeExpansionState(fRoot, out);
fDialogSettings.put(fKeyPreferenceTreeExpansion, out.toString("UTF-8")); //$NON-NLS-1$
fDialogSettings.put(fKeyPreferenceTreeExpansion, out.toString(StandardCharsets.UTF_8));
} catch (IOException e) {
throw new AssertionError(e);
}
Expand All @@ -973,7 +974,7 @@ protected void restoreExpansionState() {
String treeState= fDialogSettings.get(fKeyPreferenceTreeExpansion);
if (treeState == null)
return;
try (ByteArrayInputStream in= new ByteArrayInputStream(treeState.getBytes("UTF-8"))) { //$NON-NLS-1$
try (ByteArrayInputStream in= new ByteArrayInputStream(treeState.getBytes(StandardCharsets.UTF_8))) {
fScrolledPageContent.setReflow(false);
readExpansionState(fRoot, in);
fScrolledPageContent.setReflow(true);
Expand Down Expand Up @@ -1617,7 +1618,7 @@ private String doGetPreviewCode(PreferenceTreeNode<?> node) {

protected void loadPreviews(String previewFile) {
String resource= "/preview/" + previewFile; //$NON-NLS-1$
try (Scanner s= new Scanner(getClass().getResourceAsStream(resource), "UTF-8")) { //$NON-NLS-1$
try (Scanner s= new Scanner(getClass().getResourceAsStream(resource), StandardCharsets.UTF_8)) {
fPreviewSources= s.useDelimiter("\\A").next(); //$NON-NLS-1$
}
}
Expand Down

0 comments on commit 679a887

Please sign in to comment.