Skip to content

Commit

Permalink
inherit specific processing in enum
Browse files Browse the repository at this point in the history
  • Loading branch information
carstenartur committed Nov 9, 2024
1 parent 368db46 commit 60d2567
Show file tree
Hide file tree
Showing 21 changed files with 190 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore;
import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation;
import org.eclipse.jdt.internal.corext.fix.UseExplicitEncodingFixCore;
import org.eclipse.jdt.internal.corext.fix.helper.AbstractExplicitEncoding.ChangeBehavior;
import org.eclipse.jdt.internal.corext.fix.helper.ChangeBehavior;
import org.eclipse.jdt.internal.corext.util.Messages;

import org.eclipse.jdt.ui.cleanup.CleanUpContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation;
import org.eclipse.jdt.internal.corext.fix.helper.AbstractExplicitEncoding;
import org.eclipse.jdt.internal.corext.fix.helper.AbstractExplicitEncoding.ChangeBehavior;
import org.eclipse.jdt.internal.corext.fix.helper.ByteArrayOutputStreamExplicitEncoding;
import org.eclipse.jdt.internal.corext.fix.helper.ChangeBehavior;
import org.eclipse.jdt.internal.corext.fix.helper.ChannelsNewReaderExplicitEncoding;
import org.eclipse.jdt.internal.corext.fix.helper.ChannelsNewWriterExplicitEncoding;
import org.eclipse.jdt.internal.corext.fix.helper.CharsetForNameExplicitEncoding;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@
*******************************************************************************/
package org.eclipse.jdt.internal.corext.fix.helper;

import static org.eclipse.jdt.internal.corext.fix.LibStandardNames.METHOD_DEFAULT_CHARSET;
import static org.eclipse.jdt.internal.corext.fix.LibStandardNames.METHOD_DISPLAY_NAME;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand All @@ -29,10 +24,7 @@
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.CatchClause;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.FieldAccess;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Name;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
import org.eclipse.jdt.core.dom.TryStatement;
Expand All @@ -43,7 +35,6 @@
import org.eclipse.jdt.core.dom.rewrite.ListRewrite;

import org.eclipse.jdt.internal.common.ReferenceHolder;
import org.eclipse.jdt.internal.corext.dom.ASTNodeFactory;
import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation;
import org.eclipse.jdt.internal.corext.fix.UseExplicitEncodingFixCore;
import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite;
Expand All @@ -52,6 +43,7 @@
* @param <T> Type found in Visitor
*/
public abstract class AbstractExplicitEncoding<T extends ASTNode> {
private static final String UNSUPPORTED_ENCODING_EXCEPTION= "UnsupportedEncodingException"; //$NON-NLS-1$
static Map<String, String> encodingmap = Map.of(
"UTF-8", "UTF_8", //$NON-NLS-1$ //$NON-NLS-2$
"UTF-16", "UTF_16", //$NON-NLS-1$ //$NON-NLS-2$
Expand All @@ -61,128 +53,20 @@ public abstract class AbstractExplicitEncoding<T extends ASTNode> {
"US-ASCII", "US_ASCII" //$NON-NLS-1$ //$NON-NLS-2$
);
static Set<String> encodings=encodingmap.keySet();
public enum ChangeBehavior {KEEP_BEHAVIOR, ENFORCE_UTF8, ENFORCE_UTF8_AGGREGATE}

static class Nodedata {
public boolean replace;
public ASTNode visited;
public String encoding;
}


protected static final String ENCODING = "encoding"; //$NON-NLS-1$
protected static final String REPLACE = "replace"; //$NON-NLS-1$

public abstract void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set<CompilationUnitRewriteOperation> operations, Set<ASTNode> nodesprocessed, ChangeBehavior cb);


public abstract void rewrite(UseExplicitEncodingFixCore useExplicitEncodingFixCore, T visited, CompilationUnitRewrite cuRewrite,
TextEditGroup group, ChangeBehavior cb, ReferenceHolder<ASTNode, Object> data);

protected static Expression computeCharsetASTNode(final CompilationUnitRewrite cuRewrite, AST ast, ChangeBehavior cb, String charset) {
Expression callToCharsetDefaultCharset=null;
switch(cb) {
case KEEP_BEHAVIOR:
if(charset!=null) {
callToCharsetDefaultCharset= addCharsetUTF8(cuRewrite, ast,charset);
} else {
// needs Java 1.5
callToCharsetDefaultCharset= addCharsetComputation(cuRewrite, ast);
}
break;
case ENFORCE_UTF8_AGGREGATE:
/**
* @TODO not implemented
*/
case ENFORCE_UTF8:
// needs Java 1.7
callToCharsetDefaultCharset= addCharsetUTF8(cuRewrite, ast,charset);
break;
}
return callToCharsetDefaultCharset;
}

protected static String computeCharsetforPreview(ChangeBehavior cb) {
String insert=""; //$NON-NLS-1$
switch(cb) {
case KEEP_BEHAVIOR:
insert="Charset.defaultCharset()"; //$NON-NLS-1$
break;
case ENFORCE_UTF8_AGGREGATE:
// insert="charset_constant"; //$NON-NLS-1$
//$FALL-THROUGH$
case ENFORCE_UTF8:
insert="StandardCharsets.UTF_8"; //$NON-NLS-1$
break;
}
return insert;
}

/**
* Create access to StandardCharsets.UTF_8, needs Java 1.7 or newer
*
* @param cuRewrite CompilationUnitRewrite
* @param ast AST
* @param charset Charset as String
* @return FieldAccess that returns Charset for UTF_8
*/
protected static FieldAccess addCharsetUTF8(CompilationUnitRewrite cuRewrite, AST ast, String charset) {
/**
* Add import java.nio.charset.StandardCharsets - available since Java 1.7
*/
ImportRewrite importRewrite= cuRewrite.getImportRewrite();
importRewrite.addImport(StandardCharsets.class.getCanonicalName());
/**
* Add field access to StandardCharsets.UTF_8
*/
FieldAccess fieldaccess= ast.newFieldAccess();
fieldaccess.setExpression(ASTNodeFactory.newName(ast, StandardCharsets.class.getSimpleName()));

fieldaccess.setName(ast.newSimpleName(charset));
return fieldaccess;
}
/**
* Create call to Charset.defaultCharset(), needs Java 1.5 or newer
*
* @param cuRewrite CompilationUnitRewrite
* @param ast AST
* @return MethodInvocation that returns Charset for platform encoding
*/
protected static MethodInvocation addCharsetComputation(final CompilationUnitRewrite cuRewrite, AST ast) {
/**
* Add import java.nio.charset.Charset
*/
ImportRewrite importRewrite= cuRewrite.getImportRewrite();
importRewrite.addImport(Charset.class.getCanonicalName());
/**
* Add call to Charset.defaultCharset() - this is available since Java 1.5
*/
MethodInvocation firstCall= ast.newMethodInvocation();
firstCall.setExpression(ASTNodeFactory.newName(ast, Charset.class.getSimpleName()));
firstCall.setName(ast.newSimpleName(METHOD_DEFAULT_CHARSET));
return firstCall;
}

/**
* Create call to Charset.defaultCharset().displayName(), needs Java 1.5 or newer
*
* @param cuRewrite CompilationUnitRewrite
* @param ast AST
* @param cb ChangeBehavior
* @param charset Charset as String
* @return MethodInvocation that returns String
*/
protected static MethodInvocation addCharsetStringComputation(final CompilationUnitRewrite cuRewrite, AST ast, ChangeBehavior cb, String charset) {
Expression callToCharsetDefaultCharset= computeCharsetASTNode(cuRewrite, ast, cb, charset);
/**
* Add second call to Charset.defaultCharset().displayName()
*/
MethodInvocation secondCall= ast.newMethodInvocation();
secondCall.setExpression(callToCharsetDefaultCharset);
secondCall.setName(ast.newSimpleName(METHOD_DISPLAY_NAME));
return secondCall;
}

/**
* Adds an import to the class. This method should be used for every class reference added to
* the generated code.
Expand Down Expand Up @@ -213,7 +97,7 @@ protected void removeUnsupportedEncodingException(final ASTNode visited, TextEdi
ListRewrite throwsRewrite = rewrite.getListRewrite(method, MethodDeclaration.THROWN_EXCEPTION_TYPES_PROPERTY);
List<Type> thrownExceptions = method.thrownExceptionTypes();
for (Type exceptionType : thrownExceptions) {
if (exceptionType.toString().equals("UnsupportedEncodingException")) { //$NON-NLS-1$
if (exceptionType.toString().equals(UNSUPPORTED_ENCODING_EXCEPTION)) {
throwsRewrite.remove(exceptionType, group);
}
}
Expand All @@ -231,15 +115,15 @@ protected void removeUnsupportedEncodingException(final ASTNode visited, TextEdi

List<Type> types = unionType.types();
types.stream()
.filter(type -> type.toString().equals("UnsupportedEncodingException")) //$NON-NLS-1$
.filter(type -> type.toString().equals(UNSUPPORTED_ENCODING_EXCEPTION))
.forEach(type -> unionRewrite.remove(type, group));

if (types.size() == 1) {
rewrite.replace(unionType, types.get(0), group);
} else if (types.isEmpty()) {
rewrite.remove(catchClause, group);
}
} else if (exceptionType.toString().equals("UnsupportedEncodingException")) { //$NON-NLS-1$
} else if (exceptionType.toString().equals(UNSUPPORTED_ENCODING_EXCEPTION)) {
rewrite.remove(catchClause, group);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visite
AST ast= cuRewrite.getRoot().getAST();
ImportRewrite importRewriter= cuRewrite.getImportRewrite();
Nodedata nodedata= (Nodedata) data.get(visited);
ASTNode callToCharsetDefaultCharset= computeCharsetASTNode(cuRewrite, ast, cb, nodedata.encoding);
ASTNode callToCharsetDefaultCharset= cb.computeCharsetASTNode(cuRewrite, ast, nodedata.encoding);
/**
* Add Charset.defaultCharset().displayName() as second (last) parameter of "toString()" call
* Add Charset.defaultCharset() as second (last) parameter
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*******************************************************************************
* 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.eclipse.jdt.internal.corext.fix.helper;

import static org.eclipse.jdt.internal.corext.fix.LibStandardNames.METHOD_DEFAULT_CHARSET;
import static org.eclipse.jdt.internal.corext.fix.LibStandardNames.METHOD_DISPLAY_NAME;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.FieldAccess;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;

import org.eclipse.jdt.internal.corext.dom.ASTNodeFactory;
import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite;

public enum ChangeBehavior {
KEEP_BEHAVIOR() {
@Override
protected Expression computeCharsetASTNode(final CompilationUnitRewrite cuRewrite, AST ast, String charset) {
Expression callToCharsetDefaultCharset= null;

if (charset != null) {
callToCharsetDefaultCharset= addCharsetUTF8(cuRewrite, ast, charset);
} else {
// needs Java 1.5
callToCharsetDefaultCharset= addCharsetComputation(cuRewrite, ast);
}

return callToCharsetDefaultCharset;
}

@Override
protected String computeCharsetforPreview() {
String insert= ""; //$NON-NLS-1$
insert= "Charset.defaultCharset()"; //$NON-NLS-1$
return insert;
}
},
ENFORCE_UTF8() {
@Override
protected Expression computeCharsetASTNode(final CompilationUnitRewrite cuRewrite, AST ast, String charset) {
Expression callToCharsetDefaultCharset= null;
// needs Java 1.7
callToCharsetDefaultCharset= addCharsetUTF8(cuRewrite, ast, charset);
return callToCharsetDefaultCharset;
}

@Override
protected String computeCharsetforPreview() {
String insert= ""; //$NON-NLS-1$
insert= "StandardCharsets.UTF_8"; //$NON-NLS-1$
return insert;
}
},
ENFORCE_UTF8_AGGREGATE() {
@Override
protected Expression computeCharsetASTNode(final CompilationUnitRewrite cuRewrite, AST ast, String charset) {
Expression callToCharsetDefaultCharset= null;
/**
* @TODO not implemented
*/
return callToCharsetDefaultCharset;
}

@Override
protected String computeCharsetforPreview() {
String insert= ""; //$NON-NLS-1$
// insert="charset_constant"; //$NON-NLS-1$
return insert;
}
};


abstract protected Expression computeCharsetASTNode(final CompilationUnitRewrite cuRewrite, AST ast, String charset);

abstract protected String computeCharsetforPreview();

/**
* Create access to StandardCharsets.UTF_8, needs Java 1.7 or newer
*
* @param cuRewrite CompilationUnitRewrite
* @param ast AST
* @param charset Charset as String
* @return FieldAccess that returns Charset for UTF_8
*/
protected static FieldAccess addCharsetUTF8(CompilationUnitRewrite cuRewrite, AST ast, String charset) {
/**
* Add import java.nio.charset.StandardCharsets - available since Java 1.7
*/
ImportRewrite importRewrite= cuRewrite.getImportRewrite();
importRewrite.addImport(StandardCharsets.class.getCanonicalName());
/**
* Add field access to StandardCharsets.UTF_8
*/
FieldAccess fieldaccess= ast.newFieldAccess();
fieldaccess.setExpression(ASTNodeFactory.newName(ast, StandardCharsets.class.getSimpleName()));

fieldaccess.setName(ast.newSimpleName(charset));
return fieldaccess;
}

/**
* Create call to Charset.defaultCharset(), needs Java 1.5 or newer
*
* @param cuRewrite CompilationUnitRewrite
* @param ast AST
* @return MethodInvocation that returns Charset for platform encoding
*/
protected static MethodInvocation addCharsetComputation(final CompilationUnitRewrite cuRewrite, AST ast) {
/**
* Add import java.nio.charset.Charset
*/
ImportRewrite importRewrite= cuRewrite.getImportRewrite();
importRewrite.addImport(Charset.class.getCanonicalName());
/**
* Add call to Charset.defaultCharset() - this is available since Java 1.5
*/
MethodInvocation firstCall= ast.newMethodInvocation();
firstCall.setExpression(ASTNodeFactory.newName(ast, Charset.class.getSimpleName()));
firstCall.setName(ast.newSimpleName(METHOD_DEFAULT_CHARSET));
return firstCall;
}

/**
* Create call to Charset.defaultCharset().displayName(), needs Java 1.5 or newer
*
* @param cuRewrite CompilationUnitRewrite
* @param ast AST
* @param cb ChangeBehavior
* @param charset Charset as String
* @return MethodInvocation that returns String
*/
protected MethodInvocation addCharsetStringComputation(final CompilationUnitRewrite cuRewrite, AST ast, ChangeBehavior cb, String charset) {
Expression callToCharsetDefaultCharset= computeCharsetASTNode(cuRewrite, ast, charset);
/**
* Add second call to Charset.defaultCharset().displayName()
*/
MethodInvocation secondCall= ast.newMethodInvocation();
secondCall.setExpression(callToCharsetDefaultCharset);
secondCall.setName(ast.newSimpleName(METHOD_DISPLAY_NAME));
return secondCall;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visite
AST ast= cuRewrite.getRoot().getAST();
ImportRewrite importRewriter= cuRewrite.getImportRewrite();
Nodedata nodedata= (Nodedata) data.get(visited);
ASTNode callToCharsetDefaultCharset= computeCharsetASTNode(cuRewrite, ast, cb, nodedata.encoding);
ASTNode callToCharsetDefaultCharset= cb.computeCharsetASTNode(cuRewrite, ast, nodedata.encoding);
/**
* Add Charset.defaultCharset() as second (last) parameter
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visite
AST ast= cuRewrite.getRoot().getAST();
ImportRewrite importRewriter= cuRewrite.getImportRewrite();
Nodedata nodedata= (Nodedata) data.get(visited);
ASTNode callToCharsetDefaultCharset= computeCharsetASTNode(cuRewrite, ast, cb, nodedata.encoding);
ASTNode callToCharsetDefaultCharset= cb.computeCharsetASTNode(cuRewrite, ast, nodedata.encoding);
/**
* Add Charset.defaultCharset() as second (last) parameter
*/
Expand Down
Loading

0 comments on commit 60d2567

Please sign in to comment.