Skip to content

Commit

Permalink
add Scanner, String, URLDecoder, URLEncoder
Browse files Browse the repository at this point in the history
  • Loading branch information
carstenartur committed Sep 5, 2024
1 parent e078357 commit a5d512f
Show file tree
Hide file tree
Showing 8 changed files with 514 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ public class LibStandardNames {
*
*/
public static final String METHOD_NEW_WRITER= "newWriter"; //$NON-NLS-1$
/**
*
*/
public static final String METHOD_DECODE= "decode"; //$NON-NLS-1$
/**
*
*/
public static final String METHOD_ENCODE= "encode"; //$NON-NLS-1$
/**
*
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@
import org.sandbox.jdt.internal.corext.fix.helper.OutputStreamWriterExplicitEncoding;
import org.sandbox.jdt.internal.corext.fix.helper.PrintStreamExplicitEncoding;
import org.sandbox.jdt.internal.corext.fix.helper.PrintWriterExplicitEncoding;
import org.sandbox.jdt.internal.corext.fix.helper.ScannerExplicitEncoding;
import org.sandbox.jdt.internal.corext.fix.helper.StringGetBytesExplicitEncoding;
import org.sandbox.jdt.internal.corext.fix.helper.URLDecoderDecodeExplicitEncoding;
import org.sandbox.jdt.internal.corext.fix.helper.URLEncoderEncodeExplicitEncoding;
import org.sandbox.jdt.internal.ui.fix.MultiFixMessages;

public enum UseExplicitEncodingFixCore {
Expand All @@ -55,7 +58,10 @@ public enum UseExplicitEncodingFixCore {
PRINTWRITER(new PrintWriterExplicitEncoding()),
PRINTSTREAM(new PrintStreamExplicitEncoding()),
BYTEARRAYOUTPUTSTREAM(new ByteArrayOutputStreamExplicitEncoding()),
FORMATTER(new FormatterExplicitEncoding());
FORMATTER(new FormatterExplicitEncoding()),
URLDECODER(new URLDecoderDecodeExplicitEncoding()),
URLENCODER(new URLEncoderEncodeExplicitEncoding()),
SCANNER(new ScannerExplicitEncoding());

AbstractExplicitEncoding<ASTNode> explicitencoding;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*******************************************************************************
* Copyright (c) 2021 Carsten Hammer.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Carsten Hammer
*******************************************************************************/
package org.sandbox.jdt.internal.corext.fix.helper;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.List;
import java.util.Scanner;
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.MethodInvocation;
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.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.Nodedata;
/**
*
* Java 10
*
* Change
*
* Find: new java.util.Scanner(new File("asdf"),"UTF-8")
*
* Rewrite: new java.util.Scanner(new File("asdf"),StandardCharsets.UTF_8);
*
* Find: new java.util.Scanner("asdf", "UTF-8")
*
* Rewrite: new java.util.Scanner("asdf", StandardCharsets.UTF_8)
*
* Find: new java.util.Scanner(java.io.OutputStream, "UTF-8")
*
* Rewrite: new java.util.Scanner(java.io.OutputStream, StandardCharsets.UTF_8)
*
*/
public class ScannerExplicitEncoding extends AbstractExplicitEncoding<ClassInstanceCreation> {

@Override
public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set<CompilationUnitRewriteOperation> operations, Set<ASTNode> nodesprocessed,ChangeBehavior cb) {
ReferenceHolder<ASTNode, Object> datah= new ReferenceHolder<>();
HelperVisitor.callClassInstanceCreationVisitor(Scanner.class, compilationUnit, datah, nodesprocessed, (visited, holder) -> processFoundNode(fixcore, operations, nodesprocessed, cb, visited, holder));
}

private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, Set<CompilationUnitRewriteOperation> operations,
Set<ASTNode> nodesprocessed, ChangeBehavior cb, ClassInstanceCreation visited,
ReferenceHolder<ASTNode, Object> holder) {
List<ASTNode> arguments= visited.arguments();
switch (arguments.size()) {
case 4:
if(!(arguments.get(3) instanceof StringLiteral)) {
return false;
}
StringLiteral argstring4= (StringLiteral) arguments.get(3);
if (!encodings.contains(argstring4.getLiteralValue().toUpperCase())) {
return false;
}
Nodedata nd=new Nodedata();
nd.encoding=encodingmap.get(argstring4.getLiteralValue().toUpperCase());
nd.replace=true;
nd.visited=argstring4;
holder.put(visited,nd);
break;
case 2:
if(!(arguments.get(1) instanceof StringLiteral)) {
return false;
}
StringLiteral argstring3= (StringLiteral) arguments.get(1);
if (!encodings.contains(argstring3.getLiteralValue().toUpperCase())) {
return false;
}
Nodedata nd2=new Nodedata();
nd2.encoding=encodingmap.get(argstring3.getLiteralValue().toUpperCase());
nd2.replace=true;
nd2.visited=argstring3;
holder.put(visited,nd2);
break;
case 1:
default:
break;
}
operations.add(fixcore.rewrite(visited, cb, holder));
return false;
}

@Override
public void rewrite(UseExplicitEncodingFixCore upp,final ClassInstanceCreation visited, final CompilationUnitRewrite cuRewrite,
TextEditGroup group,ChangeBehavior cb, ReferenceHolder<ASTNode, Object> 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
*/
ListRewrite listRewrite= rewrite.getListRewrite(visited, MethodInvocation.ARGUMENTS_PROPERTY);
if(((Nodedata)(data.get(visited))).encoding!= null) {
listRewrite.replace(((Nodedata) data.get(visited)).visited, callToCharsetDefaultCharset, group);
} else {
listRewrite.insertLast(callToCharsetDefaultCharset, group);
}
}

@Override
public String getPreview(boolean afterRefactoring,ChangeBehavior cb) {
if(afterRefactoring) {
return "new java.util.Scanner(\"asdf\","+computeCharsetforPreview(cb)+");\n"; //$NON-NLS-1$ //$NON-NLS-2$
}
return "new java.util.Scanner(\"asdf\", \"UTF-8\");\n"; //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*******************************************************************************
* Copyright (c) 2021 Carsten Hammer.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Carsten Hammer
*******************************************************************************/
package org.sandbox.jdt.internal.corext.fix.helper;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
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.MethodInvocation;
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.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.Nodedata;
/**
*
* Java 10
*
* Change
*
* Find: String s=new String(byte[],"UTF-8")
*
* Rewrite: String s=new String(byte[],StandardCharsets.UTF_8);
*
* Find: String s=new String(byte[],int, int, "UTF-8")
*
* Rewrite: String s=new String(byte[],int, int, StandardCharsets.UTF_8)
*
*/
public class StringExplicitEncoding extends AbstractExplicitEncoding<ClassInstanceCreation> {

@Override
public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set<CompilationUnitRewriteOperation> operations, Set<ASTNode> nodesprocessed,ChangeBehavior cb) {
ReferenceHolder<ASTNode, Object> datah= new ReferenceHolder<>();
HelperVisitor.callClassInstanceCreationVisitor(String.class, compilationUnit, datah, nodesprocessed, (visited, holder) -> processFoundNode(fixcore, operations, nodesprocessed, cb, visited, holder));
}

private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, Set<CompilationUnitRewriteOperation> operations,
Set<ASTNode> nodesprocessed, ChangeBehavior cb, ClassInstanceCreation visited,
ReferenceHolder<ASTNode, Object> holder) {
List<ASTNode> arguments= visited.arguments();
switch (arguments.size()) {
case 4:
if(!(arguments.get(3) instanceof StringLiteral)) {
return false;
}
StringLiteral argstring4= (StringLiteral) arguments.get(3);
if (!encodings.contains(argstring4.getLiteralValue().toUpperCase())) {
return false;
}
Nodedata nd=new Nodedata();
nd.encoding=encodingmap.get(argstring4.getLiteralValue().toUpperCase());
nd.replace=true;
nd.visited=argstring4;
holder.put(visited,nd);
break;
case 2:
if(!(arguments.get(1) instanceof StringLiteral)) {
return false;
}
StringLiteral argstring3= (StringLiteral) arguments.get(1);
if (!encodings.contains(argstring3.getLiteralValue().toUpperCase())) {
return false;
}
Nodedata nd2=new Nodedata();
nd2.encoding=encodingmap.get(argstring3.getLiteralValue().toUpperCase());
nd2.replace=true;
nd2.visited=argstring3;
holder.put(visited,nd2);
break;
case 1:
default:
break;
}
operations.add(fixcore.rewrite(visited, cb, holder));
return false;
}

@Override
public void rewrite(UseExplicitEncodingFixCore upp,final ClassInstanceCreation visited, final CompilationUnitRewrite cuRewrite,
TextEditGroup group,ChangeBehavior cb, ReferenceHolder<ASTNode, Object> 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
*/
ListRewrite listRewrite= rewrite.getListRewrite(visited, MethodInvocation.ARGUMENTS_PROPERTY);
if(((Nodedata)(data.get(visited))).encoding!= null) {
listRewrite.replace(((Nodedata) data.get(visited)).visited, callToCharsetDefaultCharset, group);
} else {
listRewrite.insertLast(callToCharsetDefaultCharset, group);
}
}

@Override
public String getPreview(boolean afterRefactoring,ChangeBehavior cb) {
if(afterRefactoring) {
return "String s=new String(byte[],"+computeCharsetforPreview(cb)+");\n"; //$NON-NLS-1$ //$NON-NLS-2$
}
return "String s=new String(byte[],\"UTF-8\");\n"; //$NON-NLS-1$
}
}
Loading

0 comments on commit a5d512f

Please sign in to comment.