Skip to content

Commit

Permalink
updates for current eclipse backport from eclipse pr
Browse files Browse the repository at this point in the history
  • Loading branch information
carstenartur committed Oct 12, 2024
1 parent 2549174 commit 2f69c31
Show file tree
Hide file tree
Showing 32 changed files with 253 additions and 220 deletions.
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.*;

Check warning on line 20 in sandbox_common/src/org/sandbox/jdt/internal/common/LambdaASTVisitor.java

View workflow job for this annotation

GitHub Actions / pmd-code-scan

Unused import 'org.eclipse.jdt.core.dom.*'

Reports import statements that can be removed. They are either unused, duplicated, or the members they import are already implicitly in scope, because they're in java.lang, or the current package. If some imports cannot be resolved, for instance because you run PMD with an incomplete auxiliary classpath, some imports may be conservatively marked as used even if they're not to avoid false positives. UnnecessaryImport (Priority: 4, Ruleset: Code Style) https://docs.pmd-code.org/pmd-doc-7.6.0/pmd_rules_java_codestyle.html#unnecessaryimport

Check warning on line 20 in sandbox_common/src/org/sandbox/jdt/internal/common/LambdaASTVisitor.java

View workflow job for this annotation

GitHub Actions / pmd-code-scan

Unused import 'org.eclipse.jdt.core.dom.*'

Reports import statements that can be removed. They are either unused, duplicated, or the members they import are already implicitly in scope, because they're in java.lang, or the current package. If some imports cannot be resolved, for instance because you run PMD with an incomplete auxiliary classpath, some imports may be conservatively marked as used even if they're not to avoid false positives. UnnecessaryImport (Priority: 4, Ruleset: Code Style) https://docs.pmd-code.org/pmd-doc-7.6.0/pmd_rules_java_codestyle.html#unnecessaryimport

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,25 +521,12 @@ public boolean visit(MethodInvocation node) {
}
Class<?> typeof=(Class<?>) map.get(HelperVisitor.TYPEOF);
String[] parameterTypesQualifiedNames=(String[]) map.get(HelperVisitor.PARAMTYPENAMES);

if(typeof!=null) {
// Name sn= ASTNodes.as(node.getExpression(), Name.class);
// if (sn == null) {
// return true;
// }
// IBinding ibinding= sn.resolveBinding();
// IVariableBinding vb=(IVariableBinding) ibinding;
// if(vb!=null) {
// ITypeBinding binding= vb.getType();
// if ((binding != null) && !typeof.getSimpleName().equals(binding.getName())) {
// return true;
// }
// }

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);
Expand Down Expand Up @@ -712,14 +700,14 @@ public boolean visit(QualifiedType node) {
return true;
}

@Override
public boolean visit(ModuleQualifiedName node) {
if (this.helperVisitor.predicatemap.containsKey(VisitorEnum.ModuleQualifiedName)) {
return ((BiPredicate<ModuleQualifiedName, E>) (this.helperVisitor.predicatemap.get(VisitorEnum.ModuleQualifiedName)))
.test(node, this.helperVisitor.dataholder);
}
return true;
}
// @Override
// public boolean visit(ModuleQualifiedName node) {
// if (this.helperVisitor.predicatemap.containsKey(VisitorEnum.ModuleQualifiedName)) {
// return ((BiPredicate<ModuleQualifiedName, E>) (this.helperVisitor.predicatemap.get(VisitorEnum.ModuleQualifiedName)))
// .test(node, this.helperVisitor.dataholder);
// }
// return true;
// }

@Override
public boolean visit(RequiresDirective node) {
Expand Down Expand Up @@ -1054,7 +1042,7 @@ public boolean visit(YieldStatement node) {
public void endVisit(AnnotationTypeDeclaration node) {
if (this.helperVisitor.consumermap.containsKey(VisitorEnum.AnnotationTypeDeclaration)) {
((BiConsumer<AnnotationTypeDeclaration, E>) (this.helperVisitor.consumermap.get(VisitorEnum.AnnotationTypeDeclaration)))
.accept(node, this.helperVisitor.dataholder);
.accept(node, this.helperVisitor.dataholder);
}
}

Expand All @@ -1070,7 +1058,7 @@ public void endVisit(AnnotationTypeMemberDeclaration node) {
public void endVisit(AnonymousClassDeclaration node) {
if (this.helperVisitor.consumermap.containsKey(VisitorEnum.AnonymousClassDeclaration)) {
((BiConsumer<AnonymousClassDeclaration, E>) (this.helperVisitor.consumermap.get(VisitorEnum.AnonymousClassDeclaration)))
.accept(node, this.helperVisitor.dataholder);
.accept(node, this.helperVisitor.dataholder);
}
}

Expand Down Expand Up @@ -1175,7 +1163,7 @@ public void endVisit(CharacterLiteral node) {
public void endVisit(ClassInstanceCreation node) {
if (this.helperVisitor.consumermap.containsKey(VisitorEnum.ClassInstanceCreation)) {
((BiConsumer<ClassInstanceCreation, E>) (this.helperVisitor.consumermap.get(VisitorEnum.ClassInstanceCreation)))
.accept(node, this.helperVisitor.dataholder);
.accept(node, this.helperVisitor.dataholder);
}
}

Expand All @@ -1191,15 +1179,15 @@ public void endVisit(CompilationUnit node) {
public void endVisit(ConditionalExpression node) {
if (this.helperVisitor.consumermap.containsKey(VisitorEnum.ConditionalExpression)) {
((BiConsumer<ConditionalExpression, E>) (this.helperVisitor.consumermap.get(VisitorEnum.ConditionalExpression)))
.accept(node, this.helperVisitor.dataholder);
.accept(node, this.helperVisitor.dataholder);
}
}

@Override
public void endVisit(ConstructorInvocation node) {
if (this.helperVisitor.consumermap.containsKey(VisitorEnum.ConstructorInvocation)) {
((BiConsumer<ConstructorInvocation, E>) (this.helperVisitor.consumermap.get(VisitorEnum.ConstructorInvocation)))
.accept(node, this.helperVisitor.dataholder);
.accept(node, this.helperVisitor.dataholder);
}
}

Expand Down Expand Up @@ -1253,7 +1241,7 @@ public void endVisit(EnhancedForStatement node) {
public void endVisit(EnumConstantDeclaration node) {
if (this.helperVisitor.consumermap.containsKey(VisitorEnum.EnumConstantDeclaration)) {
((BiConsumer<EnumConstantDeclaration, E>) (this.helperVisitor.consumermap.get(VisitorEnum.EnumConstantDeclaration)))
.accept(node, this.helperVisitor.dataholder);
.accept(node, this.helperVisitor.dataholder);
}
}

Expand All @@ -1277,7 +1265,7 @@ public void endVisit(ExportsDirective node) {
public void endVisit(ExpressionMethodReference node) {
if (this.helperVisitor.consumermap.containsKey(VisitorEnum.ExpressionMethodReference)) {
((BiConsumer<ExpressionMethodReference, E>) (this.helperVisitor.consumermap.get(VisitorEnum.ExpressionMethodReference)))
.accept(node, this.helperVisitor.dataholder);
.accept(node, this.helperVisitor.dataholder);
}
}

Expand Down Expand Up @@ -1447,16 +1435,6 @@ public void endVisit(MethodInvocation node) {
if (!ASTNodes.usesGivenSignature(node, typeof.getCanonicalName(), data)) {
return;
}
// Name sn= ASTNodes.as(node.getExpression(), Name.class);
// if (sn == null) {
// return;
// }
// IBinding ibinding= sn.resolveBinding();
// IVariableBinding vb=(IVariableBinding) ibinding;
// ITypeBinding binding= vb.getType();
// if ((binding != null) && !typeof.getSimpleName().equals(binding.getName())) {
// return;
// }
}
}
((BiConsumer<MethodInvocation, E>) (this.helperVisitor.consumermap.get(VisitorEnum.MethodInvocation))).accept(node,
Expand Down Expand Up @@ -1545,7 +1523,7 @@ public void endVisit(ParameterizedType node) {
public void endVisit(ParenthesizedExpression node) {
if (this.helperVisitor.consumermap.containsKey(VisitorEnum.ParenthesizedExpression)) {
((BiConsumer<ParenthesizedExpression, E>) (this.helperVisitor.consumermap.get(VisitorEnum.ParenthesizedExpression)))
.accept(node, this.helperVisitor.dataholder);
.accept(node, this.helperVisitor.dataholder);
}
}

Expand Down Expand Up @@ -1652,15 +1630,15 @@ public void endVisit(SimpleType node) {
public void endVisit(SingleMemberAnnotation node) {
if (this.helperVisitor.consumermap.containsKey(VisitorEnum.SingleMemberAnnotation)) {
((BiConsumer<SingleMemberAnnotation, E>) (this.helperVisitor.consumermap.get(VisitorEnum.SingleMemberAnnotation)))
.accept(node, this.helperVisitor.dataholder);
.accept(node, this.helperVisitor.dataholder);
}
}

@Override
public void endVisit(SingleVariableDeclaration node) {
if (this.helperVisitor.consumermap.containsKey(VisitorEnum.SingleVariableDeclaration)) {
((BiConsumer<SingleVariableDeclaration, E>) (this.helperVisitor.consumermap.get(VisitorEnum.SingleVariableDeclaration)))
.accept(node, this.helperVisitor.dataholder);
.accept(node, this.helperVisitor.dataholder);
}
}

Expand All @@ -1675,7 +1653,7 @@ public void endVisit(StringLiteral node) {
public void endVisit(SuperConstructorInvocation node) {
if (this.helperVisitor.consumermap.containsKey(VisitorEnum.SuperConstructorInvocation)) {
((BiConsumer<SuperConstructorInvocation, E>) (this.helperVisitor.consumermap.get(VisitorEnum.SuperConstructorInvocation)))
.accept(node, this.helperVisitor.dataholder);
.accept(node, this.helperVisitor.dataholder);
}
}

Expand All @@ -1691,7 +1669,7 @@ public void endVisit(SuperFieldAccess node) {
public void endVisit(SuperMethodInvocation node) {
if (this.helperVisitor.consumermap.containsKey(VisitorEnum.SuperMethodInvocation)) {
((BiConsumer<SuperMethodInvocation, E>) (this.helperVisitor.consumermap.get(VisitorEnum.SuperMethodInvocation)))
.accept(node, this.helperVisitor.dataholder);
.accept(node, this.helperVisitor.dataholder);
}
}

Expand Down Expand Up @@ -1730,7 +1708,7 @@ public void endVisit(SwitchStatement node) {
public void endVisit(SynchronizedStatement node) {
if (this.helperVisitor.consumermap.containsKey(VisitorEnum.SynchronizedStatement)) {
((BiConsumer<SynchronizedStatement, E>) (this.helperVisitor.consumermap.get(VisitorEnum.SynchronizedStatement)))
.accept(node, this.helperVisitor.dataholder);
.accept(node, this.helperVisitor.dataholder);
}
}

Expand Down Expand Up @@ -1790,7 +1768,7 @@ public void endVisit(TypeDeclaration node) {
public void endVisit(TypeDeclarationStatement node) {
if (this.helperVisitor.consumermap.containsKey(VisitorEnum.TypeDeclarationStatement)) {
((BiConsumer<TypeDeclarationStatement, E>) (this.helperVisitor.consumermap.get(VisitorEnum.TypeDeclarationStatement)))
.accept(node, this.helperVisitor.dataholder);
.accept(node, this.helperVisitor.dataholder);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import org.eclipse.jdt.internal.corext.dom.ASTNodes;
import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation;
import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperationWithSourceRange;
import org.eclipse.jdt.internal.corext.fix.LinkedProposalModelCore;
import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite;
import org.eclipse.jdt.internal.corext.util.Messages;
Expand Down Expand Up @@ -81,21 +82,21 @@ public String getPreview(boolean i, ChangeBehavior cb) {
return explicitencoding.getPreview(i,cb);
}
/**
* Compute set of CompilationUnitRewriteOperation to refactor supported situations using default encoding to make use of explicit calls
* Compute set of CompilationUnitRewriteOperationWithSourceRange to refactor supported situations using default encoding to make use of explicit calls
*
* @param compilationUnit unit to search in
* @param operations set of all CompilationUnitRewriteOperations created already
* @param nodesprocessed list to remember nodes already processed
* @param cb distinguish if you want to keep the same behavior or get code independent of environment
*/
public void findOperations(final CompilationUnit compilationUnit,final Set<CompilationUnitRewriteOperation> operations,final Set<ASTNode> nodesprocessed, ChangeBehavior cb) {
public void findOperations(final CompilationUnit compilationUnit,final Set<CompilationUnitRewriteOperationWithSourceRange> operations,final Set<ASTNode> nodesprocessed, ChangeBehavior cb) {
explicitencoding.find(this, compilationUnit, operations, nodesprocessed, cb);
}

public CompilationUnitRewriteOperation rewrite(final ASTNode visited, ChangeBehavior cb, ReferenceHolder<ASTNode, Object> data) {
return new CompilationUnitRewriteOperation() {
public CompilationUnitRewriteOperationWithSourceRange rewrite(final ASTNode visited, ChangeBehavior cb, ReferenceHolder<ASTNode, Object> data) {
return new CompilationUnitRewriteOperationWithSourceRange() {
@Override
public void rewriteAST(final CompilationUnitRewrite cuRewrite, final LinkedProposalModelCore linkedModel) throws CoreException {
public void rewriteASTInternal(final CompilationUnitRewrite cuRewrite, final LinkedProposalModelCore linkedModel) throws CoreException {
TextEditGroup group= createTextEditGroup(Messages.format(MultiFixMessages.ExplicitEncodingCleanUp_description,new Object[] {UseExplicitEncodingFixCore.this.toString(), cb.toString()}), cuRewrite);
cuRewrite.getASTRewrite().setTargetSourceRangeComputer(computer);
explicitencoding.rewrite(UseExplicitEncodingFixCore.this, visited, cuRewrite, group, cb, data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
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.CompilationUnit;
Expand All @@ -31,10 +29,10 @@
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Name;
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;

import org.eclipse.jdt.internal.corext.dom.ASTNodeFactory;
import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation;
import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperationWithSourceRange;
import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite;
import org.eclipse.text.edits.TextEditGroup;
import org.sandbox.jdt.internal.common.ReferenceHolder;
import org.sandbox.jdt.internal.corext.fix.UseExplicitEncodingFixCore;

Expand All @@ -51,7 +49,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 @@ -63,7 +61,7 @@ static class Nodedata {
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 find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set<CompilationUnitRewriteOperationWithSourceRange> operations, Set<ASTNode> nodesprocessed, ChangeBehavior cb);


public abstract void rewrite(UseExplicitEncodingFixCore useExplicitEncodingFixCore, T visited, CompilationUnitRewrite cuRewrite,
Expand All @@ -72,19 +70,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 +93,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
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

import org.sandbox.jdt.internal.common.ReferenceHolder;
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation;
import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperationWithSourceRange;
import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite;
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;

Expand Down Expand Up @@ -60,7 +60,7 @@
public class ByteArrayOutputStreamExplicitEncoding extends AbstractExplicitEncoding<MethodInvocation> {

@Override
public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set<CompilationUnitRewriteOperation> operations, Set<ASTNode> nodesprocessed,ChangeBehavior cb) {
public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set<CompilationUnitRewriteOperationWithSourceRange> operations, Set<ASTNode> nodesprocessed,ChangeBehavior cb) {
if (!JavaModelUtil.is10OrHigher(compilationUnit.getJavaElement().getJavaProject())) {
/**
* For Java 9 and older just do nothing
Expand All @@ -71,7 +71,7 @@ public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilation
HelperVisitor.callMethodInvocationVisitor(ByteArrayOutputStream.class, METHOD_TOSTRING, compilationUnit, holder, nodesprocessed, (visited, aholder) -> processFoundNode(fixcore, operations, cb, visited, aholder));
}

private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, Set<CompilationUnitRewriteOperation> operations,
private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, Set<CompilationUnitRewriteOperationWithSourceRange> operations,
ChangeBehavior cb, MethodInvocation visited,
ReferenceHolder<ASTNode, Object> holder) {
List<ASTNode> arguments= visited.arguments();
Expand Down Expand Up @@ -125,13 +125,13 @@ public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visite
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 @@ -152,4 +152,9 @@ public String getPreview(boolean afterRefactoring,ChangeBehavior cb) {
}
"""; //$NON-NLS-1$
}

@Override
public String toString() {
return "ba.toString()"; //$NON-NLS-1$
}
}
Loading

0 comments on commit 2f69c31

Please sign in to comment.