Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
carstenartur committed Sep 2, 2024
1 parent 7a8a4e4 commit 9efdff8
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.StringLiteral;
Expand All @@ -32,6 +31,7 @@
import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite;
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
import org.eclipse.text.edits.TextEditGroup;
import org.sandbox.jdt.internal.common.HelperVisitor;
import org.sandbox.jdt.internal.common.ReferenceHolder;
import org.sandbox.jdt.internal.corext.fix.UseExplicitEncodingFixCore;

Expand Down Expand Up @@ -61,43 +61,41 @@ public class ByteArrayOutputStreamExplicitEncoding extends AbstractExplicitEncod
@Override
public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set<CompilationUnitRewriteOperation> operations, Set<ASTNode> nodesprocessed,ChangeBehavior cb) {
ReferenceHolder<ASTNode, Object> holder= new ReferenceHolder<>();
compilationUnit.accept(new ASTVisitor() {
@Override
public boolean visit(final MethodInvocation visited) {
if(nodesprocessed.contains(visited)) {
return false;
}
List<ASTNode> arguments= visited.arguments();
if (ASTNodes.usesGivenSignature(visited, ByteArrayOutputStream.class.getCanonicalName(), METHOD_TOSTRING, String.class.getCanonicalName())) {
if(!(arguments.get(0) instanceof StringLiteral)) {
return false;
}
StringLiteral argstring3= (StringLiteral) arguments.get(0);
if (!encodings.contains(argstring3.getLiteralValue())) {
return false;
}
Nodedata nd=new Nodedata();
nd.encoding=encodingmap.get(argstring3.getLiteralValue());
nd.replace=true;
nd.visited=argstring3;
holder.put(visited,nd);
operations.add(fixcore.rewrite(visited, cb, holder));
nodesprocessed.add(visited);
return false;
}
if (ASTNodes.usesGivenSignature(visited, ByteArrayOutputStream.class.getCanonicalName(), METHOD_TOSTRING)) {
Nodedata nd2=new Nodedata();
nd2.encoding=null;
nd2.replace=false;
nd2.visited=visited;
holder.put(visited,nd2);
operations.add(fixcore.rewrite(visited, cb, holder));
nodesprocessed.add(visited);
return false;
}
return true;
HelperVisitor.callMethodInvocationVisitor(ByteArrayOutputStream.class, METHOD_TOSTRING, compilationUnit, holder, nodesprocessed, (visited, aholder) -> processFoundNode(fixcore, operations, nodesprocessed, cb, visited, aholder));
}

private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, Set<CompilationUnitRewriteOperation> operations,
Set<ASTNode> nodesprocessed, ChangeBehavior cb, MethodInvocation visited,
ReferenceHolder<ASTNode, Object> holder) {
List<ASTNode> arguments= visited.arguments();
if (ASTNodes.usesGivenSignature(visited, ByteArrayOutputStream.class.getCanonicalName(), METHOD_TOSTRING, String.class.getCanonicalName())) {
if(!(arguments.get(0) instanceof StringLiteral)) {
return false;
}
StringLiteral argstring3= (StringLiteral) arguments.get(0);
if (!encodings.contains(argstring3.getLiteralValue())) {
return false;
}
});
Nodedata nd=new Nodedata();
nd.encoding=encodingmap.get(argstring3.getLiteralValue());
nd.replace=true;
nd.visited=argstring3;
holder.put(visited,nd);
operations.add(fixcore.rewrite(visited, cb, holder));
nodesprocessed.add(visited);
return false;
}
if (ASTNodes.usesGivenSignature(visited, ByteArrayOutputStream.class.getCanonicalName(), METHOD_TOSTRING)) {
Nodedata nd2=new Nodedata();
nd2.encoding=null;
nd2.replace=false;
nd2.visited=visited;
holder.put(visited,nd2);
operations.add(fixcore.rewrite(visited, cb, holder));
nodesprocessed.add(visited);
return false;
}
return false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,15 @@

import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation;
import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite;
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
import org.eclipse.text.edits.TextEditGroup;
import org.sandbox.jdt.internal.common.HelperVisitor;
import org.sandbox.jdt.internal.common.ReferenceHolder;
import org.sandbox.jdt.internal.corext.fix.UseExplicitEncodingFixCore;
/**
Expand All @@ -46,22 +45,31 @@ public class FileWriterExplicitEncoding extends AbstractExplicitEncoding<ClassIn
@Override
public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set<CompilationUnitRewriteOperation> operations, Set<ASTNode> nodesprocessed,ChangeBehavior cb) {
ReferenceHolder<ASTNode, Object> datah= new ReferenceHolder<>();
compilationUnit.accept(new ASTVisitor() {
@Override
public boolean visit(final ClassInstanceCreation visited) {
if(nodesprocessed.contains(visited)) {
return false;
}
ITypeBinding binding= visited.resolveTypeBinding();
if (FileWriter.class.getSimpleName().equals(binding.getName())) {
operations.add(fixcore.rewrite(visited, cb, datah));
nodesprocessed.add(visited);
return false;
}
return true;
}
});
}
// compilationUnit.accept(new ASTVisitor() {
// @Override
// public boolean visit(final ClassInstanceCreation visited) {
// if(nodesprocessed.contains(visited)) {
// return false;
// }
// ITypeBinding binding= visited.resolveTypeBinding();
// if (FileWriter.class.getSimpleName().equals(binding.getName())) {
// operations.add(fixcore.rewrite(visited, cb, datah));
// nodesprocessed.add(visited);
// return false;
// }
// return true;
// }
// });
HelperVisitor.callClassInstanceCreationVisitor(FileWriter.class, compilationUnit, datah, nodesprocessed, (visited, holder) -> processFoundNode(fixcore, operations, nodesprocessed, cb, visited, holder));
}

private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, Set<CompilationUnitRewriteOperation> operations,
Set<ASTNode> nodesprocessed, ChangeBehavior cb, ClassInstanceCreation visited,
ReferenceHolder<ASTNode, Object> holder) {
operations.add(fixcore.rewrite(visited, cb, holder));
nodesprocessed.add(visited);
return false;
}

@Override
public void rewrite(UseExplicitEncodingFixCore upp,final ClassInstanceCreation visited, final CompilationUnitRewrite cuRewrite,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,15 @@

import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation;
import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite;
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
import org.eclipse.text.edits.TextEditGroup;
import org.sandbox.jdt.internal.common.HelperVisitor;
import org.sandbox.jdt.internal.common.ReferenceHolder;
import org.sandbox.jdt.internal.corext.fix.UseExplicitEncodingFixCore;
/**
Expand All @@ -47,21 +46,15 @@ public class PrintWriterExplicitEncoding extends AbstractExplicitEncoding<ClassI
@Override
public void find(UseExplicitEncodingFixCore fixcore, CompilationUnit compilationUnit, Set<CompilationUnitRewriteOperation> operations, Set<ASTNode> nodesprocessed,ChangeBehavior cb) {
ReferenceHolder<ASTNode, Object> datah= new ReferenceHolder<>();
compilationUnit.accept(new ASTVisitor() {
@Override
public boolean visit(final ClassInstanceCreation visited) {
if(nodesprocessed.contains(visited)) {
return false;
}
ITypeBinding binding= visited.resolveTypeBinding();
if (PrintWriter.class.getSimpleName().equals(binding.getName())) {
operations.add(fixcore.rewrite(visited, cb, datah));
nodesprocessed.add(visited);
return false;
}
return true;
}
});
HelperVisitor.callClassInstanceCreationVisitor(PrintWriter.class, compilationUnit, datah, nodesprocessed, (visited, holder) -> processFoundNode(fixcore, operations, nodesprocessed, cb, visited, holder));
}

private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore, Set<CompilationUnitRewriteOperation> operations,
Set<ASTNode> nodesprocessed, ChangeBehavior cb, ClassInstanceCreation visited,
ReferenceHolder<ASTNode, Object> holder) {
operations.add(fixcore.rewrite(visited, cb, holder));
nodesprocessed.add(visited);
return false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore,
Set<CompilationUnitRewriteOperation> operations, Set<ASTNode> nodesprocessed, ChangeBehavior cb,
MethodInvocation visited, ReferenceHolder<ASTNode, Object> holder) {
List<ASTNode> arguments= visited.arguments();
if(nodesprocessed.contains(visited) || (arguments.size()>1)) {
return false;
}
switch (arguments.size()) {
case 1:
if(!(arguments.get(0) instanceof StringLiteral)) {
Expand All @@ -82,7 +79,6 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore,
return false;
}
operations.add(fixcore.rewrite(visited, cb, holder));
nodesprocessed.add(visited);
return false;
}

Expand Down

0 comments on commit 9efdff8

Please sign in to comment.