Skip to content

Commit

Permalink
Support replacing given String literal parameter setting encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
carstenartur committed Aug 4, 2024
1 parent ad81e3b commit 9a50c80
Show file tree
Hide file tree
Showing 10 changed files with 245 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -68,13 +69,13 @@ public void findOperations(final CompilationUnit compilationUnit,final Set<Compi
explicitencoding.find(this, compilationUnit, operations, nodesprocessed, cb);
}

public CompilationUnitRewriteOperation rewrite(final ASTNode visited, ChangeBehavior cb) {
public CompilationUnitRewriteOperation rewrite(final ASTNode visited, ChangeBehavior cb, ReferenceHolder data) {
return new CompilationUnitRewriteOperation() {
@Override
public void rewriteAST(final CompilationUnitRewrite cuRewrite, final LinkedProposalModelCore linkedModel) throws CoreException {
TextEditGroup group= createTextEditGroup(Messages.format(MultiFixMessages.ExplicitEncodingCleanUp_description,new Object[] {UseExplicitEncodingFixCore.this.toString(), cb.toString()}), cuRewrite);
cuRewrite.getASTRewrite().setTargetSourceRangeComputer(computer);
explicitencoding.rewrite(UseExplicitEncodingFixCore.this, visited, cuRewrite, group, cb);
explicitencoding.rewrite(UseExplicitEncodingFixCore.this, visited, cuRewrite, group, cb, data);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,35 @@
import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite;
import org.eclipse.text.edits.TextEditGroup;
import org.sandbox.jdt.internal.common.LibStandardNames;
import org.sandbox.jdt.internal.common.ReferenceHolder;
import org.sandbox.jdt.internal.corext.fix.UseExplicitEncodingFixCore;

/**
* @param <T> Type found in Visitor
*/
public abstract class AbstractExplicitEncoding<T extends ASTNode> {
public enum ChangeBehavior {KEEP, USE_UTF8, USE_UTF8_AGGREGATE}
ReferenceHolder<String, Object> 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<CompilationUnitRewriteOperation> operations, Set<ASTNode> nodesprocessed, ChangeBehavior cb);


public abstract void rewrite(UseExplicitEncodingFixCore useExplicitEncodingFixCore, T visited, CompilationUnitRewrite cuRewrite,
TextEditGroup group, ChangeBehavior cb);
TextEditGroup group, ChangeBehavior cb, ReferenceHolder<String, Object> 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:
/**
Expand Down Expand Up @@ -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()
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
Expand All @@ -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<String, Object> data) {
ASTRewrite rewrite= cuRewrite.getASTRewrite();
AST ast= cuRewrite.getRoot().getAST();
if (!JavaModelUtil.is50OrHigher(cuRewrite.getCu().getJavaProject())) {
Expand All @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
*
Expand All @@ -44,26 +51,51 @@ public class FileReaderExplicitEncoding extends AbstractExplicitEncoding<ClassIn

@Override
public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set<CompilationUnitRewriteOperation> operations, Set<ASTNode> 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<ASTNode> 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<String, Object> data) {
ASTRewrite rewrite= cuRewrite.getASTRewrite();
AST ast= cuRewrite.getRoot().getAST();
if (!JavaModelUtil.is50OrHigher(cuRewrite.getCu().getJavaProject())) {
Expand All @@ -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(<filename>)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
*
Expand All @@ -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;
}
Expand All @@ -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<String, Object> data) {
ASTRewrite rewrite= cuRewrite.getASTRewrite();
AST ast= cuRewrite.getRoot().getAST();
if (!JavaModelUtil.is50OrHigher(cuRewrite.getCu().getJavaProject())) {
Expand All @@ -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(<filename>)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,20 +42,35 @@ public class InputStreamReaderExplicitEncoding extends AbstractExplicitEncoding<

@Override
public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set<CompilationUnitRewriteOperation> operations, Set<ASTNode> nodesprocessed,ChangeBehavior cb) {
ReferenceHolder<ASTNode, ClassInstanceCreation> 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<ASTNode> 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;
});
}

@Override
public void rewrite(UseExplicitEncodingFixCore upp,final ClassInstanceCreation visited, final CompilationUnitRewrite cuRewrite,
TextEditGroup group,ChangeBehavior cb) {
TextEditGroup group,ChangeBehavior cb, ReferenceHolder<String, Object> data) {
ASTRewrite rewrite= cuRewrite.getASTRewrite();
AST ast= cuRewrite.getRoot().getAST();
if (!JavaModelUtil.is50OrHigher(cuRewrite.getCu().getJavaProject())) {
Expand All @@ -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
Expand Down
Loading

0 comments on commit 9a50c80

Please sign in to comment.