Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicitencoding cleanup #1631

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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 @@ -719,7 +737,27 @@ 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);
}

/**
* 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);
}

Expand Down Expand Up @@ -1484,7 +1522,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 +2220,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 +3495,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 +5701,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
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2021, 2022 Carsten Hammer.
* Copyright (c) 2021 Carsten Hammer.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
Expand All @@ -9,25 +9,32 @@
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Carsten Hammer - initial API and implementation
* Carsten Hammer
*******************************************************************************/
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
*
* @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")
@SuppressWarnings({ "unchecked" })
public class LambdaASTVisitor<E extends HelperVisitorProvider<V,T,E>, V, T> extends ASTVisitor {
/**
*
*/
private final HelperVisitor<E,V,T> helperVisitor;

/**
Expand Down Expand Up @@ -190,6 +197,16 @@ public boolean visit(CharacterLiteral node) {
@Override
public boolean visit(ClassInstanceCreation node) {
if (this.helperVisitor.predicatemap.containsKey(VisitorEnum.ClassInstanceCreation)) {
Map<String, Object> map=(Map<String, Object>) this.helperVisitor.getSupplierData().get(VisitorEnum.ClassInstanceCreation);
if(map != null) {
Class<?> typeof=(Class<?>) map.get(HelperVisitor.TYPEOF);
if(typeof!=null) {
ITypeBinding binding= node.resolveTypeBinding();
if (!typeof.getSimpleName().equals(binding.getName())) {
return true;
}
}
}
return ((BiPredicate<ClassInstanceCreation, E>) (this.helperVisitor.predicatemap
.get(VisitorEnum.ClassInstanceCreation))).test(node, this.helperVisitor.dataholder);
}
Expand Down Expand Up @@ -496,9 +513,25 @@ public boolean visit(MethodDeclaration node) {
@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 Expand Up @@ -949,14 +982,17 @@ public boolean visit(VariableDeclarationExpression node) {
@Override
public boolean visit(VariableDeclarationStatement node) {
if (this.helperVisitor.predicatemap.containsKey(VisitorEnum.VariableDeclarationStatement)) {
Class<?> data=(Class<?>) this.helperVisitor.getSupplierData().get(VisitorEnum.VariableDeclarationStatement);
if (data!= null) {
VariableDeclarationFragment bli = (VariableDeclarationFragment) node.fragments().get(0);
IVariableBinding resolveBinding = bli.resolveBinding();
if(resolveBinding!=null) {
String qualifiedName = resolveBinding.getType().getErasure().getQualifiedName();
if (!data.getCanonicalName().equals(qualifiedName)) {
return true;
Map<String, Object> map=(Map<String, Object>)this.helperVisitor.getConsumerData().get(VisitorEnum.VariableDeclarationStatement);
if(map != null) {
Class<?> data=(Class<?>) map.get(HelperVisitor.TYPEOF);
if (data!= null) {
VariableDeclarationFragment bli = (VariableDeclarationFragment) node.fragments().get(0);
IVariableBinding resolveBinding = bli.resolveBinding();
if(resolveBinding!=null) {
String qualifiedName = resolveBinding.getType().getErasure().getQualifiedName();
if (!data.getCanonicalName().equals(qualifiedName)) {
return true;
}
}
}
}
Expand Down Expand Up @@ -1388,9 +1424,18 @@ public void endVisit(MethodDeclaration node) {
@Override
public void endVisit(MethodInvocation node) {
if (this.helperVisitor.consumermap.containsKey(VisitorEnum.MethodInvocation)) {
String data=(String) this.helperVisitor.getConsumerData().get(VisitorEnum.MethodInvocation);
if (data!= null && !node.getName().getIdentifier().equals(data)) {
return;
Map<String, Object> map=(Map<String, Object>) this.helperVisitor.getConsumerData().get(VisitorEnum.MethodInvocation);
if(map != null) {
String data=(String) map.get(HelperVisitor.METHODNAME);
if ((data!= null) && !node.getName().getIdentifier().equals(data)) {
return;
}
Class<?> typeof=(Class<?>) map.get(HelperVisitor.TYPEOF);
if(typeof!=null) {
if (!ASTNodes.usesGivenSignature(node, typeof.getCanonicalName(), data)) {
return;
}
}
}
((BiConsumer<MethodInvocation, E>) (this.helperVisitor.consumermap.get(VisitorEnum.MethodInvocation))).accept(node,
this.helperVisitor.dataholder);
Expand Down Expand Up @@ -1774,14 +1819,17 @@ public void endVisit(VariableDeclarationExpression node) {
@Override
public void endVisit(VariableDeclarationStatement node) {
if (this.helperVisitor.consumermap.containsKey(VisitorEnum.VariableDeclarationStatement)) {
Class<?> data=(Class<?>) this.helperVisitor.getConsumerData().get(VisitorEnum.VariableDeclarationStatement);
if (data!= null) {
VariableDeclarationFragment bli = (VariableDeclarationFragment) node.fragments().get(0);
IVariableBinding resolveBinding = bli.resolveBinding();
if(resolveBinding!=null) {
String qualifiedName = resolveBinding.getType().getErasure().getQualifiedName();
if (!data.getCanonicalName().equals(qualifiedName)) {
return;
Map<String, Object> map=(Map<String, Object>)this.helperVisitor.getConsumerData().get(VisitorEnum.VariableDeclarationStatement);
if(map != null) {
Class<?> data=(Class<?>) map.get(HelperVisitor.TYPEOF);
if (data!= null) {
VariableDeclarationFragment bli = (VariableDeclarationFragment) node.fragments().get(0);
IVariableBinding resolveBinding = bli.resolveBinding();
if(resolveBinding!=null) {
String qualifiedName = resolveBinding.getType().getErasure().getQualifiedName();
if (!data.getCanonicalName().equals(qualifiedName)) {
return;
}
}
}
}
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=Set explicit encoding or default encoding where applicable on methods ''{0}'' using {1}
ExplicitEncodingCleanUpFix_refactor=Use explicit encoding
Loading
Loading