Skip to content

Commit

Permalink
Add part for Channels support
Browse files Browse the repository at this point in the history
  • Loading branch information
carstenartur committed Aug 25, 2024
1 parent 4da9adb commit 6f87d22
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ public class LibStandardNames {
*
*/
public static final String METHOD_FOR_NAME= "forName"; //$NON-NLS-1$
/**
*
*/
public static final String METHOD_NEW_READER= "newReader"; //$NON-NLS-1$
/**
*
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.sandbox.jdt.internal.corext.fix.helper.AbstractExplicitEncoding;
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.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;
Expand All @@ -39,7 +41,8 @@

public enum UseExplicitEncodingFixCore {

CHARSET(new StringGetBytesExplicitEncoding()),
CHARSET(new CharsetForNameExplicitEncoding()),
CHANNELS(new ChannelsNewReaderExplicitEncoding()),
STRING_GETBYTES(new StringGetBytesExplicitEncoding()),
INPUTSTREAMREADER(new InputStreamReaderExplicitEncoding()),
OUTPUTSTREAMWRITER(new OutputStreamWriterExplicitEncoding()),
Expand Down
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_READER;

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.newReader(ch,"UTF-8")
*
* Rewrite: Channels.newReader(ch,StandardCharsets.UTF_8)
*
*/
public class ChannelsNewReaderExplicitEncoding 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_READER, 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 (!"UTF-8".equals(argstring3.getLiteralValue())) { //$NON-NLS-1$
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 "Charset s=\"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$
"byte[] bytes= s.getBytes();\n"; //$NON-NLS-1$
}
}

0 comments on commit 6f87d22

Please sign in to comment.