From 88c7fb7d3f4eeeb0b00db00ffefa14861ae0896c Mon Sep 17 00:00:00 2001 From: Carsten Hammer Date: Sun, 15 Sep 2024 12:23:55 +0200 Subject: [PATCH] add tests for Java 8 --- ...ByteArrayOutputStreamExplicitEncoding.java | 12 +- .../ChannelsNewReaderExplicitEncoding.java | 12 +- .../ChannelsNewWriterExplicitEncoding.java | 12 +- .../helper/PrintWriterExplicitEncoding.java | 12 +- .../fix/helper/ScannerExplicitEncoding.java | 12 +- .../fix/helper/StringExplicitEncoding.java | 12 +- .../URLDecoderDecodeExplicitEncoding.java | 12 +- .../URLEncoderEncodeExplicitEncoding.java | 12 +- .../Java8/ExplicitEncodingCleanUpTest.java | 866 ++++++++++++++++++ 9 files changed, 914 insertions(+), 48 deletions(-) create mode 100644 sandbox_encoding_quickfix_test/src/org/eclipse/jdt/ui/tests/quickfix/Java8/ExplicitEncodingCleanUpTest.java diff --git a/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/ByteArrayOutputStreamExplicitEncoding.java b/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/ByteArrayOutputStreamExplicitEncoding.java index c5a4114d..242ca586 100644 --- a/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/ByteArrayOutputStreamExplicitEncoding.java +++ b/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/ByteArrayOutputStreamExplicitEncoding.java @@ -61,6 +61,12 @@ public class ByteArrayOutputStreamExplicitEncoding extends AbstractExplicitEncod @Override public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed,ChangeBehavior cb) { + if (!JavaModelUtil.is10OrHigher(compilationUnit.getJavaElement().getJavaProject())) { + /** + * For Java 9 and older just do nothing + */ + return; + } ReferenceHolder holder= new ReferenceHolder<>(); HelperVisitor.callMethodInvocationVisitor(ByteArrayOutputStream.class, METHOD_TOSTRING, compilationUnit, holder, nodesprocessed, (visited, aholder) -> processFoundNode(fixcore, operations, nodesprocessed, cb, visited, aholder)); } @@ -102,12 +108,6 @@ public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visite TextEditGroup group,ChangeBehavior cb, ReferenceHolder data) { ASTRewrite rewrite= cuRewrite.getASTRewrite(); AST ast= cuRewrite.getRoot().getAST(); - if (!JavaModelUtil.is10OrHigher(cuRewrite.getCu().getJavaProject())) { - /** - * For Java 9 and older just do nothing - */ - return; - } ASTNode callToCharsetDefaultCharset= computeCharsetASTNode(cuRewrite, ast, cb, ((Nodedata) data.get(visited)).encoding); /** * Add Charset.defaultCharset().displayName() as second (last) parameter of "toString()" call diff --git a/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/ChannelsNewReaderExplicitEncoding.java b/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/ChannelsNewReaderExplicitEncoding.java index 64329d33..9191a039 100644 --- a/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/ChannelsNewReaderExplicitEncoding.java +++ b/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/ChannelsNewReaderExplicitEncoding.java @@ -50,6 +50,12 @@ public class ChannelsNewReaderExplicitEncoding extends AbstractExplicitEncoding< @Override public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed,ChangeBehavior cb) { + if (!JavaModelUtil.is10OrHigher(compilationUnit.getJavaElement().getJavaProject())) { + /** + * For Java 9 and older just do nothing + */ + return; + } ReferenceHolder datah= new ReferenceHolder<>(); HelperVisitor.callMethodInvocationVisitor(Channels.class, METHOD_NEW_READER, compilationUnit, datah, nodesprocessed, (visited, holder) -> processFoundNode(fixcore, operations, nodesprocessed, cb, visited, holder)); } @@ -79,12 +85,6 @@ public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visite TextEditGroup group,ChangeBehavior cb, ReferenceHolder data) { ASTRewrite rewrite= cuRewrite.getASTRewrite(); AST ast= cuRewrite.getRoot().getAST(); - if (!JavaModelUtil.is10OrHigher(cuRewrite.getCu().getJavaProject())) { - /** - * For Java 9 and older just do nothing - */ - return; - } ASTNode callToCharsetDefaultCharset= computeCharsetASTNode(cuRewrite, ast, cb, ((Nodedata) data.get(visited)).encoding); /** * Add Charset.defaultCharset() as second (last) parameter diff --git a/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/ChannelsNewWriterExplicitEncoding.java b/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/ChannelsNewWriterExplicitEncoding.java index 56e1603a..84d37f76 100644 --- a/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/ChannelsNewWriterExplicitEncoding.java +++ b/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/ChannelsNewWriterExplicitEncoding.java @@ -49,6 +49,12 @@ public class ChannelsNewWriterExplicitEncoding extends AbstractExplicitEncoding< @Override public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed,ChangeBehavior cb) { + if (!JavaModelUtil.is10OrHigher(compilationUnit.getJavaElement().getJavaProject())) { + /** + * For Java 9 and older just do nothing + */ + return; + } ReferenceHolder datah= new ReferenceHolder<>(); HelperVisitor.callMethodInvocationVisitor(Channels.class, METHOD_NEW_WRITER, compilationUnit, datah, nodesprocessed, (visited, holder) -> processFoundNode(fixcore, operations, nodesprocessed, cb, visited, holder)); } @@ -78,12 +84,6 @@ public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visite TextEditGroup group,ChangeBehavior cb, ReferenceHolder data) { ASTRewrite rewrite= cuRewrite.getASTRewrite(); AST ast= cuRewrite.getRoot().getAST(); - if (!JavaModelUtil.is10OrHigher(cuRewrite.getCu().getJavaProject())) { - /** - * For Java 9 and older just do nothing - */ - return; - } ASTNode callToCharsetDefaultCharset= computeCharsetASTNode(cuRewrite, ast, cb, ((Nodedata) data.get(visited)).encoding); /** * Add Charset.defaultCharset() as second (last) parameter diff --git a/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/PrintWriterExplicitEncoding.java b/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/PrintWriterExplicitEncoding.java index 4fcb51f9..041fa807 100644 --- a/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/PrintWriterExplicitEncoding.java +++ b/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/PrintWriterExplicitEncoding.java @@ -55,6 +55,12 @@ public class PrintWriterExplicitEncoding extends AbstractExplicitEncoding operations, Set nodesprocessed,ChangeBehavior cb) { + if (!JavaModelUtil.is10OrHigher(compilationUnit.getJavaElement().getJavaProject())) { + /** + * For Java 9 and older just do nothing + */ + return; + } ReferenceHolder datah= new ReferenceHolder<>(); HelperVisitor.callClassInstanceCreationVisitor(PrintWriter.class, compilationUnit, datah, nodesprocessed, (visited, holder) -> processFoundNode(fixcore, operations, nodesprocessed, cb, visited, holder)); } @@ -71,12 +77,6 @@ public void rewrite(UseExplicitEncodingFixCore upp,final ClassInstanceCreation v TextEditGroup group,ChangeBehavior cb, ReferenceHolder data) { ASTRewrite rewrite= cuRewrite.getASTRewrite(); AST ast= cuRewrite.getRoot().getAST(); - if (!JavaModelUtil.is10OrHigher(cuRewrite.getCu().getJavaProject())) { - /** - * For Java 9 and older just do nothing - */ - return; - } ASTNode callToCharsetDefaultCharset= computeCharsetASTNode(cuRewrite, ast, cb, (String) data.get(visited)); /** * new FileOutputStream() diff --git a/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/ScannerExplicitEncoding.java b/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/ScannerExplicitEncoding.java index d82c7a9a..12ebaceb 100644 --- a/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/ScannerExplicitEncoding.java +++ b/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/ScannerExplicitEncoding.java @@ -59,6 +59,12 @@ public class ScannerExplicitEncoding extends AbstractExplicitEncoding operations, Set nodesprocessed,ChangeBehavior cb) { + if (!JavaModelUtil.is10OrHigher(compilationUnit.getJavaElement().getJavaProject())) { + /** + * For Java 9 and older just do nothing + */ + return; + } ReferenceHolder datah= new ReferenceHolder<>(); HelperVisitor.callClassInstanceCreationVisitor(Scanner.class, compilationUnit, datah, nodesprocessed, (visited, holder) -> processFoundNode(fixcore, operations, nodesprocessed, cb, visited, holder)); } @@ -118,12 +124,6 @@ public void rewrite(UseExplicitEncodingFixCore upp,final ClassInstanceCreation v TextEditGroup group,ChangeBehavior cb, ReferenceHolder data) { ASTRewrite rewrite= cuRewrite.getASTRewrite(); AST ast= cuRewrite.getRoot().getAST(); - if (!JavaModelUtil.is10OrHigher(cuRewrite.getCu().getJavaProject())) { - /** - * For Java 9 and older just do nothing - */ - return; - } ASTNode callToCharsetDefaultCharset= computeCharsetASTNode(cuRewrite, ast, cb, ((Nodedata) data.get(visited)).encoding); /** * Add Charset.defaultCharset() as second (last) parameter diff --git a/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/StringExplicitEncoding.java b/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/StringExplicitEncoding.java index 119421e7..5bfab3cb 100644 --- a/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/StringExplicitEncoding.java +++ b/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/StringExplicitEncoding.java @@ -51,6 +51,12 @@ public class StringExplicitEncoding extends AbstractExplicitEncoding operations, Set nodesprocessed,ChangeBehavior cb) { + if (!JavaModelUtil.is10OrHigher(compilationUnit.getJavaElement().getJavaProject())) { + /** + * For Java 9 and older just do nothing + */ + return; + } ReferenceHolder datah= new ReferenceHolder<>(); HelperVisitor.callClassInstanceCreationVisitor(String.class, compilationUnit, datah, nodesprocessed, (visited, holder) -> processFoundNode(fixcore, operations, nodesprocessed, cb, visited, holder)); } @@ -103,12 +109,6 @@ public void rewrite(UseExplicitEncodingFixCore upp,final ClassInstanceCreation v TextEditGroup group,ChangeBehavior cb, ReferenceHolder data) { ASTRewrite rewrite= cuRewrite.getASTRewrite(); AST ast= cuRewrite.getRoot().getAST(); - if (!JavaModelUtil.is10OrHigher(cuRewrite.getCu().getJavaProject())) { - /** - * For Java 9 and older just do nothing - */ - return; - } ASTNode callToCharsetDefaultCharset= computeCharsetASTNode(cuRewrite, ast, cb, ((Nodedata) data.get(visited)).encoding); /** * Add Charset.defaultCharset() as second (last) parameter diff --git a/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/URLDecoderDecodeExplicitEncoding.java b/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/URLDecoderDecodeExplicitEncoding.java index 575258fa..a98ad28e 100644 --- a/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/URLDecoderDecodeExplicitEncoding.java +++ b/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/URLDecoderDecodeExplicitEncoding.java @@ -63,6 +63,12 @@ public class URLDecoderDecodeExplicitEncoding extends AbstractExplicitEncoding operations, Set nodesprocessed,ChangeBehavior cb) { + if (!JavaModelUtil.is10OrHigher(compilationUnit.getJavaElement().getJavaProject())) { + /** + * For Java 9 and older just do nothing + */ + return; + } ReferenceHolder datah= new ReferenceHolder<>(); HelperVisitor.callMethodInvocationVisitor(URLDecoder.class, METHOD_DECODE, compilationUnit, datah, nodesprocessed, (visited, holder) -> processFoundNode(fixcore, operations, nodesprocessed, cb, visited, holder)); } @@ -110,12 +116,6 @@ public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visite TextEditGroup group,ChangeBehavior cb, ReferenceHolder data) { ASTRewrite rewrite= cuRewrite.getASTRewrite(); AST ast= cuRewrite.getRoot().getAST(); - if (!JavaModelUtil.is10OrHigher(cuRewrite.getCu().getJavaProject())) { - /** - * For Java 9 and older just do nothing - */ - return; - } ASTNode callToCharsetDefaultCharset= computeCharsetASTNode(cuRewrite, ast, cb, ((Nodedata) data.get(visited)).encoding); /** * Add Charset.defaultCharset() or StandardCharsets.UTF_8 as second (last) parameter diff --git a/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/URLEncoderEncodeExplicitEncoding.java b/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/URLEncoderEncodeExplicitEncoding.java index a07b891f..f9f265e0 100644 --- a/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/URLEncoderEncodeExplicitEncoding.java +++ b/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/URLEncoderEncodeExplicitEncoding.java @@ -63,6 +63,12 @@ public class URLEncoderEncodeExplicitEncoding extends AbstractExplicitEncoding operations, Set nodesprocessed,ChangeBehavior cb) { + if (!JavaModelUtil.is10OrHigher(compilationUnit.getJavaElement().getJavaProject())) { + /** + * For Java 9 and older just do nothing + */ + return; + } ReferenceHolder datah= new ReferenceHolder<>(); HelperVisitor.callMethodInvocationVisitor(URLEncoder.class, METHOD_ENCODE, compilationUnit, datah, nodesprocessed, (visited, holder) -> processFoundNode(fixcore, operations, nodesprocessed, cb, visited, holder)); } @@ -110,12 +116,6 @@ public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visite TextEditGroup group,ChangeBehavior cb, ReferenceHolder data) { ASTRewrite rewrite= cuRewrite.getASTRewrite(); AST ast= cuRewrite.getRoot().getAST(); - if (!JavaModelUtil.is10OrHigher(cuRewrite.getCu().getJavaProject())) { - /** - * For Java 9 and older just do nothing - */ - return; - } ASTNode callToCharsetDefaultCharset= computeCharsetASTNode(cuRewrite, ast, cb, ((Nodedata) data.get(visited)).encoding); /** * Add Charset.defaultCharset() as second (last) parameter diff --git a/sandbox_encoding_quickfix_test/src/org/eclipse/jdt/ui/tests/quickfix/Java8/ExplicitEncodingCleanUpTest.java b/sandbox_encoding_quickfix_test/src/org/eclipse/jdt/ui/tests/quickfix/Java8/ExplicitEncodingCleanUpTest.java new file mode 100644 index 00000000..5e64ed23 --- /dev/null +++ b/sandbox_encoding_quickfix_test/src/org/eclipse/jdt/ui/tests/quickfix/Java8/ExplicitEncodingCleanUpTest.java @@ -0,0 +1,866 @@ +package org.eclipse.jdt.ui.tests.quickfix.Java8; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.IPackageFragment; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; +import org.sandbox.jdt.internal.corext.fix2.MYCleanUpConstants; +import org.sandbox.jdt.ui.tests.quickfix.rules.AbstractEclipseJava; +import org.sandbox.jdt.ui.tests.quickfix.rules.EclipseJava8; + +public class ExplicitEncodingCleanUpTest { + + @RegisterExtension + AbstractEclipseJava context = new EclipseJava8(); + + enum ExplicitEncodingPatterns { + + CHARSET(""" + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.charset.Charset; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + Charset cs1= Charset.forName("UTF-8"); + Charset cs1b= Charset.forName("Utf-8"); + Charset cs2= Charset.forName("UTF-16"); + Charset cs3= Charset.forName("UTF-16BE"); + Charset cs4= Charset.forName("UTF-16LE"); + Charset cs5= Charset.forName("ISO-8859-1"); + Charset cs6= Charset.forName("US-ASCII"); + String result= cs1.toString(); + } + } + } + """, + +// """ +// package test1; +// +// import java.io.ByteArrayOutputStream; +// import java.io.InputStreamReader; +// import java.io.FileInputStream; +// import java.io.FileReader; +// import java.io.Reader; +// import java.nio.charset.Charset; +// import java.nio.charset.StandardCharsets; +// import java.io.FileNotFoundException; +// +// public class E1 { +// void method(String filename) { +// Charset cs1= StandardCharsets.UTF_8; +// Charset cs1b= StandardCharsets.UTF_8; +// Charset cs2= StandardCharsets.UTF_16; +// Charset cs3= StandardCharsets.UTF_16BE; +// Charset cs4= StandardCharsets.UTF_16LE; +// Charset cs5= StandardCharsets.ISO_8859_1; +// Charset cs6= StandardCharsets.US_ASCII; +// String result= cs1.toString(); +// } +// } +// } +// """), + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.charset.Charset; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + Charset cs1= Charset.forName("UTF-8"); + Charset cs1b= Charset.forName("Utf-8"); + Charset cs2= Charset.forName("UTF-16"); + Charset cs3= Charset.forName("UTF-16BE"); + Charset cs4= Charset.forName("UTF-16LE"); + Charset cs5= Charset.forName("ISO-8859-1"); + Charset cs6= Charset.forName("US-ASCII"); + String result= cs1.toString(); + } + } + } + """), + BYTEARRAYOUTSTREAM(""" + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + ByteArrayOutputStream ba=new ByteArrayOutputStream(); + String result=ba.toString(); + ByteArrayOutputStream ba2=new ByteArrayOutputStream(); + String result2=ba2.toString("UTF-8"); + } + } + } + """, + + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + ByteArrayOutputStream ba=new ByteArrayOutputStream(); + String result=ba.toString(); + ByteArrayOutputStream ba2=new ByteArrayOutputStream(); + String result2=ba2.toString("UTF-8"); + } + } + } + """), + FILEREADER(""" + package test1; + + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + Reader is=new FileReader(filename); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """, + + """ + package test1; + + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.charset.Charset; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + Reader is=new InputStreamReader(new FileInputStream(filename), Charset.defaultCharset()); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """), + FILEWRITER(""" + package test1; + + import java.io.FileWriter; + import java.io.Writer; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + Writer fw=new FileWriter(filename); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """, + + """ + package test1; + + import java.io.FileWriter; + import java.io.OutputStreamWriter; + import java.io.Writer; + import java.nio.charset.Charset; + import java.io.FileNotFoundException; + import java.io.FileOutputStream; + + public class E1 { + void method(String filename) { + try { + Writer fw=new OutputStreamWriter(new FileOutputStream(filename), Charset.defaultCharset()); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """), + INPUTSTREAMREADER( + """ + package test1; + + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + InputStreamReader is1=new InputStreamReader(new FileInputStream("file1.txt")); //$NON-NLS-1$ + InputStreamReader is2=new InputStreamReader(new FileInputStream("file2.txt"), "UTF-8"); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """, + + """ + package test1; + + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.charset.Charset; + import java.nio.charset.StandardCharsets; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + InputStreamReader is1=new InputStreamReader(new FileInputStream("file1.txt"), Charset.defaultCharset()); //$NON-NLS-1$ + InputStreamReader is2=new InputStreamReader(new FileInputStream("file2.txt"), StandardCharsets.UTF_8); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """), + OUTPUTSTREAMWRITER( + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream("")); //$NON-NLS-1$ + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), "UTF-8"); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """, + + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.charset.Charset; + import java.nio.charset.StandardCharsets; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), Charset.defaultCharset()); //$NON-NLS-1$ + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), StandardCharsets.UTF_8); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """), + CHANNELSNEWREADER(""" + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.channels.ReadableByteChannel; + import java.nio.charset.StandardCharsets; + import java.nio.channels.Channels; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + ReadableByteChannel ch; + Reader r=Channels.newReader(ch,"UTF-8"); //$NON-NLS-1$ + } + } + } + """, + + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.channels.ReadableByteChannel; + import java.nio.charset.StandardCharsets; + import java.nio.channels.Channels; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + ReadableByteChannel ch; + Reader r=Channels.newReader(ch,"UTF-8"); //$NON-NLS-1$ + } + } + } + """), + CHANNELSNEWWRITER(""" + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Writer; + import java.nio.channels.WritableByteChannel; + import java.nio.charset.StandardCharsets; + import java.nio.channels.Channels; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + WritableByteChannel ch; + Writer w=Channels.newWriter(ch,"UTF-8"); //$NON-NLS-1$ + } + } + } + """, + + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Writer; + import java.nio.channels.WritableByteChannel; + import java.nio.charset.StandardCharsets; + import java.nio.channels.Channels; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + WritableByteChannel ch; + Writer w=Channels.newWriter(ch,"UTF-8"); //$NON-NLS-1$ + } + } + } + """), + PRINTWRITER(""" + package test1; + + import java.io.PrintWriter; + import java.io.Writer; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + Writer w=new PrintWriter(filename); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """, + + """ + package test1; + + import java.io.PrintWriter; + import java.io.Writer; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + try { + Writer w=new PrintWriter(filename); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """), + STRINGGETBYTES(""" + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + String s="asdf"; //$NON-NLS-1$ + byte[] bytes= s.getBytes(); + byte[] bytes2= s.getBytes("UTF-8"); + System.out.println(bytes.length); + } + } + } + """, + + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.charset.Charset; + import java.nio.charset.StandardCharsets; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + String s="asdf"; //$NON-NLS-1$ + byte[] bytes= s.getBytes(Charset.defaultCharset()); + byte[] bytes2= s.getBytes(StandardCharsets.UTF_8); + System.out.println(bytes.length); + } + } + } + """), + STRING(""" + package test1; + + import java.io.FileNotFoundException; + import java.io.UnsupportedEncodingException; + + public class E5 { + + static void bla() throws FileNotFoundException, UnsupportedEncodingException { + byte[] b= {(byte)59}; + String s1=new String(b,"UTF-8"); + String s2=new String(b,0,1,"UTF-8"); + } + } + """, + + """ + package test1; + + import java.io.FileNotFoundException; + import java.io.UnsupportedEncodingException; + + public class E5 { + + static void bla() throws FileNotFoundException, UnsupportedEncodingException { + byte[] b= {(byte)59}; + String s1=new String(b,"UTF-8"); + String s2=new String(b,0,1,"UTF-8"); + } + } + """), + PROPERTIESSTORETOXML(""" + package test1; + + import java.io.FileOutputStream; + import java.io.IOException; + import java.util.Properties; + + public class E1 { + static void blu() throws IOException { + Properties p=new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, null, "UTF-8"); + } + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, null); + } + } + } + """, + + """ + package test1; + + import java.io.FileOutputStream; + import java.io.IOException; + import java.nio.charset.StandardCharsets; + import java.util.Properties; + + public class E1 { + static void blu() throws IOException { + Properties p=new Properties(); + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, null, StandardCharsets.UTF_8); + } + try (FileOutputStream os = new FileOutputStream("out.xml")) { + p.storeToXML(os, null, StandardCharsets.UTF_8); + } + } + } + """), + URLDECODER(""" + package test1; + import java.io.UnsupportedEncodingException; + import java.net.URLDecoder; + + public class E2 { + + static void bla() throws UnsupportedEncodingException { + String url=URLDecoder.decode("asdf","UTF-8"); + String url2=URLDecoder.decode("asdf"); + } + } + """, """ + package test1; + import java.io.UnsupportedEncodingException; + import java.net.URLDecoder; + + public class E2 { + + static void bla() throws UnsupportedEncodingException { + String url=URLDecoder.decode("asdf","UTF-8"); + String url2=URLDecoder.decode("asdf"); + } + } + """), + URLENCODER(""" + package test1; + import java.io.UnsupportedEncodingException; + import java.net.URLEncoder; + + public class E2 { + + static void bla() throws UnsupportedEncodingException { + String url=URLEncoder.encode("asdf","UTF-8"); + String url4=URLEncoder.encode("asdf"); + } + } + """, """ + package test1; + import java.io.UnsupportedEncodingException; + import java.net.URLEncoder; + + public class E2 { + + static void bla() throws UnsupportedEncodingException { + String url=URLEncoder.encode("asdf","UTF-8"); + String url4=URLEncoder.encode("asdf"); + } + } + """), + SCANNER(""" + package test1; + import java.io.File; + import java.io.FileNotFoundException; + import java.util.Scanner; + + public class E3 { + + static void bla() throws FileNotFoundException { + Scanner s=new Scanner(new File("asdf"),"UTF-8"); + Scanner s2=new Scanner("asdf","UTF-8"); + Scanner s3=new Scanner("asdf"); + } + } + """, """ + package test1; + import java.io.File; + import java.io.FileNotFoundException; + import java.util.Scanner; + + public class E3 { + + static void bla() throws FileNotFoundException { + Scanner s=new Scanner(new File("asdf"),"UTF-8"); + Scanner s2=new Scanner("asdf","UTF-8"); + Scanner s3=new Scanner("asdf"); + } + } + """), + FORMATTER(""" + package test1; + import java.io.File; + import java.io.FileNotFoundException; + import java.io.UnsupportedEncodingException; + import java.util.Formatter; + + public class E4 { + + static void bla() throws FileNotFoundException, UnsupportedEncodingException { + Formatter s=new Formatter(new File("asdf"),"UTF-8"); + } + } + """, """ + package test1; + import java.io.File; + import java.io.FileNotFoundException; + import java.io.UnsupportedEncodingException; + import java.nio.charset.StandardCharsets; + import java.util.Formatter; + + public class E4 { + + static void bla() throws FileNotFoundException, UnsupportedEncodingException { + Formatter s=new Formatter(new File("asdf"),StandardCharsets.UTF_8); + } + } + """), + THREE(""" + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + String s="asdf"; //$NON-NLS-1$ + byte[] bytes= s.getBytes(); + System.out.println(bytes.length); + ByteArrayOutputStream ba=new ByteArrayOutputStream(); + String result=ba.toString(); + try { + InputStreamReader is=new InputStreamReader(new FileInputStream("")); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream("")); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + Reader is=new FileReader(filename); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """, + + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.charset.Charset; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + String s="asdf"; //$NON-NLS-1$ + byte[] bytes= s.getBytes(Charset.defaultCharset()); + System.out.println(bytes.length); + ByteArrayOutputStream ba=new ByteArrayOutputStream(); + String result=ba.toString(); + try { + InputStreamReader is=new InputStreamReader(new FileInputStream(""), Charset.defaultCharset()); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), Charset.defaultCharset()); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + Reader is=new InputStreamReader(new FileInputStream(filename), Charset.defaultCharset()); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """), + ENCODINGASSTRINGPARAMETER( + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + String s="asdf"; //$NON-NLS-1$ + //byte[] bytes= s.getBytes(StandardCharsets.UTF_8); + byte[] bytes= s.getBytes("Utf-8"); + System.out.println(bytes.length); + ByteArrayOutputStream ba=new ByteArrayOutputStream(); + String result=ba.toString(); + try { + InputStreamReader is=new InputStreamReader(new FileInputStream(""), "UTF-8"); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), "UTF-8"); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + Reader is=new FileReader(filename); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """, + + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.FileInputStream; + import java.io.FileReader; + import java.io.Reader; + import java.nio.charset.Charset; + import java.nio.charset.StandardCharsets; + import java.io.FileNotFoundException; + + public class E1 { + void method(String filename) { + String s="asdf"; //$NON-NLS-1$ + //byte[] bytes= s.getBytes(StandardCharsets.UTF_8); + byte[] bytes= s.getBytes(StandardCharsets.UTF_8); + System.out.println(bytes.length); + ByteArrayOutputStream ba=new ByteArrayOutputStream(); + String result=ba.toString(); + try { + InputStreamReader is=new InputStreamReader(new FileInputStream(""), StandardCharsets.UTF_8); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + OutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(""), StandardCharsets.UTF_8); //$NON-NLS-1$ + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + Reader is=new InputStreamReader(new FileInputStream(filename), Charset.defaultCharset()); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """); + + String given; + String expected; + + ExplicitEncodingPatterns(String given, String expected) { + this.given = given; + this.expected = expected; + } + } + + @ParameterizedTest + @EnumSource(ExplicitEncodingPatterns.class) + public void testExplicitEncodingParametrized(ExplicitEncodingPatterns test) throws CoreException { + IPackageFragment pack = context.getfSourceFolder().createPackageFragment("test1", false, null); + ICompilationUnit cu = pack.createCompilationUnit("E1.java", test.given, false, null); + context.enable(MYCleanUpConstants.EXPLICITENCODING_CLEANUP); + context.assertRefactoringResultAsExpected(new ICompilationUnit[] { cu }, new String[] { test.expected }, null); + } + + @Test + public void testExplicitEncodingdonttouch() throws CoreException { + IPackageFragment pack = context.getfSourceFolder().createPackageFragment("test1", false, null); + ICompilationUnit cu = pack.createCompilationUnit("E2.java", + """ + package test1; + + import java.io.ByteArrayOutputStream; + import java.io.InputStreamReader; + import java.io.IOException; + import java.nio.charset.Charset; + import java.io.FileInputStream; + import java.io.FileNotFoundException; + import java.io.UnsupportedEncodingException; + + public class E2 { + void method() throws UnsupportedEncodingException, IOException { + String s="asdf"; //$NON-NLS-1$ + byte[] bytes= s.getBytes(Charset.defaultCharset()); + System.out.println(bytes.length); + ByteArrayOutputStream ba=new ByteArrayOutputStream(); + String result=ba.toString(Charset.defaultCharset().displayName()); + try ( + InputStreamReader is=new InputStreamReader(new FileInputStream(""), Charset.defaultCharset()); //$NON-NLS-1$ + ){ } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + """, + false, null); + + context.enable(MYCleanUpConstants.EXPLICITENCODING_CLEANUP); + + context.assertRefactoringHasNoChange(new ICompilationUnit[] { cu }); + } +}