diff --git a/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/UseExplicitEncodingFixCore.java b/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/UseExplicitEncodingFixCore.java index d00831f6..97e69ddb 100644 --- a/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/UseExplicitEncodingFixCore.java +++ b/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/UseExplicitEncodingFixCore.java @@ -25,6 +25,7 @@ import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; import org.eclipse.jdt.internal.corext.util.Messages; import org.eclipse.text.edits.TextEditGroup; +import org.sandbox.jdt.internal.common.ReferenceHolder; import org.sandbox.jdt.internal.corext.fix.helper.AbstractExplicitEncoding; import org.sandbox.jdt.internal.corext.fix.helper.AbstractExplicitEncoding.ChangeBehavior; import org.sandbox.jdt.internal.corext.fix.helper.ByteArrayOutputStreamExplicitEncoding; @@ -68,13 +69,13 @@ public void findOperations(final CompilationUnit compilationUnit,final Set { public enum ChangeBehavior {KEEP, USE_UTF8, USE_UTF8_AGGREGATE} + ReferenceHolder datah= new ReferenceHolder<>(); + + protected static final String ENCODING = "encoding"; //$NON-NLS-1$ + protected static final String REPLACE = "replace"; //$NON-NLS-1$ public abstract void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed, ChangeBehavior cb); public abstract void rewrite(UseExplicitEncodingFixCore useExplicitEncodingFixCore, T visited, CompilationUnitRewrite cuRewrite, - TextEditGroup group, ChangeBehavior cb); + TextEditGroup group, ChangeBehavior cb, ReferenceHolder data); - protected Expression computeCharsetASTNode(final CompilationUnitRewrite cuRewrite, ChangeBehavior cb, AST ast) { + protected Expression computeCharsetASTNode(final CompilationUnitRewrite cuRewrite, ChangeBehavior cb, AST ast, Charset charset) { Expression callToCharsetDefaultCharset=null; switch(cb) { case KEEP: + if(charset!=null) { + callToCharsetDefaultCharset= addCharsetUTF8(cuRewrite, ast); + } else { // needs Java 1.5 - callToCharsetDefaultCharset= addCharsetComputation(cuRewrite, ast); + callToCharsetDefaultCharset= addCharsetComputation(cuRewrite, ast); + } break; case USE_UTF8_AGGREGATE: /** @@ -131,10 +140,11 @@ protected MethodInvocation addCharsetComputation(final CompilationUnitRewrite cu * @param cuRewrite CompilationUnitRewrite * @param ast AST * @param cb ChangeBehavior + * @param charset * @return MethodInvocation that returns String */ - protected MethodInvocation addCharsetStringComputation(final CompilationUnitRewrite cuRewrite, AST ast, ChangeBehavior cb) { - Expression callToCharsetDefaultCharset= computeCharsetASTNode(cuRewrite, cb, ast); + protected MethodInvocation addCharsetStringComputation(final CompilationUnitRewrite cuRewrite, AST ast, ChangeBehavior cb, Charset charset) { + Expression callToCharsetDefaultCharset= computeCharsetASTNode(cuRewrite, cb, ast, charset); /** * Add second call to Charset.defaultCharset().displayName() */ 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 d4feb13f..2bfbafcc 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 @@ -16,6 +16,7 @@ import static org.sandbox.jdt.internal.common.LibStandardNames.METHOD_TOSTRING; import java.io.ByteArrayOutputStream; +import java.nio.charset.Charset; import java.util.Set; import org.eclipse.jdt.core.dom.AST; @@ -30,7 +31,9 @@ import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; import org.eclipse.jdt.internal.corext.util.JavaModelUtil; import org.eclipse.text.edits.TextEditGroup; +import org.sandbox.jdt.internal.common.ReferenceHolder; import org.sandbox.jdt.internal.corext.fix.UseExplicitEncodingFixCore; +import org.sandbox.jdt.internal.corext.fix.helper.AbstractExplicitEncoding.ChangeBehavior; /** * Change from @@ -58,7 +61,7 @@ public boolean visit(final MethodInvocation visited) { return false; } if (ASTNodes.usesGivenSignature(visited, ByteArrayOutputStream.class.getCanonicalName(), METHOD_TOSTRING)) { - operations.add(fixcore.rewrite(visited, cb)); + operations.add(fixcore.rewrite(visited, cb, datah)); nodesprocessed.add(visited); return false; } @@ -69,7 +72,7 @@ public boolean visit(final MethodInvocation visited) { @Override public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visited, final CompilationUnitRewrite cuRewrite, - TextEditGroup group,ChangeBehavior cb) { + TextEditGroup group,ChangeBehavior cb, ReferenceHolder data) { ASTRewrite rewrite= cuRewrite.getASTRewrite(); AST ast= cuRewrite.getRoot().getAST(); if (!JavaModelUtil.is50OrHigher(cuRewrite.getCu().getJavaProject())) { @@ -78,7 +81,7 @@ public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visite */ return; } - MethodInvocation callToCharsetDefaultCharsetDisplayname= addCharsetStringComputation(cuRewrite, ast, cb); + MethodInvocation callToCharsetDefaultCharsetDisplayname= addCharsetStringComputation(cuRewrite, ast, cb, (Charset) data.get(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/FileReaderExplicitEncoding.java b/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/FileReaderExplicitEncoding.java index 53393025..9e031faa 100644 --- a/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/FileReaderExplicitEncoding.java +++ b/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/FileReaderExplicitEncoding.java @@ -16,6 +16,9 @@ import java.io.FileInputStream; import java.io.FileReader; import java.io.InputStreamReader; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.List; import java.util.Set; import org.eclipse.jdt.core.dom.AST; @@ -24,13 +27,17 @@ import org.eclipse.jdt.core.dom.ClassInstanceCreation; import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.dom.ITypeBinding; +import org.eclipse.jdt.core.dom.StringLiteral; import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; import org.eclipse.jdt.internal.corext.dom.ASTNodes; import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation; import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; import org.eclipse.jdt.internal.corext.util.JavaModelUtil; import org.eclipse.text.edits.TextEditGroup; +import org.sandbox.jdt.internal.common.HelperVisitor; +import org.sandbox.jdt.internal.common.ReferenceHolder; import org.sandbox.jdt.internal.corext.fix.UseExplicitEncodingFixCore; +import org.sandbox.jdt.internal.corext.fix.helper.AbstractExplicitEncoding.ChangeBehavior; /** * Change * @@ -44,26 +51,51 @@ public class FileReaderExplicitEncoding extends AbstractExplicitEncoding operations, Set nodesprocessed,ChangeBehavior cb) { - compilationUnit.accept(new ASTVisitor() { - @Override - public boolean visit(final ClassInstanceCreation visited) { - if(nodesprocessed.contains(visited)) { - return false; - } - ITypeBinding binding= visited.resolveTypeBinding(); - if (FileReader.class.getSimpleName().equals(binding.getName())) { - operations.add(fixcore.rewrite(visited, cb)); - nodesprocessed.add(visited); + HelperVisitor.callClassInstanceCreationVisitor(FileReader.class, compilationUnit, datah, nodesprocessed, (visited, holder_a) -> { + List arguments= visited.arguments(); + if(nodesprocessed.contains(visited) || (arguments.size()>2)) { + return false; + } + switch (arguments.size()) { + case 1: + break; + case 2: + if(!(arguments.get(1) instanceof StringLiteral)) return false; + StringLiteral argstring3= (StringLiteral) arguments.get(1); + if (!("UTF-8".equals(argstring3.getLiteralValue()))) { //$NON-NLS-1$ return false; } - return true; + holder_a.put(ENCODING,StandardCharsets.UTF_8); + holder_a.put(REPLACE,argstring3); + break; + default: + return false; } + operations.add(fixcore.rewrite(visited, cb, holder_a)); + nodesprocessed.add(visited); + return false; }); + +// compilationUnit.accept(new ASTVisitor() { +// @Override +// public boolean visit(final ClassInstanceCreation visited) { +// if(nodesprocessed.contains(visited)) { +// return false; +// } +// ITypeBinding binding= visited.resolveTypeBinding(); +// if (FileReader.class.getSimpleName().equals(binding.getName())) { +// operations.add(fixcore.rewrite(visited, cb, datah)); +// nodesprocessed.add(visited); +// return false; +// } +// return true; +// } +// }); } @Override public void rewrite(UseExplicitEncodingFixCore upp,final ClassInstanceCreation visited, final CompilationUnitRewrite cuRewrite, - TextEditGroup group,ChangeBehavior cb) { + TextEditGroup group,ChangeBehavior cb, ReferenceHolder data) { ASTRewrite rewrite= cuRewrite.getASTRewrite(); AST ast= cuRewrite.getRoot().getAST(); if (!JavaModelUtil.is50OrHigher(cuRewrite.getCu().getJavaProject())) { @@ -72,7 +104,7 @@ public void rewrite(UseExplicitEncodingFixCore upp,final ClassInstanceCreation v */ return; } - ASTNode callToCharsetDefaultCharset= computeCharsetASTNode(cuRewrite, cb, ast); + ASTNode callToCharsetDefaultCharset= computeCharsetASTNode(cuRewrite, cb, ast, (Charset) data.get(ENCODING)); /** * new FileInputStream() */ diff --git a/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/FileWriterExplicitEncoding.java b/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/FileWriterExplicitEncoding.java index d26285ea..cc854039 100644 --- a/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/FileWriterExplicitEncoding.java +++ b/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/FileWriterExplicitEncoding.java @@ -16,6 +16,7 @@ import java.io.FileOutputStream; import java.io.FileWriter; import java.io.OutputStreamWriter; +import java.nio.charset.Charset; import java.util.Set; import org.eclipse.jdt.core.dom.AST; @@ -30,7 +31,9 @@ import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; import org.eclipse.jdt.internal.corext.util.JavaModelUtil; import org.eclipse.text.edits.TextEditGroup; +import org.sandbox.jdt.internal.common.ReferenceHolder; import org.sandbox.jdt.internal.corext.fix.UseExplicitEncodingFixCore; +import org.sandbox.jdt.internal.corext.fix.helper.AbstractExplicitEncoding.ChangeBehavior; /** * Change * @@ -52,7 +55,7 @@ public boolean visit(final ClassInstanceCreation visited) { } ITypeBinding binding= visited.resolveTypeBinding(); if (FileWriter.class.getSimpleName().equals(binding.getName())) { - operations.add(fixcore.rewrite(visited, cb)); + operations.add(fixcore.rewrite(visited, cb, datah)); nodesprocessed.add(visited); return false; } @@ -63,7 +66,7 @@ public boolean visit(final ClassInstanceCreation visited) { @Override public void rewrite(UseExplicitEncodingFixCore upp,final ClassInstanceCreation visited, final CompilationUnitRewrite cuRewrite, - TextEditGroup group,ChangeBehavior cb) { + TextEditGroup group,ChangeBehavior cb, ReferenceHolder data) { ASTRewrite rewrite= cuRewrite.getASTRewrite(); AST ast= cuRewrite.getRoot().getAST(); if (!JavaModelUtil.is50OrHigher(cuRewrite.getCu().getJavaProject())) { @@ -72,7 +75,7 @@ public void rewrite(UseExplicitEncodingFixCore upp,final ClassInstanceCreation v */ return; } - ASTNode callToCharsetDefaultCharset= computeCharsetASTNode(cuRewrite, cb, ast); + ASTNode callToCharsetDefaultCharset= computeCharsetASTNode(cuRewrite, cb, ast, (Charset) data.get(ENCODING)); /** * new FileInputStream() */ diff --git a/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/InputStreamReaderExplicitEncoding.java b/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/InputStreamReaderExplicitEncoding.java index 9393dbe1..753ac785 100644 --- a/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/InputStreamReaderExplicitEncoding.java +++ b/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/InputStreamReaderExplicitEncoding.java @@ -14,12 +14,16 @@ package org.sandbox.jdt.internal.corext.fix.helper; import java.io.InputStreamReader; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.List; import java.util.Set; import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.ASTNode; import org.eclipse.jdt.core.dom.ClassInstanceCreation; import org.eclipse.jdt.core.dom.CompilationUnit; +import org.eclipse.jdt.core.dom.StringLiteral; import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; import org.eclipse.jdt.core.dom.rewrite.ListRewrite; import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation; @@ -38,12 +42,27 @@ public class InputStreamReaderExplicitEncoding extends AbstractExplicitEncoding< @Override public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed,ChangeBehavior cb) { - ReferenceHolder dataholder= new ReferenceHolder<>(); - HelperVisitor.callClassInstanceCreationVisitor(InputStreamReader.class, compilationUnit, dataholder, nodesprocessed, (visited, holder_a) -> { - if(nodesprocessed.contains(visited) || (visited.arguments().size()>1)) { + HelperVisitor.callClassInstanceCreationVisitor(InputStreamReader.class, compilationUnit, datah, nodesprocessed, (visited, holder_a) -> { + List arguments= visited.arguments(); + if(nodesprocessed.contains(visited) || (arguments.size()>2)) { return false; } - operations.add(fixcore.rewrite(visited, cb)); + switch (arguments.size()) { + case 2: + if(!(arguments.get(1) instanceof StringLiteral)) return false; + StringLiteral argstring3= (StringLiteral) arguments.get(1); + if (!("UTF-8".equals(argstring3.getLiteralValue()))) { //$NON-NLS-1$ + return false; + } + holder_a.put(ENCODING,StandardCharsets.UTF_8); + holder_a.put(REPLACE,argstring3); + break; + case 1: + break; + default: + break; + } + operations.add(fixcore.rewrite(visited, cb, holder_a)); nodesprocessed.add(visited); return false; }); @@ -51,7 +70,7 @@ public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilation @Override public void rewrite(UseExplicitEncodingFixCore upp,final ClassInstanceCreation visited, final CompilationUnitRewrite cuRewrite, - TextEditGroup group,ChangeBehavior cb) { + TextEditGroup group,ChangeBehavior cb, ReferenceHolder data) { ASTRewrite rewrite= cuRewrite.getASTRewrite(); AST ast= cuRewrite.getRoot().getAST(); if (!JavaModelUtil.is50OrHigher(cuRewrite.getCu().getJavaProject())) { @@ -60,12 +79,16 @@ public void rewrite(UseExplicitEncodingFixCore upp,final ClassInstanceCreation v */ return; } - ASTNode callToCharsetDefaultCharset= computeCharsetASTNode(cuRewrite, cb, ast); + ASTNode callToCharsetDefaultCharset= computeCharsetASTNode(cuRewrite, cb, ast, (Charset) data.get(ENCODING)); /** * Add Charset.defaultCharset() as second (last) parameter */ ListRewrite listRewrite= rewrite.getListRewrite(visited, ClassInstanceCreation.ARGUMENTS_PROPERTY); - listRewrite.insertLast(callToCharsetDefaultCharset, group); + if(data.get(ENCODING)!= null) { + listRewrite.replace((ASTNode) data.get(REPLACE), callToCharsetDefaultCharset, group); + } else { + listRewrite.insertLast(callToCharsetDefaultCharset, group); + } } @Override diff --git a/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/OutputStreamWriterExplicitEncoding.java b/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/OutputStreamWriterExplicitEncoding.java index d632b9a2..4d7bdecd 100644 --- a/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/OutputStreamWriterExplicitEncoding.java +++ b/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/OutputStreamWriterExplicitEncoding.java @@ -13,7 +13,11 @@ *******************************************************************************/ package org.sandbox.jdt.internal.corext.fix.helper; +import java.io.InputStreamReader; import java.io.OutputStreamWriter; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.List; import java.util.Set; import org.eclipse.jdt.core.dom.AST; @@ -22,13 +26,17 @@ import org.eclipse.jdt.core.dom.ClassInstanceCreation; import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.dom.ITypeBinding; +import org.eclipse.jdt.core.dom.StringLiteral; import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; import org.eclipse.jdt.core.dom.rewrite.ListRewrite; import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation; import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; import org.eclipse.jdt.internal.corext.util.JavaModelUtil; import org.eclipse.text.edits.TextEditGroup; +import org.sandbox.jdt.internal.common.HelperVisitor; +import org.sandbox.jdt.internal.common.ReferenceHolder; import org.sandbox.jdt.internal.corext.fix.UseExplicitEncodingFixCore; +import org.sandbox.jdt.internal.corext.fix.helper.AbstractExplicitEncoding.ChangeBehavior; /** * Change * @@ -41,26 +49,51 @@ public class OutputStreamWriterExplicitEncoding extends AbstractExplicitEncoding @Override public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed,ChangeBehavior cb) { - compilationUnit.accept(new ASTVisitor() { - @Override - public boolean visit(final ClassInstanceCreation visited) { - if(nodesprocessed.contains(visited) || (visited.arguments().size()>1)) { - return false; - } - ITypeBinding binding= visited.resolveTypeBinding(); - if (OutputStreamWriter.class.getSimpleName().equals(binding.getName())) { - operations.add(fixcore.rewrite(visited, cb)); - nodesprocessed.add(visited); + HelperVisitor.callClassInstanceCreationVisitor(OutputStreamWriter.class, compilationUnit, datah, nodesprocessed, (visited, holder_a) -> { + List arguments= visited.arguments(); + if(nodesprocessed.contains(visited) || (arguments.size()>2)) { + return false; + } + switch (arguments.size()) { + case 2: + if(!(arguments.get(1) instanceof StringLiteral)) return false; + StringLiteral argstring3= (StringLiteral) arguments.get(1); + if (!("UTF-8".equals(argstring3.getLiteralValue()))) { //$NON-NLS-1$ return false; } - return true; + holder_a.put(ENCODING,StandardCharsets.UTF_8); + holder_a.put(REPLACE,argstring3); + break; + case 1: + break; + default: + break; } + operations.add(fixcore.rewrite(visited, cb, holder_a)); + nodesprocessed.add(visited); + return false; }); +// +// compilationUnit.accept(new ASTVisitor() { +// @Override +// public boolean visit(final ClassInstanceCreation visited) { +// if(nodesprocessed.contains(visited) || (visited.arguments().size()>1)) { +// return false; +// } +// ITypeBinding binding= visited.resolveTypeBinding(); +// if (OutputStreamWriter.class.getSimpleName().equals(binding.getName())) { +// operations.add(fixcore.rewrite(visited, cb, datah)); +// nodesprocessed.add(visited); +// return false; +// } +// return true; +// } +// }); } @Override public void rewrite(UseExplicitEncodingFixCore upp,final ClassInstanceCreation visited, final CompilationUnitRewrite cuRewrite, - TextEditGroup group,ChangeBehavior cb) { + TextEditGroup group,ChangeBehavior cb, ReferenceHolder data) { ASTRewrite rewrite= cuRewrite.getASTRewrite(); AST ast= cuRewrite.getRoot().getAST(); if (!JavaModelUtil.is50OrHigher(cuRewrite.getCu().getJavaProject())) { @@ -69,12 +102,17 @@ public void rewrite(UseExplicitEncodingFixCore upp,final ClassInstanceCreation v */ return; } - ASTNode callToCharsetDefaultCharset= computeCharsetASTNode(cuRewrite, cb, ast); + ASTNode callToCharsetDefaultCharset= computeCharsetASTNode(cuRewrite, cb, ast, (Charset) data.get(ENCODING)); /** * Add Charset.defaultCharset() as second (last) parameter */ ListRewrite listRewrite= rewrite.getListRewrite(visited, ClassInstanceCreation.ARGUMENTS_PROPERTY); - listRewrite.insertLast(callToCharsetDefaultCharset, group); +// listRewrite.insertLast(callToCharsetDefaultCharset, group); + if(data.get(ENCODING)!= null) { + listRewrite.replace((ASTNode) data.get(REPLACE), callToCharsetDefaultCharset, group); + } else { + listRewrite.insertLast(callToCharsetDefaultCharset, group); + } } @Override 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 4ff040ff..1687f4d7 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 @@ -17,6 +17,7 @@ import java.io.FileOutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; +import java.nio.charset.Charset; import java.util.Set; import org.eclipse.jdt.core.dom.AST; @@ -31,7 +32,9 @@ import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; import org.eclipse.jdt.internal.corext.util.JavaModelUtil; import org.eclipse.text.edits.TextEditGroup; +import org.sandbox.jdt.internal.common.ReferenceHolder; import org.sandbox.jdt.internal.corext.fix.UseExplicitEncodingFixCore; +import org.sandbox.jdt.internal.corext.fix.helper.AbstractExplicitEncoding.ChangeBehavior; /** * Change * @@ -53,7 +56,7 @@ public boolean visit(final ClassInstanceCreation visited) { } ITypeBinding binding= visited.resolveTypeBinding(); if (PrintWriter.class.getSimpleName().equals(binding.getName())) { - operations.add(fixcore.rewrite(visited, cb)); + operations.add(fixcore.rewrite(visited, cb, datah)); nodesprocessed.add(visited); return false; } @@ -64,7 +67,7 @@ public boolean visit(final ClassInstanceCreation visited) { @Override public void rewrite(UseExplicitEncodingFixCore upp,final ClassInstanceCreation visited, final CompilationUnitRewrite cuRewrite, - TextEditGroup group,ChangeBehavior cb) { + TextEditGroup group,ChangeBehavior cb, ReferenceHolder data) { ASTRewrite rewrite= cuRewrite.getASTRewrite(); AST ast= cuRewrite.getRoot().getAST(); if (!JavaModelUtil.is50OrHigher(cuRewrite.getCu().getJavaProject())) { @@ -73,7 +76,7 @@ public void rewrite(UseExplicitEncodingFixCore upp,final ClassInstanceCreation v */ return; } - ASTNode callToCharsetDefaultCharset= computeCharsetASTNode(cuRewrite, cb, ast); + ASTNode callToCharsetDefaultCharset= computeCharsetASTNode(cuRewrite, cb, ast, (Charset) data.get(ENCODING)); /** * new FileOutputStream() */ diff --git a/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/StringGetBytesExplicitEncoding.java b/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/StringGetBytesExplicitEncoding.java index 4510f5bf..db24869f 100644 --- a/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/StringGetBytesExplicitEncoding.java +++ b/sandbox_encoding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/StringGetBytesExplicitEncoding.java @@ -15,6 +15,7 @@ import static org.sandbox.jdt.internal.common.LibStandardNames.METHOD_GET_BYTES; +import java.nio.charset.Charset; import java.util.Set; import org.eclipse.jdt.core.dom.AST; @@ -29,7 +30,9 @@ import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; import org.eclipse.jdt.internal.corext.util.JavaModelUtil; import org.eclipse.text.edits.TextEditGroup; +import org.sandbox.jdt.internal.common.ReferenceHolder; import org.sandbox.jdt.internal.corext.fix.UseExplicitEncodingFixCore; +import org.sandbox.jdt.internal.corext.fix.helper.AbstractExplicitEncoding.ChangeBehavior; /** * Find: String.getBytes() * @@ -48,7 +51,7 @@ public boolean visit(final MethodInvocation visited) { } if (ASTNodes.usesGivenSignature(visited, String.class.getCanonicalName(), METHOD_GET_BYTES)) { - operations.add(fixcore.rewrite(visited, cb)); + operations.add(fixcore.rewrite(visited, cb, datah)); nodesprocessed.add(visited); return false; } @@ -59,7 +62,7 @@ public boolean visit(final MethodInvocation visited) { @Override public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visited, final CompilationUnitRewrite cuRewrite, - TextEditGroup group,ChangeBehavior cb) { + TextEditGroup group,ChangeBehavior cb, ReferenceHolder data) { ASTRewrite rewrite= cuRewrite.getASTRewrite(); AST ast= cuRewrite.getRoot().getAST(); if (!JavaModelUtil.is50OrHigher(cuRewrite.getCu().getJavaProject())) { @@ -68,7 +71,7 @@ public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visite */ return; } - ASTNode callToCharsetDefaultCharset= computeCharsetASTNode(cuRewrite, cb, ast); + ASTNode callToCharsetDefaultCharset= computeCharsetASTNode(cuRewrite, cb, ast, (Charset) data.get(ENCODING)); ListRewrite listRewrite= rewrite.getListRewrite(visited, MethodInvocation.ARGUMENTS_PROPERTY); listRewrite.insertLast(callToCharsetDefaultCharset, group); } diff --git a/sandbox_encoding_quickfix_test/src/org/sandbox/jdt/ui/tests/quickfix/Java9CleanUpTest.java b/sandbox_encoding_quickfix_test/src/org/sandbox/jdt/ui/tests/quickfix/Java9CleanUpTest.java index 3599e303..143479a4 100644 --- a/sandbox_encoding_quickfix_test/src/org/sandbox/jdt/ui/tests/quickfix/Java9CleanUpTest.java +++ b/sandbox_encoding_quickfix_test/src/org/sandbox/jdt/ui/tests/quickfix/Java9CleanUpTest.java @@ -378,7 +378,82 @@ void method(String filename) { } } } - """); + """), + 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(); + 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, "UTF-8"); + } 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(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(""), 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), StandardCharsets.UTF_8); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + } + } + """); ExplicitEncodingPatterns(String given, String expected) { this.given=given;