Skip to content

Commit

Permalink
add explicitencoding cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
carstenartur committed Sep 28, 2024
1 parent a00fd89 commit 8538f32
Show file tree
Hide file tree
Showing 66 changed files with 7,701 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*******************************************************************************/
package org.eclipse.jdt.internal.common;

import java.util.AbstractMap;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.LinkedHashMap;
Expand All @@ -34,6 +35,9 @@
*/
public class HelperVisitor<E extends HelperVisitorProvider<V, T, E>,V,T> {

public static final String TYPEOF = "typeof"; //$NON-NLS-1$
public static final String METHODNAME = "methodname"; //$NON-NLS-1$
public static final String PARAMTYPENAMES = "paramtypenames"; //$NON-NLS-1$
ASTVisitor astvisitor;

public E dataholder;
Expand Down Expand Up @@ -370,6 +374,20 @@ public BiPredicate<? extends ASTNode, E> addClassInstanceCreation(BiPredicate<Cl
return predicatemap.put(VisitorEnum.ClassInstanceCreation, bs);
}

/**
*
* @param typeof class to be instantiated
* @param bs - BiPredicate that can be assigned a lambda expression
* @return - previous BiPredicate registered
*/
public BiPredicate<? extends ASTNode, E> addClassInstanceCreation(Class<?> typeof, BiPredicate<ClassInstanceCreation, E> bs) {
Map<String, Object> map = Map.ofEntries(
new AbstractMap.SimpleEntry<>(TYPEOF, typeof)
);
predicatedata.put(VisitorEnum.ClassInstanceCreation, map);
return predicatemap.put(VisitorEnum.ClassInstanceCreation, bs);
}

/**
* Add BiPredicate to use for CompilationUnit visit
*
Expand Down Expand Up @@ -723,6 +741,24 @@ public BiPredicate<? extends ASTNode, E> addMethodInvocation(String methodname,
return predicatemap.put(VisitorEnum.MethodInvocation, bs);
}

/**
* Add BiPredicate to use for MethodInvocation visit where class and method name is specified
*
* @param typeof class whose method is called
* @param methodname name of the method that is called
* @param bs BiPredicate that can be assigned a lambda expression
* @return previous BiPredicate registered
*/
public BiPredicate<? extends ASTNode, E> addMethodInvocation(Class<?> typeof, String methodname,
BiPredicate<MethodInvocation, E> bs) {
Map<String, Object> map = Map.ofEntries(
new AbstractMap.SimpleEntry<>(METHODNAME, methodname),
new AbstractMap.SimpleEntry<>(TYPEOF, typeof)
);
predicatedata.put(VisitorEnum.MethodInvocation, map);
return predicatemap.put(VisitorEnum.MethodInvocation, bs);
}

/**
* Add BiPredicate to use for Modifier visit
*
Expand Down Expand Up @@ -1484,7 +1520,9 @@ public BiConsumer<? extends ASTNode, E> addMethodInvocation(BiConsumer<MethodInv
}

public BiConsumer<? extends ASTNode, E> addMethodInvocation(String methodname, BiConsumer<MethodInvocation, E> bc) {
this.consumerdata.put(VisitorEnum.MethodInvocation, methodname);
this.consumerdata.put(VisitorEnum.MethodInvocation, Map.ofEntries(
new AbstractMap.SimpleEntry<>(METHODNAME, methodname)
));
return consumermap.put(VisitorEnum.MethodInvocation, bc);
}

Expand Down Expand Up @@ -2180,24 +2218,51 @@ public void addMethodDeclaration(BiPredicate<MethodDeclaration, E> bs, BiConsume

/**
*
* @param bs - BiPredicate that can be assigned a lambda expression
* @param methodname Only visit MethodInvocation with this name
* @param bs - BiPredicate that is visited when a MethodInvocation is found
* @param bc - BiConsumer that is visited at the end after a MethodInvocation has been found
*/
public void addMethodInvocation(String methodname, BiPredicate<MethodInvocation, E> bs,
BiConsumer<MethodInvocation, E> bc) {
this.predicatedata.put(VisitorEnum.MethodInvocation, methodname);
predicatedata.put(VisitorEnum.MethodInvocation, Map.ofEntries(
new AbstractMap.SimpleEntry<>(METHODNAME, methodname)
));
predicatemap.put(VisitorEnum.MethodInvocation, bs);
consumerdata.put(VisitorEnum.MethodInvocation, Map.ofEntries(
new AbstractMap.SimpleEntry<>(METHODNAME, methodname)
));
consumermap.put(VisitorEnum.MethodInvocation, bc);
}

/**
* @param typeof Only visit MethodInvocation calling a method of this class
* @param methodname Only visit MethodInvocation with this name
* @param bs - BiPredicate that is visited when a MethodInvocation is found
* @param bc - BiConsumer that is visited at the end after a MethodInvocation has been found
*/
public void addMethodInvocation(Class<?> typeof, String methodname, BiPredicate<MethodInvocation, E> bs,
BiConsumer<MethodInvocation, E> bc) {
Map<String, Object> map = Map.ofEntries(
new AbstractMap.SimpleEntry<>(METHODNAME, methodname),
new AbstractMap.SimpleEntry<>(TYPEOF, typeof)
);
predicatedata.put(VisitorEnum.MethodInvocation, map);
predicatemap.put(VisitorEnum.MethodInvocation, bs);
consumerdata.put(VisitorEnum.MethodInvocation, map);
consumermap.put(VisitorEnum.MethodInvocation, bc);
}

/**
*
* @param bs - BiPredicate that can be assigned a lambda expression
* @param bc - BiConsumer that is visited at the end after a MethodInvocation has been found
*/
public void addMethodInvocation(BiPredicate<MethodInvocation, E> bs, BiConsumer<MethodInvocation, E> bc) {
predicatemap.put(VisitorEnum.MethodInvocation, bs);
consumermap.put(VisitorEnum.MethodInvocation, bc);
}


/**
*
* @param bs - BiPredicate that can be assigned a lambda expression
Expand Down Expand Up @@ -3428,6 +3493,13 @@ public static <V, T> void callMethodInvocationVisitor(String methodname, ASTNode
hv.build(node);
}

public static <V, T> void callMethodInvocationVisitor(Class<?> methodof, String methodname, ASTNode node, ReferenceHolder<V, T> dataholder, Set<ASTNode> nodesprocessed,
BiPredicate<MethodInvocation, ReferenceHolder<V, T>> bs) {

HelperVisitor<ReferenceHolder<V, T>,V,T> hv= new HelperVisitor<>(nodesprocessed, dataholder);
hv.addMethodInvocation(methodof, methodname, bs);
hv.build(node);
}
/**
*
* @param nodesprocessed - set of nodes processed
Expand Down Expand Up @@ -5627,6 +5699,13 @@ public static <V, T> void callClassInstanceCreationVisitor(ASTNode node, Referen
hv.build(node);
}

public static <V, T> void callClassInstanceCreationVisitor(Class<?> class1, ASTNode node, ReferenceHolder<V, T> dataholder, Set<ASTNode> nodesprocessed,
BiPredicate<ClassInstanceCreation, ReferenceHolder<V, T>> bs) {

HelperVisitor<ReferenceHolder<V, T>,V,T> hv= new HelperVisitor<>(nodesprocessed, dataholder);
hv.addClassInstanceCreation(class1, bs);
hv.build(node);
}
/**
*
* @param nodesprocessed - set of nodes processed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
*******************************************************************************/
package org.eclipse.jdt.internal.common;

import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.BiPredicate;

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

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

/**
*
* @author chammer
Expand Down Expand Up @@ -493,12 +496,40 @@ public boolean visit(MethodDeclaration node) {
return true;
}

// @Override
// public boolean visit(MethodInvocation node) {
// if (this.helperVisitor.predicatemap.containsKey(VisitorEnum.MethodInvocation)) {
// String data=(String) this.helperVisitor.getSupplierData().get(VisitorEnum.MethodInvocation);
// if (data!= null && !node.getName().getIdentifier().equals(data)) {
// return true;
// }
// return ((BiPredicate<MethodInvocation, E>) (this.helperVisitor.predicatemap.get(VisitorEnum.MethodInvocation))).test(node, this.helperVisitor.dataholder);
// }
// return true;
// }

@Override
public boolean visit(MethodInvocation node) {
if (this.helperVisitor.predicatemap.containsKey(VisitorEnum.MethodInvocation)) {
String data=(String) this.helperVisitor.getSupplierData().get(VisitorEnum.MethodInvocation);
if (data!= null && !node.getName().getIdentifier().equals(data)) {
return true;
Map<String, Object> map=(Map<String, Object>) this.helperVisitor.getSupplierData().get(VisitorEnum.MethodInvocation);
if(map != null) {
String data=(String) map.get(HelperVisitor.METHODNAME);
if ((data!= null) && !node.getName().getIdentifier().equals(data)) {
return true;
}
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)) {
return ((BiPredicate<MethodInvocation, E>) (this.helperVisitor.predicatemap.get(VisitorEnum.MethodInvocation))).test(node, this.helperVisitor.dataholder);
}
} else
if (ASTNodes.usesGivenSignature(node, typeof.getCanonicalName(), data, parameterTypesQualifiedNames)) {
return ((BiPredicate<MethodInvocation, E>) (this.helperVisitor.predicatemap.get(VisitorEnum.MethodInvocation))).test(node, this.helperVisitor.dataholder);
}
}
}
return ((BiPredicate<MethodInvocation, E>) (this.helperVisitor.predicatemap.get(VisitorEnum.MethodInvocation))).test(node, this.helperVisitor.dataholder);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ private MultiFixMessages() {
public static String StringConcatToTextBlockCleanUp_description;
public static String StringConcatToTextBlockStringBuffer_description;
public static String StringBuilderForLocalVarsOnlyCleanUp_description;
public static String ExplicitEncodingCleanUp_description;
public static String ExplicitEncodingCleanUpFix_refactor;

static {
// initialize resource bundle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +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}
ExplicitEncodingCleanUpFix_refactor=Use explicit encoding
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*******************************************************************************
* 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.ui.fix;

import static org.eclipse.jdt.internal.corext.fix.CleanUpConstants.EXPLICITENCODING_AGGREGATE_TO_UTF8;
import static org.eclipse.jdt.internal.corext.fix.CleanUpConstants.EXPLICITENCODING_CLEANUP;
import static org.eclipse.jdt.internal.corext.fix.CleanUpConstants.EXPLICITENCODING_INSERT_UTF8;
import static org.eclipse.jdt.internal.corext.fix.CleanUpConstants.EXPLICITENCODING_KEEP_BEHAVIOR;
import static org.eclipse.jdt.internal.ui.fix.MultiFixMessages.ExplicitEncodingCleanUpFix_refactor;
import static org.eclipse.jdt.internal.ui.fix.MultiFixMessages.ExplicitEncodingCleanUp_description;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.eclipse.core.runtime.CoreException;

import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.CompilationUnit;

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.util.Messages;

import org.eclipse.jdt.ui.cleanup.CleanUpContext;
import org.eclipse.jdt.ui.cleanup.CleanUpRequirements;
import org.eclipse.jdt.ui.cleanup.ICleanUpFix;

public class UseExplicitEncodingCleanUpCore extends AbstractCleanUp {
public UseExplicitEncodingCleanUpCore(final Map<String, String> options) {
super(options);
}
public UseExplicitEncodingCleanUpCore() {
}

@Override
public CleanUpRequirements getRequirements() {
return new CleanUpRequirements(requireAST(), false, false, null);
}

public boolean requireAST() {
return isEnabled(EXPLICITENCODING_CLEANUP)&& !computeFixSet().isEmpty();
}
@Override
public ICleanUpFix createFix(final CleanUpContext context) throws CoreException {
CompilationUnit compilationUnit= context.getAST();
if (compilationUnit == null) {
return null;
}
EnumSet<UseExplicitEncodingFixCore> computeFixSet= computeFixSet();
if(!isEnabled(EXPLICITENCODING_CLEANUP) || computeFixSet.isEmpty()) {
return null;
}

ChangeBehavior cb= computeRefactorDeepth();
Set<CompilationUnitRewriteOperation> operations= new LinkedHashSet<>();
Set<ASTNode> nodesprocessed= new HashSet<>();
computeFixSet.forEach(i->i.findOperations(compilationUnit,operations,nodesprocessed,cb));
if (operations.isEmpty()) {
return null;
}
return new CompilationUnitRewriteOperationsFixCore(ExplicitEncodingCleanUpFix_refactor,
compilationUnit, operations.toArray(new CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation[0]));
}

private ChangeBehavior computeRefactorDeepth() {
ChangeBehavior cb=ChangeBehavior.KEEP;
if(isEnabled(EXPLICITENCODING_KEEP_BEHAVIOR)) {
cb=ChangeBehavior.KEEP;
}
if(isEnabled(EXPLICITENCODING_INSERT_UTF8)) {
cb=ChangeBehavior.USE_UTF8;
}
if(isEnabled(EXPLICITENCODING_AGGREGATE_TO_UTF8)) {
cb=ChangeBehavior.USE_UTF8_AGGREGATE;
}
return cb;
}

@Override
public String[] getStepDescriptions() {
List<String> result= new ArrayList<>();
if (isEnabled(EXPLICITENCODING_CLEANUP)) {
String with=computeRefactorDeepth().toString();
result.add(Messages.format(ExplicitEncodingCleanUp_description,new Object[] {String.join(",", computeFixSet().stream().map(UseExplicitEncodingFixCore::toString).collect(Collectors.toList())),with})); //$NON-NLS-1$
}
return result.toArray(new String[0]);
}

@Override
public String getPreview() {
StringBuilder sb=new StringBuilder();
EnumSet<UseExplicitEncodingFixCore> computeFixSet= computeFixSet();
ChangeBehavior cb= computeRefactorDeepth();
EnumSet.allOf(UseExplicitEncodingFixCore.class).forEach(e->sb.append(e.getPreview(computeFixSet.contains(e),cb)));
return sb.toString();
}

private EnumSet<UseExplicitEncodingFixCore> computeFixSet() {
EnumSet<UseExplicitEncodingFixCore> fixSet= EnumSet.noneOf(UseExplicitEncodingFixCore.class);

if(isEnabled(EXPLICITENCODING_CLEANUP)) {
fixSet= EnumSet.allOf(UseExplicitEncodingFixCore.class);
}
return fixSet;
}
}
Loading

0 comments on commit 8538f32

Please sign in to comment.