Skip to content

Commit

Permalink
remove UnsupportedCharsetException and Add Java22
Browse files Browse the repository at this point in the history
  • Loading branch information
carstenartur committed Nov 8, 2024
1 parent 95149bc commit 427cd92
Show file tree
Hide file tree
Showing 41 changed files with 1,633 additions and 427 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
*******************************************************************************/
package org.eclipse.jdt.internal.common;

import java.util.AbstractMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
import java.util.function.BiPredicate;
import java.util.function.Function;
Expand Down Expand Up @@ -1197,7 +1199,9 @@ public ASTProcessor<E, V, T> callMethodInvocationVisitor(
*/
public ASTProcessor<E, V, T> callMethodInvocationVisitor(String methodname,
BiPredicate<ASTNode, E> bs) {
nodetypelist.put(VisitorEnum.MethodInvocation, new NodeHolder(bs, null, methodname));
nodetypelist.put(VisitorEnum.MethodInvocation, new NodeHolder(bs, null, Map.ofEntries(
new AbstractMap.SimpleEntry<>(HelperVisitor.METHODNAME, methodname)
)));
return this;
}

Expand All @@ -1209,7 +1213,9 @@ public ASTProcessor<E, V, T> callMethodInvocationVisitor(String methodname,
*/
public ASTProcessor<E, V, T> callMethodInvocationVisitor(String methodname,
BiPredicate<ASTNode, E> bs, Function<ASTNode, ASTNode> navigate) {
nodetypelist.put(VisitorEnum.MethodInvocation, new NodeHolder(bs, navigate, methodname));
nodetypelist.put(VisitorEnum.MethodInvocation, new NodeHolder(bs, navigate, Map.ofEntries(
new AbstractMap.SimpleEntry<>(HelperVisitor.METHODNAME, methodname)
)));
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,9 @@ public BiPredicate<? extends ASTNode, E> addMethodInvocation(BiPredicate<MethodI
*/
public BiPredicate<? extends ASTNode, E> addMethodInvocation(String methodname,
BiPredicate<MethodInvocation, E> bs) {
this.predicatedata.put(VisitorEnum.MethodInvocation, methodname);
this.predicatedata.put(VisitorEnum.MethodInvocation, Map.ofEntries(
new AbstractMap.SimpleEntry<>(METHODNAME, methodname)
));
return predicatemap.put(VisitorEnum.MethodInvocation, bs);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,27 @@
import java.util.function.BiPredicate;

import org.eclipse.jdt.core.dom.*;

import org.eclipse.jdt.internal.corext.dom.ASTNodes;

/**
*
* @author chammer
*
* @param <E>
* @param <V>
* @param <T>
* @param <E> - type that extends HelpVisitorProvider that provides {@code HelperVisitor<V, T>}
* @param <V> - type that HelperVisitor uses as map key type
* @param <T> - type that HelperVisitor uses as map value type
* @since 1.15
*/
@SuppressWarnings({ "unchecked", "restriction" })
@SuppressWarnings({ "unchecked" })
public class LambdaASTVisitor<E extends HelperVisitorProvider<V,T,E>, V, T> extends ASTVisitor {
/**
*
*/
private final HelperVisitor<E,V,T> helperVisitor;

/**
* @param helperVisitor
* @param helperVisitor - HelperVisitor
*/
LambdaASTVisitor(HelperVisitor<E,V,T> helperVisitor) {
super(false);
Expand Down Expand Up @@ -520,7 +521,7 @@ public boolean visit(MethodInvocation node) {
}
Class<?> typeof=(Class<?>) map.get(HelperVisitor.TYPEOF);
String[] parameterTypesQualifiedNames=(String[]) map.get(HelperVisitor.PARAMTYPENAMES);

if(typeof!=null) {
if(parameterTypesQualifiedNames==null) {
if (ASTNodes.usesGivenSignature(node, typeof.getCanonicalName(), data)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,5 +191,5 @@ StringBufferToStringBuilderCleanUp_description=Convert StringBuffer to StringBui
StringConcatToTextBlockCleanUp_description=Convert String concatenation to Text Block
StringConcatToTextBlockStringBuffer_description=Convert String/StringBuffer/StringBuilder concatenation to Text Block
StringBuilderForLocalVarsOnlyCleanUp_description=Convert StringBuffer to StringBuilder for local variables
ExplicitEncodingCleanUp_description=Fix reliance on default encoding ''{0}'' using {1}
ExplicitEncodingCleanUp_description=Set explicit encoding or default encoding where applicable on methods ''{0}'' using {1}
ExplicitEncodingCleanUpFix_refactor=Use explicit encoding
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ public ICleanUpFix createFix(final CleanUpContext context) throws CoreException
}

private ChangeBehavior computeRefactorDeepth() {
ChangeBehavior cb=ChangeBehavior.KEEP;
ChangeBehavior cb=ChangeBehavior.KEEP_BEHAVIOR;
if(isEnabled(EXPLICITENCODING_KEEP_BEHAVIOR)) {
cb=ChangeBehavior.KEEP;
cb=ChangeBehavior.KEEP_BEHAVIOR;
}
if(isEnabled(EXPLICITENCODING_INSERT_UTF8)) {
cb=ChangeBehavior.USE_UTF8;
cb=ChangeBehavior.ENFORCE_UTF8;
}
if(isEnabled(EXPLICITENCODING_AGGREGATE_TO_UTF8)) {
cb=ChangeBehavior.USE_UTF8_AGGREGATE;
cb=ChangeBehavior.ENFORCE_UTF8_AGGREGATE;
}
return cb;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,15 @@ public enum UseExplicitEncodingFixCore {
}

public String getPreview(boolean i, ChangeBehavior cb) {
return explicitencoding.getPreview(i,cb);
long countother= explicitencoding.getPreview(!i, cb).lines().count();
StringBuilder preview= new StringBuilder(explicitencoding.getPreview(i,cb));
long countnow= preview.toString().lines().count();
if(countnow<countother) {
for (long ii=0;ii<countother-countnow;ii++) {
preview.append(System.lineSeparator());
}
}
return preview.toString();
}
/**
* Compute set of CompilationUnitRewriteOperation to refactor supported situations using default encoding to make use of explicit calls
Expand Down Expand Up @@ -112,4 +120,9 @@ public SourceRange computeSourceRange(final ASTNode nodeWithComment) {
return super.computeSourceRange(nodeWithComment);
}
};

@Override
public String toString() {
return explicitencoding.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,29 @@

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.text.edits.TextEditGroup;

import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
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;
import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.core.dom.UnionType;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
import org.eclipse.jdt.core.dom.rewrite.ListRewrite;

import org.eclipse.jdt.internal.common.ReferenceHolder;
import org.eclipse.jdt.internal.corext.dom.ASTNodeFactory;
Expand All @@ -51,7 +61,7 @@ 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, USE_UTF8, USE_UTF8_AGGREGATE}
public enum ChangeBehavior {KEEP_BEHAVIOR, ENFORCE_UTF8, ENFORCE_UTF8_AGGREGATE}

static class Nodedata {
public boolean replace;
Expand All @@ -72,19 +82,19 @@ public abstract void rewrite(UseExplicitEncodingFixCore useExplicitEncodingFixCo
protected static Expression computeCharsetASTNode(final CompilationUnitRewrite cuRewrite, AST ast, ChangeBehavior cb, String charset) {
Expression callToCharsetDefaultCharset=null;
switch(cb) {
case KEEP:
case KEEP_BEHAVIOR:
if(charset!=null) {
callToCharsetDefaultCharset= addCharsetUTF8(cuRewrite, ast,charset);
} else {
// needs Java 1.5
callToCharsetDefaultCharset= addCharsetComputation(cuRewrite, ast);
}
break;
case USE_UTF8_AGGREGATE:
case ENFORCE_UTF8_AGGREGATE:
/**
* @TODO not implemented
*/
case USE_UTF8:
case ENFORCE_UTF8:
// needs Java 1.7
callToCharsetDefaultCharset= addCharsetUTF8(cuRewrite, ast,charset);
break;
Expand All @@ -95,13 +105,13 @@ protected static Expression computeCharsetASTNode(final CompilationUnitRewrite c
protected static String computeCharsetforPreview(ChangeBehavior cb) {
String insert=""; //$NON-NLS-1$
switch(cb) {
case KEEP:
case KEEP_BEHAVIOR:
insert="Charset.defaultCharset()"; //$NON-NLS-1$
break;
case USE_UTF8_AGGREGATE:
case ENFORCE_UTF8_AGGREGATE:
// insert="charset_constant"; //$NON-NLS-1$
//$FALL-THROUGH$
case USE_UTF8:
case ENFORCE_UTF8:
insert="StandardCharsets.UTF_8"; //$NON-NLS-1$
break;
}
Expand Down Expand Up @@ -173,7 +183,6 @@ protected static MethodInvocation addCharsetStringComputation(final CompilationU
return secondCall;
}


/**
* Adds an import to the class. This method should be used for every class reference added to
* the generated code.
Expand All @@ -189,6 +198,61 @@ protected static Name addImport(String typeName, final CompilationUnitRewrite cu
return ast.newName(importedName);
}


public abstract String getPreview(boolean afterRefactoring, ChangeBehavior cb);

protected void removeUnsupportedEncodingException(final ASTNode visited, TextEditGroup group, ASTRewrite rewrite, ImportRewrite importRewriter) {
importRewriter.removeImport("java.io.UnsupportedEncodingException"); //$NON-NLS-1$

ASTNode parent = visited.getParent();
while (parent != null && !(parent instanceof MethodDeclaration) && !(parent instanceof TryStatement)) {
parent = parent.getParent();
}

if (parent instanceof MethodDeclaration) {
MethodDeclaration method = (MethodDeclaration) parent;
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$
throwsRewrite.remove(exceptionType, group);
}
}
} else if (parent instanceof TryStatement) {
TryStatement tryStatement = (TryStatement) parent;

List<CatchClause> catchClauses = tryStatement.catchClauses();
for (CatchClause catchClause : catchClauses) {
SingleVariableDeclaration exception = catchClause.getException();
Type exceptionType = exception.getType();

if (exceptionType instanceof UnionType) {
UnionType unionType = (UnionType) exceptionType;
ListRewrite unionRewrite = rewrite.getListRewrite(unionType, UnionType.TYPES_PROPERTY);

List<Type> types = unionType.types();
types.stream()
.filter(type -> type.toString().equals("UnsupportedEncodingException")) //$NON-NLS-1$
.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$
rewrite.remove(catchClause, group);
}
}

if (tryStatement.catchClauses().isEmpty() && tryStatement.getFinally() == null) {
Block tryBlock = tryStatement.getBody();

if (tryStatement.resources().isEmpty() && tryBlock.statements().isEmpty()) {
rewrite.remove(tryStatement, group);
} else if (tryStatement.resources().isEmpty()) {
rewrite.replace(tryStatement, tryBlock, group);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
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.ImportRewrite;
import org.eclipse.jdt.core.dom.rewrite.ListRewrite;

import org.eclipse.jdt.internal.common.HelperVisitor;
Expand Down Expand Up @@ -109,30 +110,33 @@ public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visite
TextEditGroup group,ChangeBehavior cb, ReferenceHolder<ASTNode, Object> data) {
ASTRewrite rewrite= cuRewrite.getASTRewrite();
AST ast= cuRewrite.getRoot().getAST();
ASTNode callToCharsetDefaultCharset= computeCharsetASTNode(cuRewrite, ast, cb, ((Nodedata) data.get(visited)).encoding);
ImportRewrite importRewriter= cuRewrite.getImportRewrite();
Nodedata nodedata= (Nodedata) data.get(visited);
ASTNode callToCharsetDefaultCharset= computeCharsetASTNode(cuRewrite, ast, cb, nodedata.encoding);
/**
* Add Charset.defaultCharset().displayName() as second (last) parameter of "toString()" call
* Add Charset.defaultCharset() as second (last) parameter
*/
ListRewrite listRewrite= rewrite.getListRewrite(visited, MethodInvocation.ARGUMENTS_PROPERTY);
if(((Nodedata)(data.get(visited))).encoding!= null) {
listRewrite.replace(((Nodedata) data.get(visited)).visited, callToCharsetDefaultCharset, group);
if(nodedata.replace) {
listRewrite.replace(nodedata.visited, callToCharsetDefaultCharset, group);
} else {
listRewrite.insertLast(callToCharsetDefaultCharset, group);
}
removeUnsupportedEncodingException(visited, group, rewrite, importRewriter);
}

@Override
public String getPreview(boolean afterRefactoring,ChangeBehavior cb) {
String insert=""; //$NON-NLS-1$
switch(cb) {
case KEEP:
case KEEP_BEHAVIOR:
insert="Charset.defaultCharset().displayName()"; //$NON-NLS-1$
break;
case USE_UTF8_AGGREGATE:
case ENFORCE_UTF8_AGGREGATE:
// insert="charset_constant"; //$NON-NLS-1$
//$FALL-THROUGH$
case USE_UTF8:
case ENFORCE_UTF8:
insert="StandardCharsets.UTF_8.displayName()"; //$NON-NLS-1$
break;
}
Expand All @@ -153,4 +157,9 @@ public String getPreview(boolean afterRefactoring,ChangeBehavior cb) {
}
"""; //$NON-NLS-1$
}

@Override
public String toString() {
return "ba.toString()"; //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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.ImportRewrite;
import org.eclipse.jdt.core.dom.rewrite.ListRewrite;

import org.eclipse.jdt.internal.common.HelperVisitor;
Expand Down Expand Up @@ -86,25 +87,31 @@ public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visite
TextEditGroup group,ChangeBehavior cb, ReferenceHolder<ASTNode, Object> data) {
ASTRewrite rewrite= cuRewrite.getASTRewrite();
AST ast= cuRewrite.getRoot().getAST();
ASTNode callToCharsetDefaultCharset= computeCharsetASTNode(cuRewrite, ast, cb, ((Nodedata) data.get(visited)).encoding);
ImportRewrite importRewriter= cuRewrite.getImportRewrite();
Nodedata nodedata= (Nodedata) data.get(visited);
ASTNode callToCharsetDefaultCharset= computeCharsetASTNode(cuRewrite, ast, cb, nodedata.encoding);
/**
* Add Charset.defaultCharset() as second (last) parameter
*/
ListRewrite listRewrite= rewrite.getListRewrite(visited, MethodInvocation.ARGUMENTS_PROPERTY);
if(((Nodedata)(data.get(visited))).encoding!= null) {
listRewrite.replace(((Nodedata) data.get(visited)).visited, callToCharsetDefaultCharset, group);
if(nodedata.replace) {
listRewrite.replace(nodedata.visited, callToCharsetDefaultCharset, group);
} else {
listRewrite.insertLast(callToCharsetDefaultCharset, group);
}
removeUnsupportedEncodingException(visited, group, rewrite, importRewriter);
}

@Override
public String getPreview(boolean afterRefactoring,ChangeBehavior cb) {
if(afterRefactoring) {
return "Reader r=Channels.newReader(ch,StandardCharsets.UTF_8);\n"+ //$NON-NLS-1$
""; //$NON-NLS-1$
return "Reader r=Channels.newReader(ch,StandardCharsets.UTF_8);\n"; //$NON-NLS-1$
}
return "Reader r=Channels.newReader(ch,\"UTF-8\");\n"+ //$NON-NLS-1$
""; //$NON-NLS-1$
return "Reader r=Channels.newReader(ch,\"UTF-8\");\n"; //$NON-NLS-1$
}

@Override
public String toString() {
return "Channels.newReader(ch,StandardCharsets.UTF_8)"; //$NON-NLS-1$
}
}
Loading

0 comments on commit 427cd92

Please sign in to comment.