-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Scanner, String, URLDecoder, URLEncoder
- Loading branch information
1 parent
e078357
commit a5d512f
Showing
8 changed files
with
514 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
...ding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/ScannerExplicitEncoding.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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$ | ||
} | ||
} |
134 changes: 134 additions & 0 deletions
134
...oding_quickfix/src/org/sandbox/jdt/internal/corext/fix/helper/StringExplicitEncoding.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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$ | ||
} | ||
} |
Oops, something went wrong.