Skip to content

Commit b69eafb

Browse files
Explicitencoding cleanup (eclipse-jdt#1631)
* add explicitencoding cleanup - add new UseExplicitEncodingCleanUp, UseExplicitEncodingCleanUpCore, UseExplicitEncodingFixCore, and AbstractExplicitEndoing classes - inherit specific processing in enum - add testcases - add some suppresswarnings to other code to allow build to succeed --------- Co-authored-by: Jeff Johnston <[email protected]>
1 parent af9fabb commit b69eafb

File tree

57 files changed

+11707
-117
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+11707
-117
lines changed

org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/common/ASTProcessor.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2021, 2022 Carsten Hammer.
2+
* Copyright (c) 2021, 2025 Carsten Hammer.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -13,8 +13,10 @@
1313
*******************************************************************************/
1414
package org.eclipse.jdt.internal.common;
1515

16+
import java.util.AbstractMap;
1617
import java.util.LinkedHashMap;
1718
import java.util.LinkedList;
19+
import java.util.Map;
1820
import java.util.Set;
1921
import java.util.function.BiPredicate;
2022
import java.util.function.Function;
@@ -1197,7 +1199,9 @@ public ASTProcessor<E, V, T> callMethodInvocationVisitor(
11971199
*/
11981200
public ASTProcessor<E, V, T> callMethodInvocationVisitor(String methodname,
11991201
BiPredicate<ASTNode, E> bs) {
1200-
nodetypelist.put(VisitorEnum.MethodInvocation, new NodeHolder(bs, null, methodname));
1202+
nodetypelist.put(VisitorEnum.MethodInvocation, new NodeHolder(bs, null, Map.ofEntries(
1203+
new AbstractMap.SimpleEntry<>(HelperVisitor.METHODNAME, methodname)
1204+
)));
12011205
return this;
12021206
}
12031207

@@ -1209,7 +1213,9 @@ public ASTProcessor<E, V, T> callMethodInvocationVisitor(String methodname,
12091213
*/
12101214
public ASTProcessor<E, V, T> callMethodInvocationVisitor(String methodname,
12111215
BiPredicate<ASTNode, E> bs, Function<ASTNode, ASTNode> navigate) {
1212-
nodetypelist.put(VisitorEnum.MethodInvocation, new NodeHolder(bs, navigate, methodname));
1216+
nodetypelist.put(VisitorEnum.MethodInvocation, new NodeHolder(bs, navigate, Map.ofEntries(
1217+
new AbstractMap.SimpleEntry<>(HelperVisitor.METHODNAME, methodname)
1218+
)));
12131219
return this;
12141220
}
12151221

org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/common/HelperVisitor.java

Lines changed: 86 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2021, 2022 Carsten Hammer.
2+
* Copyright (c) 2021, 2025 Carsten Hammer.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -13,6 +13,7 @@
1313
*******************************************************************************/
1414
package org.eclipse.jdt.internal.common;
1515

16+
import java.util.AbstractMap;
1617
import java.util.EnumSet;
1718
import java.util.HashMap;
1819
import java.util.LinkedHashMap;
@@ -34,6 +35,9 @@
3435
*/
3536
public class HelperVisitor<E extends HelperVisitorProvider<V, T, E>,V,T> {
3637

38+
public static final String TYPEOF = "typeof"; //$NON-NLS-1$
39+
public static final String METHODNAME = "methodname"; //$NON-NLS-1$
40+
public static final String PARAMTYPENAMES = "paramtypenames"; //$NON-NLS-1$
3741
ASTVisitor astvisitor;
3842

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

377+
/**
378+
*
379+
* @param typeof class to be instantiated
380+
* @param bs - BiPredicate that can be assigned a lambda expression
381+
* @return - previous BiPredicate registered
382+
*/
383+
public BiPredicate<? extends ASTNode, E> addClassInstanceCreation(Class<?> typeof, BiPredicate<ClassInstanceCreation, E> bs) {
384+
Map<String, Object> map = Map.ofEntries(
385+
new AbstractMap.SimpleEntry<>(TYPEOF, typeof)
386+
);
387+
predicatedata.put(VisitorEnum.ClassInstanceCreation, map);
388+
return predicatemap.put(VisitorEnum.ClassInstanceCreation, bs);
389+
}
390+
373391
/**
374392
* Add BiPredicate to use for CompilationUnit visit
375393
*
@@ -719,7 +737,27 @@ public BiPredicate<? extends ASTNode, E> addMethodInvocation(BiPredicate<MethodI
719737
*/
720738
public BiPredicate<? extends ASTNode, E> addMethodInvocation(String methodname,
721739
BiPredicate<MethodInvocation, E> bs) {
722-
this.predicatedata.put(VisitorEnum.MethodInvocation, methodname);
740+
this.predicatedata.put(VisitorEnum.MethodInvocation, Map.ofEntries(
741+
new AbstractMap.SimpleEntry<>(METHODNAME, methodname)
742+
));
743+
return predicatemap.put(VisitorEnum.MethodInvocation, bs);
744+
}
745+
746+
/**
747+
* Add BiPredicate to use for MethodInvocation visit where class and method name is specified
748+
*
749+
* @param typeof class whose method is called
750+
* @param methodname name of the method that is called
751+
* @param bs BiPredicate that can be assigned a lambda expression
752+
* @return previous BiPredicate registered
753+
*/
754+
public BiPredicate<? extends ASTNode, E> addMethodInvocation(Class<?> typeof, String methodname,
755+
BiPredicate<MethodInvocation, E> bs) {
756+
Map<String, Object> map = Map.ofEntries(
757+
new AbstractMap.SimpleEntry<>(METHODNAME, methodname),
758+
new AbstractMap.SimpleEntry<>(TYPEOF, typeof)
759+
);
760+
predicatedata.put(VisitorEnum.MethodInvocation, map);
723761
return predicatemap.put(VisitorEnum.MethodInvocation, bs);
724762
}
725763

@@ -1484,7 +1522,9 @@ public BiConsumer<? extends ASTNode, E> addMethodInvocation(BiConsumer<MethodInv
14841522
}
14851523

14861524
public BiConsumer<? extends ASTNode, E> addMethodInvocation(String methodname, BiConsumer<MethodInvocation, E> bc) {
1487-
this.consumerdata.put(VisitorEnum.MethodInvocation, methodname);
1525+
this.consumerdata.put(VisitorEnum.MethodInvocation, Map.ofEntries(
1526+
new AbstractMap.SimpleEntry<>(METHODNAME, methodname)
1527+
));
14881528
return consumermap.put(VisitorEnum.MethodInvocation, bc);
14891529
}
14901530

@@ -2180,24 +2220,51 @@ public void addMethodDeclaration(BiPredicate<MethodDeclaration, E> bs, BiConsume
21802220

21812221
/**
21822222
*
2183-
* @param bs - BiPredicate that can be assigned a lambda expression
2223+
* @param methodname Only visit MethodInvocation with this name
2224+
* @param bs - BiPredicate that is visited when a MethodInvocation is found
2225+
* @param bc - BiConsumer that is visited at the end after a MethodInvocation has been found
21842226
*/
21852227
public void addMethodInvocation(String methodname, BiPredicate<MethodInvocation, E> bs,
21862228
BiConsumer<MethodInvocation, E> bc) {
2187-
this.predicatedata.put(VisitorEnum.MethodInvocation, methodname);
2229+
predicatedata.put(VisitorEnum.MethodInvocation, Map.ofEntries(
2230+
new AbstractMap.SimpleEntry<>(METHODNAME, methodname)
2231+
));
2232+
predicatemap.put(VisitorEnum.MethodInvocation, bs);
2233+
consumerdata.put(VisitorEnum.MethodInvocation, Map.ofEntries(
2234+
new AbstractMap.SimpleEntry<>(METHODNAME, methodname)
2235+
));
2236+
consumermap.put(VisitorEnum.MethodInvocation, bc);
2237+
}
2238+
2239+
/**
2240+
* @param typeof Only visit MethodInvocation calling a method of this class
2241+
* @param methodname Only visit MethodInvocation with this name
2242+
* @param bs - BiPredicate that is visited when a MethodInvocation is found
2243+
* @param bc - BiConsumer that is visited at the end after a MethodInvocation has been found
2244+
*/
2245+
public void addMethodInvocation(Class<?> typeof, String methodname, BiPredicate<MethodInvocation, E> bs,
2246+
BiConsumer<MethodInvocation, E> bc) {
2247+
Map<String, Object> map = Map.ofEntries(
2248+
new AbstractMap.SimpleEntry<>(METHODNAME, methodname),
2249+
new AbstractMap.SimpleEntry<>(TYPEOF, typeof)
2250+
);
2251+
predicatedata.put(VisitorEnum.MethodInvocation, map);
21882252
predicatemap.put(VisitorEnum.MethodInvocation, bs);
2253+
consumerdata.put(VisitorEnum.MethodInvocation, map);
21892254
consumermap.put(VisitorEnum.MethodInvocation, bc);
21902255
}
21912256

21922257
/**
21932258
*
21942259
* @param bs - BiPredicate that can be assigned a lambda expression
2260+
* @param bc - BiConsumer that is visited at the end after a MethodInvocation has been found
21952261
*/
21962262
public void addMethodInvocation(BiPredicate<MethodInvocation, E> bs, BiConsumer<MethodInvocation, E> bc) {
21972263
predicatemap.put(VisitorEnum.MethodInvocation, bs);
21982264
consumermap.put(VisitorEnum.MethodInvocation, bc);
21992265
}
22002266

2267+
22012268
/**
22022269
*
22032270
* @param bs - BiPredicate that can be assigned a lambda expression
@@ -3428,6 +3495,13 @@ public static <V, T> void callMethodInvocationVisitor(String methodname, ASTNode
34283495
hv.build(node);
34293496
}
34303497

3498+
public static <V, T> void callMethodInvocationVisitor(Class<?> methodof, String methodname, ASTNode node, ReferenceHolder<V, T> dataholder, Set<ASTNode> nodesprocessed,
3499+
BiPredicate<MethodInvocation, ReferenceHolder<V, T>> bs) {
3500+
3501+
HelperVisitor<ReferenceHolder<V, T>,V,T> hv= new HelperVisitor<>(nodesprocessed, dataholder);
3502+
hv.addMethodInvocation(methodof, methodname, bs);
3503+
hv.build(node);
3504+
}
34313505
/**
34323506
*
34333507
* @param nodesprocessed - set of nodes processed
@@ -5627,6 +5701,13 @@ public static <V, T> void callClassInstanceCreationVisitor(ASTNode node, Referen
56275701
hv.build(node);
56285702
}
56295703

5704+
public static <V, T> void callClassInstanceCreationVisitor(Class<?> class1, ASTNode node, ReferenceHolder<V, T> dataholder, Set<ASTNode> nodesprocessed,
5705+
BiPredicate<ClassInstanceCreation, ReferenceHolder<V, T>> bs) {
5706+
5707+
HelperVisitor<ReferenceHolder<V, T>,V,T> hv= new HelperVisitor<>(nodesprocessed, dataholder);
5708+
hv.addClassInstanceCreation(class1, bs);
5709+
hv.build(node);
5710+
}
56305711
/**
56315712
*
56325713
* @param nodesprocessed - set of nodes processed

org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/common/LambdaASTVisitor.java

Lines changed: 72 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2021, 2022 Carsten Hammer.
2+
* Copyright (c) 2021, 2025 Carsten Hammer.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -13,21 +13,28 @@
1313
*******************************************************************************/
1414
package org.eclipse.jdt.internal.common;
1515

16+
import java.util.Map;
1617
import java.util.function.BiConsumer;
1718
import java.util.function.BiPredicate;
1819

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

22+
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
23+
2124
/**
2225
*
2326
* @author chammer
2427
*
2528
* @param <E> - type that extends HelpVisitorProvider that provides {@code HelperVisitor<V, T>}
2629
* @param <V> - type that HelperVisitor uses as map key type
2730
* @param <T> - type that HelperVisitor uses as map value type
31+
* @since 1.15
2832
*/
29-
@SuppressWarnings("unchecked")
33+
@SuppressWarnings({ "unchecked" })
3034
public class LambdaASTVisitor<E extends HelperVisitorProvider<V,T,E>, V, T> extends ASTVisitor {
35+
/**
36+
*
37+
*/
3138
private final HelperVisitor<E,V,T> helperVisitor;
3239

3340
/**
@@ -190,6 +197,16 @@ public boolean visit(CharacterLiteral node) {
190197
@Override
191198
public boolean visit(ClassInstanceCreation node) {
192199
if (this.helperVisitor.predicatemap.containsKey(VisitorEnum.ClassInstanceCreation)) {
200+
Map<String, Object> map=(Map<String, Object>) this.helperVisitor.getSupplierData().get(VisitorEnum.ClassInstanceCreation);
201+
if(map != null) {
202+
Class<?> typeof=(Class<?>) map.get(HelperVisitor.TYPEOF);
203+
if(typeof!=null) {
204+
ITypeBinding binding= node.resolveTypeBinding();
205+
if (!typeof.getSimpleName().equals(binding.getName())) {
206+
return true;
207+
}
208+
}
209+
}
193210
return ((BiPredicate<ClassInstanceCreation, E>) (this.helperVisitor.predicatemap
194211
.get(VisitorEnum.ClassInstanceCreation))).test(node, this.helperVisitor.dataholder);
195212
}
@@ -496,9 +513,25 @@ public boolean visit(MethodDeclaration node) {
496513
@Override
497514
public boolean visit(MethodInvocation node) {
498515
if (this.helperVisitor.predicatemap.containsKey(VisitorEnum.MethodInvocation)) {
499-
String data=(String) this.helperVisitor.getSupplierData().get(VisitorEnum.MethodInvocation);
500-
if (data!= null && !node.getName().getIdentifier().equals(data)) {
501-
return true;
516+
Map<String, Object> map=(Map<String, Object>) this.helperVisitor.getSupplierData().get(VisitorEnum.MethodInvocation);
517+
if(map != null) {
518+
String data=(String) map.get(HelperVisitor.METHODNAME);
519+
if ((data!= null) && !node.getName().getIdentifier().equals(data)) {
520+
return true;
521+
}
522+
Class<?> typeof=(Class<?>) map.get(HelperVisitor.TYPEOF);
523+
String[] parameterTypesQualifiedNames=(String[]) map.get(HelperVisitor.PARAMTYPENAMES);
524+
525+
if(typeof!=null) {
526+
if(parameterTypesQualifiedNames==null) {
527+
if (ASTNodes.usesGivenSignature(node, typeof.getCanonicalName(), data)) {
528+
return ((BiPredicate<MethodInvocation, E>) (this.helperVisitor.predicatemap.get(VisitorEnum.MethodInvocation))).test(node, this.helperVisitor.dataholder);
529+
}
530+
} else
531+
if (ASTNodes.usesGivenSignature(node, typeof.getCanonicalName(), data, parameterTypesQualifiedNames)) {
532+
return ((BiPredicate<MethodInvocation, E>) (this.helperVisitor.predicatemap.get(VisitorEnum.MethodInvocation))).test(node, this.helperVisitor.dataholder);
533+
}
534+
}
502535
}
503536
return ((BiPredicate<MethodInvocation, E>) (this.helperVisitor.predicatemap.get(VisitorEnum.MethodInvocation))).test(node, this.helperVisitor.dataholder);
504537
}
@@ -949,14 +982,17 @@ public boolean visit(VariableDeclarationExpression node) {
949982
@Override
950983
public boolean visit(VariableDeclarationStatement node) {
951984
if (this.helperVisitor.predicatemap.containsKey(VisitorEnum.VariableDeclarationStatement)) {
952-
Class<?> data=(Class<?>) this.helperVisitor.getSupplierData().get(VisitorEnum.VariableDeclarationStatement);
953-
if (data!= null) {
954-
VariableDeclarationFragment bli = (VariableDeclarationFragment) node.fragments().get(0);
955-
IVariableBinding resolveBinding = bli.resolveBinding();
956-
if(resolveBinding!=null) {
957-
String qualifiedName = resolveBinding.getType().getErasure().getQualifiedName();
958-
if (!data.getCanonicalName().equals(qualifiedName)) {
959-
return true;
985+
Map<String, Object> map=(Map<String, Object>)this.helperVisitor.getConsumerData().get(VisitorEnum.VariableDeclarationStatement);
986+
if(map != null) {
987+
Class<?> data=(Class<?>) map.get(HelperVisitor.TYPEOF);
988+
if (data!= null) {
989+
VariableDeclarationFragment bli = (VariableDeclarationFragment) node.fragments().get(0);
990+
IVariableBinding resolveBinding = bli.resolveBinding();
991+
if(resolveBinding!=null) {
992+
String qualifiedName = resolveBinding.getType().getErasure().getQualifiedName();
993+
if (!data.getCanonicalName().equals(qualifiedName)) {
994+
return true;
995+
}
960996
}
961997
}
962998
}
@@ -1388,9 +1424,18 @@ public void endVisit(MethodDeclaration node) {
13881424
@Override
13891425
public void endVisit(MethodInvocation node) {
13901426
if (this.helperVisitor.consumermap.containsKey(VisitorEnum.MethodInvocation)) {
1391-
String data=(String) this.helperVisitor.getConsumerData().get(VisitorEnum.MethodInvocation);
1392-
if (data!= null && !node.getName().getIdentifier().equals(data)) {
1393-
return;
1427+
Map<String, Object> map=(Map<String, Object>) this.helperVisitor.getConsumerData().get(VisitorEnum.MethodInvocation);
1428+
if(map != null) {
1429+
String data=(String) map.get(HelperVisitor.METHODNAME);
1430+
if ((data!= null) && !node.getName().getIdentifier().equals(data)) {
1431+
return;
1432+
}
1433+
Class<?> typeof=(Class<?>) map.get(HelperVisitor.TYPEOF);
1434+
if(typeof!=null) {
1435+
if (!ASTNodes.usesGivenSignature(node, typeof.getCanonicalName(), data)) {
1436+
return;
1437+
}
1438+
}
13941439
}
13951440
((BiConsumer<MethodInvocation, E>) (this.helperVisitor.consumermap.get(VisitorEnum.MethodInvocation))).accept(node,
13961441
this.helperVisitor.dataholder);
@@ -1774,14 +1819,17 @@ public void endVisit(VariableDeclarationExpression node) {
17741819
@Override
17751820
public void endVisit(VariableDeclarationStatement node) {
17761821
if (this.helperVisitor.consumermap.containsKey(VisitorEnum.VariableDeclarationStatement)) {
1777-
Class<?> data=(Class<?>) this.helperVisitor.getConsumerData().get(VisitorEnum.VariableDeclarationStatement);
1778-
if (data!= null) {
1779-
VariableDeclarationFragment bli = (VariableDeclarationFragment) node.fragments().get(0);
1780-
IVariableBinding resolveBinding = bli.resolveBinding();
1781-
if(resolveBinding!=null) {
1782-
String qualifiedName = resolveBinding.getType().getErasure().getQualifiedName();
1783-
if (!data.getCanonicalName().equals(qualifiedName)) {
1784-
return;
1822+
Map<String, Object> map=(Map<String, Object>)this.helperVisitor.getConsumerData().get(VisitorEnum.VariableDeclarationStatement);
1823+
if(map != null) {
1824+
Class<?> data=(Class<?>) map.get(HelperVisitor.TYPEOF);
1825+
if (data!= null) {
1826+
VariableDeclarationFragment bli = (VariableDeclarationFragment) node.fragments().get(0);
1827+
IVariableBinding resolveBinding = bli.resolveBinding();
1828+
if(resolveBinding!=null) {
1829+
String qualifiedName = resolveBinding.getType().getErasure().getQualifiedName();
1830+
if (!data.getCanonicalName().equals(qualifiedName)) {
1831+
return;
1832+
}
17851833
}
17861834
}
17871835
}

org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/ui/fix/MultiFixMessages.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ private MultiFixMessages() {
211211
public static String StringConcatToTextBlockCleanUp_description;
212212
public static String StringConcatToTextBlockStringBuffer_description;
213213
public static String StringBuilderForLocalVarsOnlyCleanUp_description;
214+
public static String ExplicitEncodingCleanUp_description;
215+
public static String ExplicitEncodingCleanUpFix_refactor;
214216

215217
static {
216218
// initialize resource bundle

org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/internal/ui/fix/MultiFixMessages.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,5 @@ StringBufferToStringBuilderCleanUp_description=Convert StringBuffer to StringBui
193193
StringConcatToTextBlockCleanUp_description=Convert String concatenation to Text Block
194194
StringConcatToTextBlockStringBuffer_description=Convert String/StringBuffer/StringBuilder concatenation to Text Block
195195
StringBuilderForLocalVarsOnlyCleanUp_description=Convert StringBuffer to StringBuilder for local variables
196+
ExplicitEncodingCleanUp_description=Set explicit encoding or default encoding where applicable using {0}
197+
ExplicitEncodingCleanUpFix_refactor=Use explicit encoding

0 commit comments

Comments
 (0)