Skip to content

Commit

Permalink
some cleanup, unfortunate charset test now failing
Browse files Browse the repository at this point in the history
  • Loading branch information
carstenartur committed Sep 3, 2024
1 parent 9efdff8 commit 57d006e
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import org.sandbox.jdt.internal.common.ReferenceHolder;
import org.sandbox.jdt.internal.corext.fix.UseExplicitEncodingFixCore;
/**
* Java 10
*
* Change
*
* Find: Reader r=Channels.newReader(ch,"UTF-8")
Expand Down Expand Up @@ -65,7 +67,6 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore,
nd.visited=argstring3;
holder.put(visited,nd);
operations.add(fixcore.rewrite(visited, cb, holder));
nodesprocessed.add(visited);
return false;
}
return false;
Expand All @@ -76,9 +77,9 @@ public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visite
TextEditGroup group,ChangeBehavior cb, ReferenceHolder<ASTNode, Object> data) {
ASTRewrite rewrite= cuRewrite.getASTRewrite();
AST ast= cuRewrite.getRoot().getAST();
if (!JavaModelUtil.is50OrHigher(cuRewrite.getCu().getJavaProject())) {
if (!JavaModelUtil.is10OrHigher(cuRewrite.getCu().getJavaProject())) {
/**
* For Java 1.4 and older just do nothing
* For Java 9 and older just do nothing
*/
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import org.sandbox.jdt.internal.common.ReferenceHolder;
import org.sandbox.jdt.internal.corext.fix.UseExplicitEncodingFixCore;
/**
* Java 10
*
* Find: Channels.newWriter(ch,"UTF-8")
*
* Rewrite: Channels.newWriter(ch,StandardCharsets.UTF_8)
Expand Down Expand Up @@ -64,7 +66,6 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore,
nd.visited=argstring3;
holder.put(visited,nd);
operations.add(fixcore.rewrite(visited, cb, holder));
nodesprocessed.add(visited);
return false;
}
return false;
Expand All @@ -75,9 +76,9 @@ public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visite
TextEditGroup group,ChangeBehavior cb, ReferenceHolder<ASTNode, Object> data) {
ASTRewrite rewrite= cuRewrite.getASTRewrite();
AST ast= cuRewrite.getRoot().getAST();
if (!JavaModelUtil.is50OrHigher(cuRewrite.getCu().getJavaProject())) {
if (!JavaModelUtil.is10OrHigher(cuRewrite.getCu().getJavaProject())) {
/**
* For Java 1.4 and older just do nothing
* For Java 9 and older just do nothing
*/
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import org.sandbox.jdt.internal.common.ReferenceHolder;
import org.sandbox.jdt.internal.corext.fix.UseExplicitEncodingFixCore;
/**
* Java 18
*
* Find: Charset.forName("UTF-8")
*
* Rewrite: StandardCharsets.UTF_8
Expand All @@ -46,6 +48,12 @@ public class CharsetForNameExplicitEncoding extends AbstractExplicitEncoding<Met

@Override
public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set<CompilationUnitRewriteOperation> operations, Set<ASTNode> nodesprocessed,ChangeBehavior cb) {
if (!JavaModelUtil.is18OrHigher(compilationUnit.getJavaElement().getJavaProject())) {
/**
* For Java 17 and older just do nothing
*/
return;
}
ReferenceHolder<ASTNode, Object> datah= new ReferenceHolder<>();
HelperVisitor.callMethodInvocationVisitor(Charset.class, METHOD_FOR_NAME, compilationUnit, datah, nodesprocessed, (visited, holder) -> processFoundNode(fixcore, operations, nodesprocessed, cb, visited, holder));
}
Expand All @@ -63,7 +71,6 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore,
}
holder.put(visited,encodingmap.get(argstring3.getLiteralValue().toUpperCase()));
operations.add(fixcore.rewrite(visited, cb, holder));
nodesprocessed.add(visited);
return false;
}

Expand All @@ -72,9 +79,9 @@ public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visite
TextEditGroup group,ChangeBehavior cb, ReferenceHolder<ASTNode, Object> data) {
ASTRewrite rewrite= cuRewrite.getASTRewrite();
AST ast= cuRewrite.getRoot().getAST();
if (!JavaModelUtil.is50OrHigher(cuRewrite.getCu().getJavaProject())) {
if (!JavaModelUtil.is18OrHigher(cuRewrite.getCu().getJavaProject())) {
/**
* For Java 1.4 and older just do nothing
* For Java 17 and older just do nothing
*/
return;
}
Expand All @@ -86,9 +93,9 @@ public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visite
public String getPreview(boolean afterRefactoring,ChangeBehavior cb) {
if(afterRefactoring) {
return "Charset s=\"StandardCharsets.UTF_8\";\n"+ //$NON-NLS-1$
"byte[] bytes= s.getBytes("+computeCharsetforPreview(cb)+");\n"; //$NON-NLS-1$ //$NON-NLS-2$
""; //$NON-NLS-1$
}
return "Charset s=\"Charset.forName(\"UTF-8\")\";\n"+ //$NON-NLS-1$
"byte[] bytes= s.getBytes();\n"; //$NON-NLS-1$
""; //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@
/**
* Change
*
* Reader is=new FileReader("file.txt")
* Reader is=new InputStreamReader(new FileInputStream("file.txt"),Charset.defaultCharset());
* Find: Reader is=new FileReader("file.txt")
*
* Rewrite: Reader is=new InputStreamReader(new FileInputStream("file.txt"),Charset.defaultCharset());
*
* Charset.defaultCharset() is available since Java 1.5
*
Expand All @@ -54,9 +55,6 @@ 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();
if(nodesprocessed.contains(visited) || (arguments.size()>2)) {
return false;
}
switch (arguments.size()) {
case 1:
break;
Expand All @@ -74,7 +72,6 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore,
return false;
}
operations.add(fixcore.rewrite(visited, cb, holder));
nodesprocessed.add(visited);
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,31 +45,15 @@ public class FileWriterExplicitEncoding extends AbstractExplicitEncoding<ClassIn
@Override
public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set<CompilationUnitRewriteOperation> operations, Set<ASTNode> nodesprocessed,ChangeBehavior cb) {
ReferenceHolder<ASTNode, Object> datah= new ReferenceHolder<>();
// compilationUnit.accept(new ASTVisitor() {
// @Override
// public boolean visit(final ClassInstanceCreation visited) {
// if(nodesprocessed.contains(visited)) {
// return false;
// }
// ITypeBinding binding= visited.resolveTypeBinding();
// if (FileWriter.class.getSimpleName().equals(binding.getName())) {
// operations.add(fixcore.rewrite(visited, cb, datah));
// nodesprocessed.add(visited);
// return false;
// }
// return true;
// }
// });
HelperVisitor.callClassInstanceCreationVisitor(FileWriter.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) {
operations.add(fixcore.rewrite(visited, cb, holder));
nodesprocessed.add(visited);
return false;
}
private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, Set<CompilationUnitRewriteOperation> operations,
Set<ASTNode> nodesprocessed, ChangeBehavior cb, ClassInstanceCreation visited,
ReferenceHolder<ASTNode, Object> holder) {
operations.add(fixcore.rewrite(visited, cb, holder));
return false;
}

@Override
public void rewrite(UseExplicitEncodingFixCore upp,final ClassInstanceCreation visited, final CompilationUnitRewrite cuRewrite,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ 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();
if(nodesprocessed.contains(visited) || (arguments.size()>2)) {
return false;
}
switch (arguments.size()) {
case 2:
if(!(arguments.get(1) instanceof StringLiteral)) {
Expand All @@ -77,7 +74,6 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore,
break;
}
operations.add(fixcore.rewrite(visited, cb, holder));
nodesprocessed.add(visited);
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ 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();
if(nodesprocessed.contains(visited) || (arguments.size()>2)) {
return false;
}
switch (arguments.size()) {
case 2:
if(!(arguments.get(1) instanceof StringLiteral)) {
Expand All @@ -82,7 +79,6 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore,
break;
}
operations.add(fixcore.rewrite(visited, cb, holder));
nodesprocessed.add(visited);
return false;
}

Expand All @@ -102,7 +98,6 @@ public void rewrite(UseExplicitEncodingFixCore upp,final ClassInstanceCreation v
* Add Charset.defaultCharset() as second (last) parameter
*/
ListRewrite listRewrite= rewrite.getListRewrite(visited, ClassInstanceCreation.ARGUMENTS_PROPERTY);
// listRewrite.insertLast(callToCharsetDefaultCharset, group);
if(((Nodedata)(data.get(visited))).encoding!= null) {
listRewrite.replace(((Nodedata) data.get(visited)).visited, callToCharsetDefaultCharset, group);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, Set<
Set<ASTNode> nodesprocessed, ChangeBehavior cb, ClassInstanceCreation visited,
ReferenceHolder<ASTNode, Object> holder) {
List<ASTNode> arguments= visited.arguments();
if(nodesprocessed.contains(visited) || (arguments.size()>2)) {
return false;
}
switch (arguments.size()) {
case 1:
break;
Expand All @@ -78,7 +75,6 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, Set<
return false;
}
operations.add(fixcore.rewrite(visited, cb, holder));
nodesprocessed.add(visited);
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,18 @@
import org.sandbox.jdt.internal.common.ReferenceHolder;
import org.sandbox.jdt.internal.corext.fix.UseExplicitEncodingFixCore;
/**
*
* Java 10
*
* Change
*
* Writer fw=new PrintWriter("file.txt")
* Writer fw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("file.txt"),defaultCharset)));
* Find: Writer fw=new PrintWriter("file.txt")
*
* Rewrite: Writer fw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("file.txt"),defaultCharset)));
*
* Find: Writer fw=new PrintWriter(new File("file.txt"))
*
* Rewrite: Writer fw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("file.txt"),defaultCharset)));
*
* Charset.defaultCharset() is available since Java 1.5
*
Expand All @@ -53,7 +61,6 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, Set<
Set<ASTNode> nodesprocessed, ChangeBehavior cb, ClassInstanceCreation visited,
ReferenceHolder<ASTNode, Object> holder) {
operations.add(fixcore.rewrite(visited, cb, holder));
nodesprocessed.add(visited);
return false;
}

Expand All @@ -62,9 +69,9 @@ public void rewrite(UseExplicitEncodingFixCore upp,final ClassInstanceCreation v
TextEditGroup group,ChangeBehavior cb, ReferenceHolder<ASTNode, Object> data) {
ASTRewrite rewrite= cuRewrite.getASTRewrite();
AST ast= cuRewrite.getRoot().getAST();
if (!JavaModelUtil.is50OrHigher(cuRewrite.getCu().getJavaProject())) {
if (!JavaModelUtil.is10OrHigher(cuRewrite.getCu().getJavaProject())) {
/**
* For Java 1.4 and older just do nothing
* For Java 9 and older just do nothing
*/
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,32 +44,57 @@ void method(String filename) {
}
""",

"""
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.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;
Expand Down
5 changes: 4 additions & 1 deletion sandbox_web/src/main/webapp/index.jsp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<h2>This is the Sandbox Client Update Repository.</h2>
</body>
</html>
</html>

0 comments on commit 57d006e

Please sign in to comment.