Skip to content

Commit

Permalink
extract common code to abstract class (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
carstenartur authored Dec 30, 2024
1 parent 49454d0 commit 8b09664
Show file tree
Hide file tree
Showing 25 changed files with 461 additions and 316 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import org.eclipse.core.filebuffers.FileBuffers;
Expand Down Expand Up @@ -93,13 +92,13 @@
import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
import org.eclipse.jdt.core.refactoring.CompilationUnitChange;
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
import org.eclipse.jdt.internal.corext.dom.AbortSearchException;
import org.eclipse.jdt.internal.corext.dom.ScopeAnalyzer;
import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperationWithSourceRange;
import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite;
import org.eclipse.jface.text.IDocument;
import org.eclipse.text.edits.TextEdit;
import org.eclipse.text.edits.TextEditGroup;
import org.sandbox.jdt.internal.common.ReferenceHolder;
import org.sandbox.jdt.internal.corext.fix.JUnitCleanUpFixCore;

/**
Expand Down Expand Up @@ -187,6 +186,9 @@ public static Collection<String> getUsedVariableNames(ASTNode node) {
return new ScopeAnalyzer(root).getUsedVariableNames(node.getStartPosition(), node.getLength());
}

abstract void process2Rewrite(TextEditGroup group, ASTRewrite rewriter, AST ast, ImportRewrite importRewriter,
JunitHolder mh);

private void addContextArgumentIfMissing(ASTNode node, ASTRewrite rewriter, AST ast, TextEditGroup group) {
ListRewrite argsRewrite;
if (node instanceof MethodInvocation) {
Expand Down Expand Up @@ -1262,8 +1264,7 @@ public void reorderParameters(MethodInvocation node, ASTRewrite rewriter, TextEd
}
}

public abstract void rewrite(JUnitCleanUpFixCore useExplicitEncodingFixCore, T holder,
CompilationUnitRewrite cuRewrite, TextEditGroup group);


private void addTestNameField(TypeDeclaration parentClass, ASTRewrite rewriter, TextEditGroup group) {
AST ast= parentClass.getAST();
Expand Down Expand Up @@ -1402,4 +1403,13 @@ public boolean visit(MethodInvocation node) {
importRewrite.removeImport(ORG_JUNIT_RULE);
importRewrite.removeImport(ORG_JUNIT_RULES_TEST_NAME);
}

public void rewrite(JUnitCleanUpFixCore upp, ReferenceHolder<Integer, JunitHolder> hit, CompilationUnitRewrite cuRewrite, TextEditGroup group) {
ASTRewrite rewriter= cuRewrite.getASTRewrite();
AST ast= cuRewrite.getRoot().getAST();
ImportRewrite importRewriter= cuRewrite.getImportRewrite();
JunitHolder mh= hit.get(hit.size() - 1);
process2Rewrite(group, rewriter, ast, importRewriter, mh);
hit.remove(hit.size() - 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
* #L%
*/

import java.util.Map.Entry;
import java.util.Set;

import org.eclipse.jdt.core.dom.AST;
Expand All @@ -45,7 +44,6 @@
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
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.HelperVisitor;
import org.sandbox.jdt.internal.common.ReferenceHolder;
Expand Down Expand Up @@ -75,22 +73,16 @@ private boolean processFoundNode(JUnitCleanUpFixCore fixcore,
operations.add(fixcore.rewrite(dataholder));
return false;
}

@Override
public void rewrite(JUnitCleanUpFixCore upp, final ReferenceHolder<Integer, JunitHolder> hit,
final CompilationUnitRewrite cuRewrite, TextEditGroup group) {
ASTRewrite rewrite= cuRewrite.getASTRewrite();
AST ast= cuRewrite.getRoot().getAST();
ImportRewrite importRewriter= cuRewrite.getImportRewrite();
for (Entry<Integer, JunitHolder> entry : hit.entrySet()) {
JunitHolder mh= entry.getValue();
Annotation minv= mh.getAnnotation();
MarkerAnnotation newAnnotation= ast.newMarkerAnnotation();
newAnnotation.setTypeName(ast.newSimpleName(ANNOTATION_AFTER_ALL));
importRewriter.addImport(ORG_JUNIT_JUPITER_API_AFTER_ALL);
ASTNodes.replaceButKeepComment(rewrite, minv, newAnnotation, group);
importRewriter.removeImport(ORG_JUNIT_AFTERCLASS);
}
void process2Rewrite(TextEditGroup group, ASTRewrite rewriter, AST ast, ImportRewrite importRewriter,
JunitHolder mh) {
Annotation minv= mh.getAnnotation();
MarkerAnnotation newAnnotation= ast.newMarkerAnnotation();
newAnnotation.setTypeName(ast.newSimpleName(ANNOTATION_AFTER_ALL));
importRewriter.addImport(ORG_JUNIT_JUPITER_API_AFTER_ALL);
ASTNodes.replaceButKeepComment(rewriter, minv, newAnnotation, group);
importRewriter.removeImport(ORG_JUNIT_AFTERCLASS);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
* #L%
*/

import java.util.Map.Entry;
import java.util.Set;

import org.eclipse.jdt.core.dom.AST;
Expand All @@ -45,7 +44,6 @@
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
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.HelperVisitor;
import org.sandbox.jdt.internal.common.ReferenceHolder;
Expand Down Expand Up @@ -77,22 +75,16 @@ private boolean processFoundNode(JUnitCleanUpFixCore fixcore,
}

@Override
public void rewrite(JUnitCleanUpFixCore upp, final ReferenceHolder<Integer, JunitHolder> hit,
final CompilationUnitRewrite cuRewrite, TextEditGroup group) {
ASTRewrite rewrite= cuRewrite.getASTRewrite();
AST ast= cuRewrite.getRoot().getAST();
ImportRewrite importRewriter= cuRewrite.getImportRewrite();
for (Entry<Integer, JunitHolder> entry : hit.entrySet()) {
JunitHolder mh= entry.getValue();
Annotation minv= mh.getAnnotation();
MarkerAnnotation newAnnotation= ast.newMarkerAnnotation();
newAnnotation.setTypeName(ast.newSimpleName(ANNOTATION_AFTER_EACH));
importRewriter.addImport(ORG_JUNIT_JUPITER_API_AFTER_EACH);
ASTNodes.replaceButKeepComment(rewrite, minv, newAnnotation, group);
importRewriter.removeImport(ORG_JUNIT_AFTER);
}
void process2Rewrite(TextEditGroup group, ASTRewrite rewriter, AST ast, ImportRewrite importRewriter,
JunitHolder mh) {
Annotation minv= mh.getAnnotation();
MarkerAnnotation newAnnotation= ast.newMarkerAnnotation();
newAnnotation.setTypeName(ast.newSimpleName(ANNOTATION_AFTER_EACH));
importRewriter.addImport(ORG_JUNIT_JUPITER_API_AFTER_EACH);
ASTNodes.replaceButKeepComment(rewriter, minv, newAnnotation, group);
importRewriter.removeImport(ORG_JUNIT_AFTER);
}

@Override
public String getPreview(boolean afterRefactoring) {
if (afterRefactoring) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
* #L%
*/

import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand All @@ -50,7 +49,6 @@
import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
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.HelperVisitor;
import org.sandbox.jdt.internal.common.ReferenceHolder;
Expand Down Expand Up @@ -97,45 +95,39 @@ private boolean processFoundNode(JUnitCleanUpFixCore fixcore,
}

@Override
public void rewrite(JUnitCleanUpFixCore upp, final ReferenceHolder<Integer, JunitHolder> hit,
final CompilationUnitRewrite cuRewrite, TextEditGroup group) {
ASTRewrite rewrite= cuRewrite.getASTRewrite();
AST ast= cuRewrite.getRoot().getAST();
ImportRewrite importRewrite= cuRewrite.getImportRewrite();
for (Entry<Integer, JunitHolder> entry : hit.entrySet()) {
JunitHolder mh= entry.getValue();
if (mh.minv instanceof MethodInvocation) {
MethodInvocation node= mh.getMethodInvocation();
Expression assertexpression= node.getExpression();
if ("assertThat".equals(node.getName().getIdentifier()) &&
assertexpression instanceof SimpleName &&
"Assert".equals(((SimpleName) assertexpression).getIdentifier())) {
rewrite.set(node, MethodInvocation.EXPRESSION_PROPERTY, null, group);
importRewrite.addStaticImport("org.hamcrest.MatcherAssert", "assertThat", false);
importRewrite.removeImport("org.junit.Assert");
if (node.arguments().size() == 3) {
Expression errorMessage = (Expression) node.arguments().get(0);
Expression actualValue = (Expression) node.arguments().get(1);
Expression matcher = (Expression) node.arguments().get(2);
ListRewrite argumentRewrite = rewrite.getListRewrite(node, MethodInvocation.ARGUMENTS_PROPERTY);
argumentRewrite.replace((ASTNode) node.arguments().get(0), errorMessage, group);
argumentRewrite.replace((ASTNode) node.arguments().get(1), actualValue, group);
argumentRewrite.replace((ASTNode) node.arguments().get(2), matcher, group);
}
} else {
reorderParameters(node, rewrite, group, oneparam, twoparam);
SimpleName newQualifier= ast.newSimpleName(ASSERTIONS);
Expression expression= assertexpression;
if (expression != null) {
ASTNodes.replaceButKeepComment(rewrite, expression, newQualifier, group);
}
void process2Rewrite(TextEditGroup group, ASTRewrite rewriter, AST ast, ImportRewrite importRewriter,
JunitHolder mh) {
if (mh.minv instanceof MethodInvocation) {
MethodInvocation node= mh.getMethodInvocation();
Expression assertexpression= node.getExpression();
if ("assertThat".equals(node.getName().getIdentifier()) &&
assertexpression instanceof SimpleName &&
"Assert".equals(((SimpleName) assertexpression).getIdentifier())) {
rewriter.set(node, MethodInvocation.EXPRESSION_PROPERTY, null, group);
importRewriter.addStaticImport("org.hamcrest.MatcherAssert", "assertThat", false);
importRewriter.removeImport("org.junit.Assert");
if (node.arguments().size() == 3) {
Expression errorMessage = (Expression) node.arguments().get(0);
Expression actualValue = (Expression) node.arguments().get(1);
Expression matcher = (Expression) node.arguments().get(2);
ListRewrite argumentRewrite = rewriter.getListRewrite(node, MethodInvocation.ARGUMENTS_PROPERTY);
argumentRewrite.replace((ASTNode) node.arguments().get(0), errorMessage, group);
argumentRewrite.replace((ASTNode) node.arguments().get(1), actualValue, group);
argumentRewrite.replace((ASTNode) node.arguments().get(2), matcher, group);
}
} else {
changeImportDeclaration(mh.getImportDeclaration(), importRewrite, group);
reorderParameters(node, rewriter, group, oneparam, twoparam);
SimpleName newQualifier= ast.newSimpleName(ASSERTIONS);
Expression expression= assertexpression;
if (expression != null) {
ASTNodes.replaceButKeepComment(rewriter, expression, newQualifier, group);
}
}
} else {
changeImportDeclaration(mh.getImportDeclaration(), importRewriter, group);
}
}

public void changeImportDeclaration(ImportDeclaration node, ImportRewrite importRewriter, TextEditGroup group) {
String importName= node.getName().getFullyQualifiedName();
if (node.isStatic() && importName.equals(ORG_JUNIT_ASSERT)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
* #L%
*/

import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand All @@ -50,7 +49,6 @@
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
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.HelperVisitor;
import org.sandbox.jdt.internal.common.ReferenceHolder;
Expand Down Expand Up @@ -93,37 +91,31 @@ private boolean processFoundNode(JUnitCleanUpFixCore fixcore,
operations.add(fixcore.rewrite(dataholder));
return false;
}

@Override
public void rewrite(JUnitCleanUpFixCore upp, final ReferenceHolder<Integer, JunitHolder> hit,
final CompilationUnitRewrite cuRewrite, TextEditGroup group) {
ASTRewrite rewrite= cuRewrite.getASTRewrite();
AST ast= cuRewrite.getRoot().getAST();
ImportRewrite importRewriter= cuRewrite.getImportRewrite();
for (Entry<Integer, JunitHolder> entry : hit.entrySet()) {
JunitHolder mh= entry.getValue();
if (mh.minv instanceof MethodInvocation) {
MethodInvocation minv= mh.getMethodInvocation();
if ("assumeThat".equals(minv.getName().getIdentifier()) && isJUnitAssume(minv)) {
importRewriter.addStaticImport("org.hamcrest.junit.MatcherAssume", "assumeThat", true);
importRewriter.removeStaticImport("org.junit.Assume.assumeThat");
MethodInvocation newAssumeThatCall = ast.newMethodInvocation();
newAssumeThatCall.setName(ast.newSimpleName("assumeThat"));
for (Object arg : minv.arguments()) {
newAssumeThatCall.arguments().add(rewrite.createCopyTarget((ASTNode) arg));
}
ASTNodes.replaceButKeepComment(rewrite,minv, newAssumeThatCall, group);
} else {
reorderParameters(minv, rewrite, group, oneparam, twoparam);
SimpleName newQualifier= ast.newSimpleName(ASSUMPTIONS);
Expression expression= minv.getExpression();
if (expression != null) {
ASTNodes.replaceButKeepComment(rewrite, expression, newQualifier, group);
}
void process2Rewrite(TextEditGroup group, ASTRewrite rewriter, AST ast, ImportRewrite importRewriter,
JunitHolder mh) {
if (mh.minv instanceof MethodInvocation) {
MethodInvocation minv= mh.getMethodInvocation();
if ("assumeThat".equals(minv.getName().getIdentifier()) && isJUnitAssume(minv)) {
importRewriter.addStaticImport("org.hamcrest.junit.MatcherAssume", "assumeThat", true);
importRewriter.removeStaticImport("org.junit.Assume.assumeThat");
MethodInvocation newAssumeThatCall = ast.newMethodInvocation();
newAssumeThatCall.setName(ast.newSimpleName("assumeThat"));
for (Object arg : minv.arguments()) {
newAssumeThatCall.arguments().add(rewriter.createCopyTarget((ASTNode) arg));
}
ASTNodes.replaceButKeepComment(rewriter,minv, newAssumeThatCall, group);
} else {
changeImportDeclaration(mh.getImportDeclaration(), importRewriter, group);
reorderParameters(minv, rewriter, group, oneparam, twoparam);
SimpleName newQualifier= ast.newSimpleName(ASSUMPTIONS);
Expression expression= minv.getExpression();
if (expression != null) {
ASTNodes.replaceButKeepComment(rewriter, expression, newQualifier, group);
}
}
} else {
changeImportDeclaration(mh.getImportDeclaration(), importRewriter, group);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
* #L%
*/

import java.util.Map.Entry;
import java.util.Set;

import org.eclipse.jdt.core.dom.AST;
Expand All @@ -45,7 +44,6 @@
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
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.HelperVisitor;
import org.sandbox.jdt.internal.common.ReferenceHolder;
Expand Down Expand Up @@ -77,22 +75,16 @@ private boolean processFoundNode(JUnitCleanUpFixCore fixcore,
}

@Override
public void rewrite(JUnitCleanUpFixCore upp, final ReferenceHolder<Integer, JunitHolder> hit,
final CompilationUnitRewrite cuRewrite, TextEditGroup group) {
ASTRewrite rewrite= cuRewrite.getASTRewrite();
AST ast= cuRewrite.getRoot().getAST();
ImportRewrite importRewriter= cuRewrite.getImportRewrite();
for (Entry<Integer, JunitHolder> entry : hit.entrySet()) {
JunitHolder mh= entry.getValue();
Annotation minv= mh.getAnnotation();
MarkerAnnotation newAnnotation= ast.newMarkerAnnotation();
newAnnotation.setTypeName(ast.newSimpleName(ANNOTATION_BEFORE_ALL));
importRewriter.addImport(ORG_JUNIT_JUPITER_API_BEFORE_ALL);
ASTNodes.replaceButKeepComment(rewrite, minv, newAnnotation, group);
importRewriter.removeImport(ORG_JUNIT_BEFORECLASS);
}
void process2Rewrite(TextEditGroup group, ASTRewrite rewriter, AST ast, ImportRewrite importRewriter,
JunitHolder mh) {
Annotation minv= mh.getAnnotation();
MarkerAnnotation newAnnotation= ast.newMarkerAnnotation();
newAnnotation.setTypeName(ast.newSimpleName(ANNOTATION_BEFORE_ALL));
importRewriter.addImport(ORG_JUNIT_JUPITER_API_BEFORE_ALL);
ASTNodes.replaceButKeepComment(rewriter, minv, newAnnotation, group);
importRewriter.removeImport(ORG_JUNIT_BEFORECLASS);
}

@Override
public String getPreview(boolean afterRefactoring) {
if (afterRefactoring) {
Expand Down
Loading

0 comments on commit 8b09664

Please sign in to comment.