Skip to content

Commit

Permalink
add first part of channelsNewWriter and PrintStream
Browse files Browse the repository at this point in the history
  • Loading branch information
carstenartur committed Aug 25, 2024
1 parent 6f87d22 commit 88cfe82
Show file tree
Hide file tree
Showing 12 changed files with 277 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ public class LibStandardNames {
*
*/
public static final String METHOD_NEW_READER= "newReader"; //$NON-NLS-1$
/**
*
*/
public static final String METHOD_NEW_WRITER= "newWriter"; //$NON-NLS-1$
/**
*
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,29 @@
import org.sandbox.jdt.internal.corext.fix.helper.AbstractExplicitEncoding.ChangeBehavior;
import org.sandbox.jdt.internal.corext.fix.helper.ByteArrayOutputStreamExplicitEncoding;
import org.sandbox.jdt.internal.corext.fix.helper.ChannelsNewReaderExplicitEncoding;
import org.sandbox.jdt.internal.corext.fix.helper.ChannelsNewWriterExplicitEncoding;
import org.sandbox.jdt.internal.corext.fix.helper.CharsetForNameExplicitEncoding;
import org.sandbox.jdt.internal.corext.fix.helper.FileReaderExplicitEncoding;
import org.sandbox.jdt.internal.corext.fix.helper.FileWriterExplicitEncoding;
import org.sandbox.jdt.internal.corext.fix.helper.InputStreamReaderExplicitEncoding;
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.StringGetBytesExplicitEncoding;
import org.sandbox.jdt.internal.ui.fix.MultiFixMessages;

public enum UseExplicitEncodingFixCore {

CHARSET(new CharsetForNameExplicitEncoding()),
CHANNELS(new ChannelsNewReaderExplicitEncoding()),
CHANNELSNEWREADER(new ChannelsNewReaderExplicitEncoding()),
CHANNELSNEWWRITER(new ChannelsNewWriterExplicitEncoding()),
STRING_GETBYTES(new StringGetBytesExplicitEncoding()),
INPUTSTREAMREADER(new InputStreamReaderExplicitEncoding()),
OUTPUTSTREAMWRITER(new OutputStreamWriterExplicitEncoding()),
FILEREADER(new FileReaderExplicitEncoding()),
FILEWRITER(new FileWriterExplicitEncoding()),
PRINTWRITER(new PrintWriterExplicitEncoding()),
PRINTSTREAM(new PrintStreamExplicitEncoding()),
BYTEARRAYOUTPUTSTREAM(new ByteArrayOutputStreamExplicitEncoding());

AbstractExplicitEncoding<ASTNode> explicitencoding;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
* @param <T> Type found in Visitor
*/
public abstract class AbstractExplicitEncoding<T extends ASTNode> {
static Set<String> encodings= Set.of("UTF-8","UTF-16","UTF-16BE","UTF-16LE","ISO-8859-1","US-ASCII"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
public enum ChangeBehavior {KEEP, USE_UTF8, USE_UTF8_AGGREGATE}
ReferenceHolder<String, Object> datah= new ReferenceHolder<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private boolean processFoundNode(UseExplicitEncodingFixCore fixcore,
return false;
}
StringLiteral argstring3= (StringLiteral) arguments.get(0);
if (!"UTF-8".equals(argstring3.getLiteralValue())) { //$NON-NLS-1$
if (!encodings.contains(argstring3.getLiteralValue())) {
return false;
}
holder.put(ENCODING,StandardCharsets.UTF_8);
Expand Down Expand Up @@ -103,10 +103,10 @@ public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visite
@Override
public String getPreview(boolean afterRefactoring,ChangeBehavior cb) {
if(afterRefactoring) {
return "Charset s=\"StandardCharsets.UTF_8\";\n"+ //$NON-NLS-1$
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$
}
return "Charset s=\"Charset.forName(\"UTF-8\")\";\n"+ //$NON-NLS-1$
return "Reader r=\"Channels.newReader(ch,\"UTF-8\")\";\n"+ //$NON-NLS-1$
"byte[] bytes= s.getBytes();\n"; //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*******************************************************************************
* 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 static org.sandbox.jdt.internal.common.LibStandardNames.METHOD_NEW_WRITER;

import java.nio.channels.Channels;
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.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.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;
/**
* Find: Channels.newWriter(ch,"UTF-8")
*
* Rewrite: Channels.newWriter(ch,StandardCharsets.UTF_8)
*
*/
public class ChannelsNewWriterExplicitEncoding extends AbstractExplicitEncoding<MethodInvocation> {

@Override
public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set<CompilationUnitRewriteOperation> operations, Set<ASTNode> nodesprocessed,ChangeBehavior cb) {
HelperVisitor.callMethodInvocationVisitor(Channels.class, METHOD_NEW_WRITER, compilationUnit, datah, nodesprocessed, (visited, holder) -> processFoundNode(fixcore, operations, nodesprocessed, cb, visited, holder));
}

private boolean processFoundNode(UseExplicitEncodingFixCore fixcore,
Set<CompilationUnitRewriteOperation> operations, Set<ASTNode> nodesprocessed, ChangeBehavior cb,
MethodInvocation visited, ReferenceHolder<String, 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)) {
return false;
}
StringLiteral argstring3= (StringLiteral) arguments.get(0);
if (!encodings.contains(argstring3.getLiteralValue())) {
return false;
}
holder.put(ENCODING,StandardCharsets.UTF_8);
holder.put(REPLACE,argstring3);
break;
case 0:
break;
default:
return false;
}
operations.add(fixcore.rewrite(visited, cb, datah));
nodesprocessed.add(visited);
return false;
}

@Override
public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visited, final CompilationUnitRewrite cuRewrite,
TextEditGroup group,ChangeBehavior cb, ReferenceHolder<String, Object> data) {
ASTRewrite rewrite= cuRewrite.getASTRewrite();
AST ast= cuRewrite.getRoot().getAST();
if (!JavaModelUtil.is50OrHigher(cuRewrite.getCu().getJavaProject())) {
/**
* For Java 1.4 and older just do nothing
*/
return;
}
ASTNode callToCharsetDefaultCharset= computeCharsetASTNode(cuRewrite, cb, ast, (Charset) data.get(ENCODING));
// ListRewrite listRewrite= rewrite.getListRewrite(visited, MethodInvocation.ARGUMENTS_PROPERTY);
// listRewrite.insertLast(callToCharsetDefaultCharset, group);

ListRewrite listRewrite= rewrite.getListRewrite(visited, MethodInvocation.ARGUMENTS_PROPERTY);
if(data.get(ENCODING)!= null) {
listRewrite.replace((ASTNode) data.get(REPLACE), callToCharsetDefaultCharset, group);
} else {
listRewrite.insertLast(callToCharsetDefaultCharset, group);
}
}

@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(\"UTF-8\")\";\n"+ //$NON-NLS-1$
"byte[] bytes= s.getBytes();\n"; //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private boolean processFoundNode(UseExplicitEncodingFixCore fixcore,
return false;
}
StringLiteral argstring3= (StringLiteral) arguments.get(0);
if (!"UTF-8".equals(argstring3.getLiteralValue())) { //$NON-NLS-1$
if (!encodings.contains(argstring3.getLiteralValue())) {
return false;
}
holder.put(ENCODING,StandardCharsets.UTF_8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore,
return false;
}
StringLiteral argstring3= (StringLiteral) arguments.get(1);
if (!"UTF-8".equals(argstring3.getLiteralValue())) { //$NON-NLS-1$
if (!encodings.contains(argstring3.getLiteralValue())) {
return false;
}
holder.put(ENCODING,StandardCharsets.UTF_8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore,
return false;
}
StringLiteral argstring3= (StringLiteral) arguments.get(1);
if (!"UTF-8".equals(argstring3.getLiteralValue())) { //$NON-NLS-1$
if (!encodings.contains(argstring3.getLiteralValue())) {
return false;
}
holder.put(ENCODING,StandardCharsets.UTF_8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore,
return false;
}
StringLiteral argstring3= (StringLiteral) arguments.get(1);
if (!"UTF-8".equals(argstring3.getLiteralValue())) { //$NON-NLS-1$
if (!encodings.contains(argstring3.getLiteralValue())) {
return false;
}
holder.put(ENCODING,StandardCharsets.UTF_8);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*******************************************************************************
* 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.FileReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
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.ASTVisitor;
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;
/**
* Change
*
* Find: Stream fw=new PrintStream("file.txt", "UTF-8")
*
* Rewrite: Stream fw=new PrintStream("file.txt", StandardCharsets.UTF_8)
*
* Find: Stream fw=new PrintStream(new File("file.txt"), "UTF-8")
*
* Rewrite: Stream fw=new PrintStream(new File("file.txt"), StandardCharsets.UTF_8)
*
*/
public class PrintStreamExplicitEncoding extends AbstractExplicitEncoding<ClassInstanceCreation> {

@Override
public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set<CompilationUnitRewriteOperation> operations, Set<ASTNode> nodesprocessed,ChangeBehavior cb) {
HelperVisitor.callClassInstanceCreationVisitor(PrintStream.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<String, Object> holder) {
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 (!encodings.contains(argstring3.getLiteralValue())) {
return false;
}
holder.put(ENCODING,StandardCharsets.UTF_8);
holder.put(REPLACE,argstring3);
break;
default:
return false;
}
operations.add(fixcore.rewrite(visited, cb, holder));
nodesprocessed.add(visited);
return false;
}

@Override
public void rewrite(UseExplicitEncodingFixCore upp,final ClassInstanceCreation visited, final CompilationUnitRewrite cuRewrite,
TextEditGroup group,ChangeBehavior cb, ReferenceHolder<String, Object> data) {
ASTRewrite rewrite= cuRewrite.getASTRewrite();
AST ast= cuRewrite.getRoot().getAST();
if (!JavaModelUtil.is50OrHigher(cuRewrite.getCu().getJavaProject())) {
/**
* For Java 1.4 and older just do nothing
*/
return;
}
ASTNode callToCharsetDefaultCharset= computeCharsetASTNode(cuRewrite, cb, ast, (Charset) data.get(ENCODING));
/**
* new FileOutputStream(<filename>)
*/
ClassInstanceCreation fosclassInstance= ast.newClassInstanceCreation();
fosclassInstance.setType(ast.newSimpleType(addImport(FileOutputStream.class.getCanonicalName(), cuRewrite, ast)));
fosclassInstance.arguments().add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression((ASTNode) visited.arguments().get(0))));
/**
* new OutputStreamWriter(new FileOutputStream(<filename>))
*/
ClassInstanceCreation oswclassInstance= ast.newClassInstanceCreation();
oswclassInstance.setType(ast.newSimpleType(addImport(OutputStreamWriter.class.getCanonicalName(), cuRewrite, ast)));
oswclassInstance.arguments().add(fosclassInstance);
oswclassInstance.arguments().add(callToCharsetDefaultCharset);
/**
* new BufferedWriter(new OutputStreamWriter(new FileOutputStream(<filename>)))
*/
ClassInstanceCreation bwclassInstance= ast.newClassInstanceCreation();
bwclassInstance.setType(ast.newSimpleType(addImport(BufferedWriter.class.getCanonicalName(), cuRewrite, ast)));
bwclassInstance.arguments().add(oswclassInstance);

ASTNodes.replaceButKeepComment(rewrite, visited, bwclassInstance, group);
}

@Override
public String getPreview(boolean afterRefactoring,ChangeBehavior cb) {
if(afterRefactoring) {
return "Stream w=new PrintStream(\"out.txt\","+computeCharsetforPreview(cb)+"));\n"+ //$NON-NLS-1$ //$NON-NLS-2$
"Stream w=new PrintStream(\"out.txt\",StandardCharsets.UTF_8));\n"+ //$NON-NLS-1$
"Stream w=new PrintStream(new File(\"out.txt\"),StandardCharsets.UTF_8));\n"; //$NON-NLS-1$
}
return "Stream w=new PrintStream(\"out.txt\");\n"+ //$NON-NLS-1$
"Stream w=new PrintStream(\"out.txt\",\"UTF-8\");\n"+ //$NON-NLS-1$
"Stream w=new PrintStream(new File(\"out.txt\"),\"UTF-8\");\n"; //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private boolean processFoundNode(UseExplicitEncodingFixCore fixcore,
return false;
}
StringLiteral argstring3= (StringLiteral) arguments.get(0);
if (!"UTF-8".equals(argstring3.getLiteralValue())) { //$NON-NLS-1$
if (!encodings.contains(argstring3.getLiteralValue())) {
return false;
}
holder.put(ENCODING,StandardCharsets.UTF_8);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.sandbox.jdt.ui.tests.quickfix;
import java.io.UnsupportedEncodingException;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
public class E2 {
void bla(Throwable e) throws UnsupportedEncodingException {
IStatus status = new Status(IStatus.WARNING, "plugin id","important message",null);
byte[] bytes = "asdf".getBytes("Utf-8");
}
}

0 comments on commit 88cfe82

Please sign in to comment.