Skip to content

Commit

Permalink
implement Channels.newReader
Browse files Browse the repository at this point in the history
  • Loading branch information
carstenartur committed Sep 1, 2024
1 parent 351dc9b commit 3156032
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -535,11 +535,14 @@ public boolean visit(MethodInvocation node) {
// }
// }

if(parameterTypesQualifiedNames!=null) {
if (ASTNodes.usesGivenSignature(node, typeof.getCanonicalName(), data, parameterTypesQualifiedNames)) {
return ((BiPredicate<MethodInvocation, E>) (this.helperVisitor.predicatemap.get(VisitorEnum.MethodInvocation))).test(node, this.helperVisitor.dataholder);
}
}
if(parameterTypesQualifiedNames==null) {
if (ASTNodes.usesGivenSignature(node, typeof.getCanonicalName(), data)) {
return ((BiPredicate<MethodInvocation, E>) (this.helperVisitor.predicatemap.get(VisitorEnum.MethodInvocation))).test(node, this.helperVisitor.dataholder);
}
} else
if (ASTNodes.usesGivenSignature(node, typeof.getCanonicalName(), data, parameterTypesQualifiedNames)) {
return ((BiPredicate<MethodInvocation, E>) (this.helperVisitor.predicatemap.get(VisitorEnum.MethodInvocation))).test(node, this.helperVisitor.dataholder);
}
}
}
return ((BiPredicate<MethodInvocation, E>) (this.helperVisitor.predicatemap.get(VisitorEnum.MethodInvocation))).test(node, this.helperVisitor.dataholder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
package org.sandbox.jdt.internal.corext.fix.helper;

import static org.sandbox.jdt.internal.common.LibStandardNames.METHOD_NEW_READER;

import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.util.List;
import java.util.Set;

Expand All @@ -26,6 +26,7 @@
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;
Expand All @@ -34,9 +35,11 @@
import org.sandbox.jdt.internal.common.ReferenceHolder;
import org.sandbox.jdt.internal.corext.fix.UseExplicitEncodingFixCore;
/**
* Find: Channels.newReader(ch,"UTF-8")
* Change
*
* Rewrite: Channels.newReader(ch,StandardCharsets.UTF_8)
* Find: Reader r=Channels.newReader(ch,"UTF-8")
*
* Rewrite: Reader r=Channels.newReader(ch,StandardCharsets.UTF_8)
*
*/
public class ChannelsNewReaderExplicitEncoding extends AbstractExplicitEncoding<MethodInvocation> {
Expand All @@ -51,29 +54,20 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore,
Set<CompilationUnitRewriteOperation> operations, Set<ASTNode> nodesprocessed, ChangeBehavior cb,
MethodInvocation visited, ReferenceHolder<ASTNode, Object> holder) {
List<ASTNode> arguments= visited.arguments();
if(nodesprocessed.contains(visited) || (arguments.size()>1)) {
return false;
}
switch (arguments.size()) {
case 1:
if(!(arguments.get(0) instanceof StringLiteral)) {
if (ASTNodes.usesGivenSignature(visited, Channels.class.getCanonicalName(), METHOD_NEW_READER, ReadableByteChannel.class.getCanonicalName(),String.class.getCanonicalName())) {
StringLiteral argstring3= (StringLiteral) arguments.get(1);
if (!encodings.contains(argstring3.getLiteralValue().toUpperCase())) {
return false;
}
StringLiteral argstring3= (StringLiteral) arguments.get(0);
if (!encodings.contains(argstring3.getLiteralValue())) {
return false;
}
holder.put(argstring3,encodingmap.get(argstring3.getLiteralValue()));
// holder.put(ENCODING,StandardCharsets.UTF_8);
// holder.put(REPLACE,argstring3);
break;
case 0:
break;
default:
Nodedata nd=new Nodedata();
nd.encoding=encodingmap.get(argstring3.getLiteralValue().toUpperCase());
nd.replace=true;
nd.visited=argstring3;
holder.put(visited,nd);
operations.add(fixcore.rewrite(visited, cb, holder));
nodesprocessed.add(visited);
return false;
}
operations.add(fixcore.rewrite(visited, cb, holder));
nodesprocessed.add(visited);
return false;
}

Expand All @@ -88,13 +82,13 @@ public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visite
*/
return;
}
ASTNode callToCharsetDefaultCharset= computeCharsetASTNode(cuRewrite, ast, cb, (String) data.get(visited));
// ListRewrite listRewrite= rewrite.getListRewrite(visited, MethodInvocation.ARGUMENTS_PROPERTY);
// listRewrite.insertLast(callToCharsetDefaultCharset, group);

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(data.get(ENCODING)!= null) {
listRewrite.replace((ASTNode) data.get(REPLACE), callToCharsetDefaultCharset, group);
if(((Nodedata)(data.get(visited))).encoding!= null) {
listRewrite.replace(((Nodedata) data.get(visited)).visited, callToCharsetDefaultCharset, group);
} else {
listRewrite.insertLast(callToCharsetDefaultCharset, group);
}
Expand All @@ -104,9 +98,9 @@ public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visite
public String getPreview(boolean afterRefactoring,ChangeBehavior cb) {
if(afterRefactoring) {
return "Reader r=\"Channels.newReader(ch,StandardCharsets.UTF_8)\";\n"+ //$NON-NLS-1$
"byte[] bytes= s.getBytes("+computeCharsetforPreview(cb)+");\n"; //$NON-NLS-1$ //$NON-NLS-2$
""; //$NON-NLS-1$
}
return "Reader r=\"Channels.newReader(ch,\"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 @@ -64,8 +64,6 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore,
return false;
}
holder.put(argstring3,encodingmap.get(argstring3.getLiteralValue()));
// holder.put(ENCODING,StandardCharsets.UTF_8);
// holder.put(REPLACE,argstring3);
break;
case 0:
break;
Expand Down Expand Up @@ -103,10 +101,10 @@ public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visite
@Override
public String getPreview(boolean afterRefactoring,ChangeBehavior cb) {
if(afterRefactoring) {
return "Writer w=\"Channels.newWriter(StandardCharsets.UTF_8)\";\n"+ //$NON-NLS-1$
"byte[] bytes= s.getBytes("+computeCharsetforPreview(cb)+");\n"; //$NON-NLS-1$ //$NON-NLS-2$
return "Writer w=\"Channels.newWriter(ch, StandardCharsets.UTF_8)\";\n"+ //$NON-NLS-1$
""; //$NON-NLS-1$
}
return "Writer w=\"Channels.newWriter(\"UTF-8\")\";\n"+ //$NON-NLS-1$
"byte[] bytes= s.getBytes();\n"; //$NON-NLS-1$
return "Writer w=\"Channels.newWriter(ch, \"UTF-8\")\";\n"+ //$NON-NLS-1$
""; //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,13 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore,
nd.replace=true;
nd.visited=argstring3;
holder.put(visited,nd);
// holder.put(ENCODING,StandardCharsets.UTF_8);
// holder.put(REPLACE,argstring3);
break;
case 1:
Nodedata nd2=new Nodedata();
nd2.encoding=null;
nd2.replace=false;
nd2.visited=visited;
holder.put(visited,nd2);
// holder.put(visited,encodingmap.get("UTF-8")); //$NON-NLS-1$
break;
default:
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, Set<
return false;
}
holder.put(argstring3,encodingmap.get(argstring3.getLiteralValue()));
// holder.put(ENCODING,StandardCharsets.UTF_8);
// holder.put(REPLACE,argstring3);
break;
default:
return false;
Expand Down
2 changes: 1 addition & 1 deletion sandbox_encoding_quickfix_test/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Bundle-SymbolicName: sandbox_encoding_quickfix_test;singleton:=true
Bundle-Vendor: sandbox
Bundle-Version: 1.1.0.qualifier
Fragment-Host: sandbox_encoding_quickfix
Bundle-RequiredExecutionEnvironment: JavaSE-21
Bundle-RequiredExecutionEnvironment: JavaSE-17
Require-Bundle: org.junit,
sandbox_test_commons
Bundle-ActivationPolicy: lazy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,49 @@ void method(String filename) {
}
}
"""),
CHANNELSNEWREADER("""
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.channels.ReadableByteChannel;
import java.nio.charset.StandardCharsets;
import java.nio.channels.Channels;
import java.io.FileNotFoundException;
public class E1 {
void method(String filename) {
ReadableByteChannel ch;
Reader r=Channels.newReader(ch,"UTF-8"); //$NON-NLS-1$
}
}
}
""",

"""
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.channels.ReadableByteChannel;
import java.nio.charset.StandardCharsets;
import java.nio.channels.Channels;
import java.io.FileNotFoundException;
public class E1 {
void method(String filename) {
ReadableByteChannel ch;
Reader r=Channels.newReader(ch,StandardCharsets.UTF_8); //$NON-NLS-1$
}
}
}
"""),
PRINTWRITER("""
package test1;
Expand Down

0 comments on commit 3156032

Please sign in to comment.