ByteArrayOutputStreamExplicitEncoding.java
/*******************************************************************************
* 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_TOSTRING;
import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Set;
import org.eclipse.text.edits.TextEditGroup;
import org.eclipse.core.runtime.CoreException;
import org.sandbox.jdt.internal.common.HelperVisitor;
import org.sandbox.jdt.internal.common.HelperVisitorFactory;
import org.sandbox.jdt.internal.corext.fix.UseExplicitEncodingFixCore;
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.sandbox.jdt.internal.common.ReferenceHolder;
import org.eclipse.jdt.internal.core.manipulation.JavaManipulationPlugin;
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;
/**
* Change from
*
* <pre>
* ByteArrayOutputStream ba= new ByteArrayOutputStream();
*
* String result= ba.toString();
* </pre>
*
* <pre>
* ByteArrayOutputStream ba= new ByteArrayOutputStream();
* try {
* String result= ba.toString(Charset.defaultCharset().displayName());
* } catch (UnsupportedEncodingException e1) {
* e1.printStackTrace();
* }
* </pre>
*
* since Java 10
*
* <pre>
* ByteArrayOutputStream ba= new ByteArrayOutputStream();
*
* String result= ba.toString(Charset.defaultCharset());
* </pre>
*
*/
public class ByteArrayOutputStreamExplicitEncoding extends AbstractExplicitEncoding<MethodInvocation> {
@Override
public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set<CompilationUnitRewriteOperation> operations, Set<ASTNode> nodesprocessed, ChangeBehavior cb) {
if (!JavaModelUtil.is10OrHigher(compilationUnit.getJavaElement().getJavaProject())) {
/**
* For Java 9 and older just do nothing
*/
return;
}
ReferenceHolder<ASTNode, Object> holder= new ReferenceHolder<>();
HelperVisitorFactory.forMethodCall(ByteArrayOutputStream.class, METHOD_TOSTRING)
.in(compilationUnit)
.excluding(nodesprocessed)
.processEach(holder, (visited, aholder) -> processFoundNode(fixcore, operations, cb, visited, aholder));
}
private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, Set<CompilationUnitRewriteOperation> operations,
ChangeBehavior cb, MethodInvocation visited,
ReferenceHolder<ASTNode, Object> holder) {
List<ASTNode> arguments= visited.arguments();
if (ASTNodes.usesGivenSignature(visited, ByteArrayOutputStream.class.getCanonicalName(), METHOD_TOSTRING, String.class.getCanonicalName())) {
if (!(arguments.get(0) instanceof StringLiteral)) {
return false;
}
StringLiteral argstring3= (StringLiteral) arguments.get(0);
if (!ENCODINGS.contains(argstring3.getLiteralValue())) {
return false;
}
NodeData nd= new NodeData(true, argstring3, ENCODING_MAP.get(argstring3.getLiteralValue()));
holder.put(visited, nd);
operations.add(fixcore.rewrite(visited, cb, holder));
return false;
}
if (ASTNodes.usesGivenSignature(visited, ByteArrayOutputStream.class.getCanonicalName(), METHOD_TOSTRING)) {
NodeData nd2= new NodeData(false, visited, null);
holder.put(visited, nd2);
operations.add(fixcore.rewrite(visited, cb, holder));
return false;
}
return false;
}
@Override
public void rewrite(UseExplicitEncodingFixCore upp, final MethodInvocation visited, final CompilationUnitRewrite cuRewrite,
TextEditGroup group, ChangeBehavior cb, ReferenceHolder<ASTNode, Object> data) {
ASTRewrite rewrite= cuRewrite.getASTRewrite();
AST ast= cuRewrite.getRoot().getAST();
NodeData nodedata= (NodeData) data.get(visited);
ASTNode callToCharsetDefaultCharset= cb.computeCharsetASTNode(cuRewrite, ast, nodedata.encoding(), getCharsetConstants());
/**
* Register encoding replacement BEFORE removing exception handling.
* removeUnsupportedEncodingException may call simplifyEmptyTryStatement
* which uses createMoveTarget to move statements out of the try block.
* replaceAndRemoveNLS fails silently on nodes that have already been
* marked as move targets, so the replacement must be registered first.
*/
ListRewrite listRewrite= rewrite.getListRewrite(visited, MethodInvocation.ARGUMENTS_PROPERTY);
boolean tryAlreadyUnwrapped= false;
if (nodedata.replace()) {
tryAlreadyUnwrapped= replaceArgumentAndRemoveNLS(rewrite, nodedata.visited(), callToCharsetDefaultCharset, group, cuRewrite);
} else {
listRewrite.insertLast(callToCharsetDefaultCharset, group);
}
if (!tryAlreadyUnwrapped) {
removeUnsupportedEncodingException(visited, group, rewrite, cuRewrite.getImportRemover());
}
}
@Override
public String getPreview(boolean afterRefactoring, ChangeBehavior cb) {
String insert= ""; //$NON-NLS-1$
switch (cb) {
case KEEP_BEHAVIOR:
insert= "Charset.defaultCharset().displayName()"; //$NON-NLS-1$
break;
case ENFORCE_UTF8_AGGREGATE:
// insert="charset_constant"; //$NON-NLS-1$
//$FALL-THROUGH$
case ENFORCE_UTF8:
insert= "StandardCharsets.UTF_8.displayName()"; //$NON-NLS-1$
break;
}
if (afterRefactoring) {
return "ByteArrayOutputStream ba=new ByteArrayOutputStream();\n" //$NON-NLS-1$
+ "try {\n" //$NON-NLS-1$
+ " String result=ba.toString(" + insert + ");\n" //$NON-NLS-1$ //$NON-NLS-2$
+ "} catch (UnsupportedEncodingException e1) {\n" //$NON-NLS-1$
+ " e1.printStackTrace();\n" //$NON-NLS-1$
+ "}\n"; //$NON-NLS-1$
}
return """
ByteArrayOutputStream ba=new ByteArrayOutputStream();
try {
String result=ba.toString();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
"""; //$NON-NLS-1$
}
@Override
public String toString() {
return "ba.toString()"; //$NON-NLS-1$
}
}