Skip to content

Commit

Permalink
add processing for @before
Browse files Browse the repository at this point in the history
  • Loading branch information
carstenartur committed Oct 13, 2024
1 parent e000d49 commit 09f2a16
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public class HelperVisitor<E extends HelperVisitorProvider<V, T, E>,V,T> {
*
*/
public static final String METHODNAME = "methodname"; //$NON-NLS-1$
/**
*
*/
public static final String ANNOTATIONNAME = "annotationname"; //$NON-NLS-1$

/**
*
Expand Down Expand Up @@ -637,6 +641,18 @@ public BiPredicate<? extends ASTNode, E> addMarkerAnnotation(BiPredicate<MarkerA
return predicatemap.put(VisitorEnum.MarkerAnnotation, bs);
}

/**
* @param name
* @param bs
* @return old BiPredicate assigned for nodetype
*/
public BiPredicate<? extends ASTNode, E> addMarkerAnnotation(String name, BiPredicate<MarkerAnnotation, E> bs) {
predicatedata.put(VisitorEnum.MarkerAnnotation, Map.ofEntries(
new AbstractMap.SimpleEntry<>(ANNOTATIONNAME, name)
));
return predicatemap.put(VisitorEnum.MarkerAnnotation, bs);
}

/**
*
* @param bs
Expand Down Expand Up @@ -4184,6 +4200,23 @@ public static <V, T> void callMarkerAnnotationVisitor(ASTNode node, ReferenceHol
hv.build(node);
}

/**
* @param <V>
* @param <T>
* @param name
* @param node
* @param dataholder
* @param nodesprocessed
* @param bs
*/
public static <V, T> void callMarkerAnnotationVisitor(String name, ASTNode node, ReferenceHolder<V, T> dataholder, Set<ASTNode> nodesprocessed,
BiPredicate<MarkerAnnotation, ReferenceHolder<V, T>> bs) {

HelperVisitor<ReferenceHolder<V, T>,V,T> hv= new HelperVisitor<>(nodesprocessed, dataholder);
hv.addMarkerAnnotation(name, bs);
hv.build(node);
}

/**
*
* @param <V>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,9 +458,22 @@ public boolean visit(LineComment node) {

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


if (this.helperVisitor.predicatemap.containsKey(VisitorEnum.MarkerAnnotation)) {
return ((BiPredicate<MarkerAnnotation, E>) (this.helperVisitor.predicatemap.get(VisitorEnum.MarkerAnnotation)))
.test(node, this.helperVisitor.dataholder);
Map<String, Object> map=(Map<String, Object>) this.helperVisitor.getSupplierData().get(VisitorEnum.MarkerAnnotation);
if(map != null) {
String data=(String) map.get(HelperVisitor.ANNOTATIONNAME);
if ((data!= null) && !node.getTypeName().getFullyQualifiedName().equals(data)) {
return true;
}
}
return ((BiPredicate<MarkerAnnotation, E>) (this.helperVisitor.predicatemap.get(VisitorEnum.MarkerAnnotation))).test(node, this.helperVisitor.dataholder);
}
return true;
}
Expand Down Expand Up @@ -1377,7 +1390,19 @@ public void endVisit(LineComment node) {

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

if (this.helperVisitor.consumermap.containsKey(VisitorEnum.MarkerAnnotation)) {
Map<String, Object> map=(Map<String, Object>) this.helperVisitor.getConsumerData().get(VisitorEnum.MarkerAnnotation);
if(map != null) {
String data=(String) map.get(HelperVisitor.ANNOTATIONNAME);
if ((data!= null) && !node.getTypeName().getFullyQualifiedName().equals(data)) {
return;
}
}
((BiConsumer<MarkerAnnotation, E>) (this.helperVisitor.consumermap.get(VisitorEnum.MarkerAnnotation))).accept(node,
this.helperVisitor.dataholder);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@
import org.eclipse.text.edits.TextEditGroup;
import org.sandbox.jdt.internal.common.ReferenceHolder;
import org.sandbox.jdt.internal.corext.fix.helper.AbstractTool;
import org.sandbox.jdt.internal.corext.fix.helper.JUnitPlugin;
import org.sandbox.jdt.internal.corext.fix.helper.JUnitPlugin.MonitorHolder;
import org.sandbox.jdt.internal.corext.fix.helper.BeforeJUnitPlugin;
import org.sandbox.jdt.internal.corext.fix.helper.BeforeJUnitPlugin.JunitHolder;
import org.sandbox.jdt.internal.ui.fix.MultiFixMessages;

public enum JUnitCleanUpFixCore {

MONITOR(new JUnitPlugin());
MONITOR(new BeforeJUnitPlugin());

AbstractTool<ReferenceHolder<Integer, MonitorHolder>> junitfound;
AbstractTool<ReferenceHolder<Integer, JunitHolder>> junitfound;

@SuppressWarnings("unchecked")
JUnitCleanUpFixCore(AbstractTool<? extends ReferenceHolder<Integer, MonitorHolder>> xmlsimplify) {
this.junitfound= (AbstractTool<ReferenceHolder<Integer, MonitorHolder>>) xmlsimplify;
JUnitCleanUpFixCore(AbstractTool<? extends ReferenceHolder<Integer, JunitHolder>> xmlsimplify) {
this.junitfound= (AbstractTool<ReferenceHolder<Integer, JunitHolder>>) xmlsimplify;
}

public String getPreview(boolean i) {
Expand All @@ -57,12 +57,11 @@ public String getPreview(boolean i) {
* loop var used within
*/
public void findOperations(final CompilationUnit compilationUnit,
final Set<CompilationUnitRewriteOperationWithSourceRange> operations, final Set<ASTNode> nodesprocessed,
boolean createForOnlyIfVarUsed) {
junitfound.find(this, compilationUnit, operations, nodesprocessed, createForOnlyIfVarUsed);
final Set<CompilationUnitRewriteOperationWithSourceRange> operations, final Set<ASTNode> nodesprocessed) {
junitfound.find(this, compilationUnit, operations, nodesprocessed);
}

public CompilationUnitRewriteOperationWithSourceRange rewrite(final ReferenceHolder<Integer, MonitorHolder> hit) {
public CompilationUnitRewriteOperationWithSourceRange rewrite(final ReferenceHolder<Integer, JunitHolder> hit) {
return new CompilationUnitRewriteOperationWithSourceRange() {
@Override
public void rewriteASTInternal(final CompilationUnitRewrite cuRewrite, final LinkedProposalModelCore linkedModel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ protected static boolean isOfType(ITypeBinding typeBinding, String typename) {
}

public abstract void find(JUnitCleanUpFixCore fixcore, CompilationUnit compilationUnit,
Set<CompilationUnitRewriteOperationWithSourceRange> operations, Set<ASTNode> nodesprocessed,
boolean createForIfVarNotUsed);
Set<CompilationUnitRewriteOperationWithSourceRange> operations, Set<ASTNode> nodesprocessed);

public abstract void rewrite(JUnitCleanUpFixCore useExplicitEncodingFixCore, T holder,
CompilationUnitRewrite cuRewrite, TextEditGroup group);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,60 +15,81 @@

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

import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.MarkerAnnotation;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
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;
import org.sandbox.jdt.internal.corext.fix.JUnitCleanUpFixCore;

/**
*
*
*/
public class JUnitPlugin extends
AbstractTool<ReferenceHolder<Integer, JUnitPlugin.MonitorHolder>> {
public class BeforeJUnitPlugin extends AbstractTool<ReferenceHolder<Integer, BeforeJUnitPlugin.JunitHolder>> {

public static class MonitorHolder {
public MethodInvocation minv;
public static class JunitHolder {
public MarkerAnnotation minv;
public String minvname;
public Set<ClassInstanceCreation> setofcic = new HashSet<>();
public Set<ASTNode> nodesprocessed;
}

@Override
public void find(JUnitCleanUpFixCore fixcore, CompilationUnit compilationUnit,
Set<CompilationUnitRewriteOperationWithSourceRange> operations, Set<ASTNode> nodesprocessed,
boolean createForOnlyIfVarUsed) {
ReferenceHolder<Integer, MonitorHolder> dataholder = new ReferenceHolder<>();
Set<CompilationUnitRewriteOperationWithSourceRange> operations, Set<ASTNode> nodesprocessed) {
ReferenceHolder<Integer, JunitHolder> dataholder = new ReferenceHolder<>();
HelperVisitor.callMarkerAnnotationVisitor("Before", compilationUnit, dataholder, nodesprocessed,
(visited, aholder) -> processFoundNode(fixcore, operations, visited, aholder));
}

private boolean processFoundNode(JUnitCleanUpFixCore fixcore,
Set<CompilationUnitRewriteOperationWithSourceRange> operations, MarkerAnnotation node,
ReferenceHolder<Integer, JunitHolder> dataholder) {
JunitHolder mh = new JunitHolder();
mh.minv = node;
mh.minvname = node.getTypeName().getFullyQualifiedName();
dataholder.put(dataholder.size(), mh);
operations.add(fixcore.rewrite(dataholder));
return false;
}

@Override
public void rewrite(JUnitCleanUpFixCore upp, final ReferenceHolder<Integer, MonitorHolder> hit,
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 importRemover = cuRewrite.getImportRewrite();

for (Entry<Integer, JunitHolder> entry : hit.entrySet()) {
JunitHolder mh = entry.getValue();
MarkerAnnotation minv = mh.minv;
MarkerAnnotation newAnnotation = ast.newMarkerAnnotation();
newAnnotation.setTypeName(ast.newSimpleName("BeforeEach"));
addImport("org.junit.jupiter.api.BeforeEach", cuRewrite, ast);
ASTNodes.replaceButKeepComment(rewrite, minv, newAnnotation, group);
importRemover.removeImport("org.junit.Before");
}
}

@Override
public String getPreview(boolean afterRefactoring) {
if (!afterRefactoring) {
return """
;
"""; //$NON-NLS-1$
;
"""; //$NON-NLS-1$
}
return """
;
"""; //$NON-NLS-1$
;
"""; //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ public ICleanUpFix createFix(final CleanUpContext context) throws CoreException
}
Set<CompilationUnitRewriteOperationWithSourceRange> operations= new LinkedHashSet<>();
Set<ASTNode> nodesprocessed= new HashSet<>();
computeFixSet.forEach(i -> i.findOperations(compilationUnit, operations, nodesprocessed,
true));
computeFixSet.forEach(i -> i.findOperations(compilationUnit, operations, nodesprocessed));
if (operations.isEmpty()) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ public void test() {
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
/**
*
Expand All @@ -113,7 +113,7 @@ public static void tearDownAfterClass() throws Exception {
/**
* @throws java.lang.Exception
*/
@Before
@BeforeEach
public void setUp() throws Exception {
}
Expand Down Expand Up @@ -188,9 +188,9 @@ public void test() {
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
/**
*
Expand All @@ -214,7 +214,7 @@ public static void tearDownAfterClass() throws Exception {
/**
* @throws java.lang.Exception
*/
@Before
@BeforeEach
public void setUp() throws Exception {
}
Expand Down

0 comments on commit 09f2a16

Please sign in to comment.