From 09f2a16188afabe1ae8baf8ac1ee90cdb9680365 Mon Sep 17 00:00:00 2001 From: Carsten Hammer Date: Sun, 13 Oct 2024 14:17:30 +0200 Subject: [PATCH] add processing for @Before --- .../jdt/internal/common/HelperVisitor.java | 33 +++++++++++++ .../jdt/internal/common/LambdaASTVisitor.java | 29 ++++++++++- .../corext/fix/JUnitCleanUpFixCore.java | 19 ++++--- .../corext/fix/helper/AbstractTool.java | 3 +- ...UnitPlugin.java => BeforeJUnitPlugin.java} | 49 +++++++++++++------ .../jdt/internal/ui/fix/JUnitCleanUpCore.java | 3 +- .../ui/tests/quickfix/Java8CleanUpTest.java | 8 +-- 7 files changed, 110 insertions(+), 34 deletions(-) rename sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/{JUnitPlugin.java => BeforeJUnitPlugin.java} (56%) diff --git a/sandbox_common/src/org/sandbox/jdt/internal/common/HelperVisitor.java b/sandbox_common/src/org/sandbox/jdt/internal/common/HelperVisitor.java index 0dd2c0b1..4cba65a9 100644 --- a/sandbox_common/src/org/sandbox/jdt/internal/common/HelperVisitor.java +++ b/sandbox_common/src/org/sandbox/jdt/internal/common/HelperVisitor.java @@ -45,6 +45,10 @@ public class HelperVisitor,V,T> { * */ public static final String METHODNAME = "methodname"; //$NON-NLS-1$ + /** + * + */ + public static final String ANNOTATIONNAME = "annotationname"; //$NON-NLS-1$ /** * @@ -637,6 +641,18 @@ public BiPredicate addMarkerAnnotation(BiPredicate addMarkerAnnotation(String name, BiPredicate bs) { + predicatedata.put(VisitorEnum.MarkerAnnotation, Map.ofEntries( + new AbstractMap.SimpleEntry<>(ANNOTATIONNAME, name) + )); + return predicatemap.put(VisitorEnum.MarkerAnnotation, bs); + } + /** * * @param bs @@ -4184,6 +4200,23 @@ public static void callMarkerAnnotationVisitor(ASTNode node, ReferenceHol hv.build(node); } + /** + * @param + * @param + * @param name + * @param node + * @param dataholder + * @param nodesprocessed + * @param bs + */ + public static void callMarkerAnnotationVisitor(String name, ASTNode node, ReferenceHolder dataholder, Set nodesprocessed, + BiPredicate> bs) { + + HelperVisitor,V,T> hv= new HelperVisitor<>(nodesprocessed, dataholder); + hv.addMarkerAnnotation(name, bs); + hv.build(node); + } + /** * * @param diff --git a/sandbox_common/src/org/sandbox/jdt/internal/common/LambdaASTVisitor.java b/sandbox_common/src/org/sandbox/jdt/internal/common/LambdaASTVisitor.java index f95b2f11..55fb9502 100644 --- a/sandbox_common/src/org/sandbox/jdt/internal/common/LambdaASTVisitor.java +++ b/sandbox_common/src/org/sandbox/jdt/internal/common/LambdaASTVisitor.java @@ -458,9 +458,22 @@ public boolean visit(LineComment node) { @Override public boolean visit(MarkerAnnotation node) { +// if (this.helperVisitor.predicatemap.containsKey(VisitorEnum.MarkerAnnotation)) { +// return ((BiPredicate) (this.helperVisitor.predicatemap.get(VisitorEnum.MarkerAnnotation))) +// .test(node, this.helperVisitor.dataholder); +// } +// return true; + + if (this.helperVisitor.predicatemap.containsKey(VisitorEnum.MarkerAnnotation)) { - return ((BiPredicate) (this.helperVisitor.predicatemap.get(VisitorEnum.MarkerAnnotation))) - .test(node, this.helperVisitor.dataholder); + Map map=(Map) 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) (this.helperVisitor.predicatemap.get(VisitorEnum.MarkerAnnotation))).test(node, this.helperVisitor.dataholder); } return true; } @@ -1377,7 +1390,19 @@ public void endVisit(LineComment node) { @Override public void endVisit(MarkerAnnotation node) { +// if (this.helperVisitor.consumermap.containsKey(VisitorEnum.MarkerAnnotation)) { +// ((BiConsumer) (this.helperVisitor.consumermap.get(VisitorEnum.MarkerAnnotation))).accept(node, +// this.helperVisitor.dataholder); +// } + if (this.helperVisitor.consumermap.containsKey(VisitorEnum.MarkerAnnotation)) { + Map map=(Map) 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) (this.helperVisitor.consumermap.get(VisitorEnum.MarkerAnnotation))).accept(node, this.helperVisitor.dataholder); } diff --git a/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/JUnitCleanUpFixCore.java b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/JUnitCleanUpFixCore.java index 638c1309..b30a149b 100644 --- a/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/JUnitCleanUpFixCore.java +++ b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/JUnitCleanUpFixCore.java @@ -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> junitfound; + AbstractTool> junitfound; @SuppressWarnings("unchecked") - JUnitCleanUpFixCore(AbstractTool> xmlsimplify) { - this.junitfound= (AbstractTool>) xmlsimplify; + JUnitCleanUpFixCore(AbstractTool> xmlsimplify) { + this.junitfound= (AbstractTool>) xmlsimplify; } public String getPreview(boolean i) { @@ -57,12 +57,11 @@ public String getPreview(boolean i) { * loop var used within */ public void findOperations(final CompilationUnit compilationUnit, - final Set operations, final Set nodesprocessed, - boolean createForOnlyIfVarUsed) { - junitfound.find(this, compilationUnit, operations, nodesprocessed, createForOnlyIfVarUsed); + final Set operations, final Set nodesprocessed) { + junitfound.find(this, compilationUnit, operations, nodesprocessed); } - public CompilationUnitRewriteOperationWithSourceRange rewrite(final ReferenceHolder hit) { + public CompilationUnitRewriteOperationWithSourceRange rewrite(final ReferenceHolder hit) { return new CompilationUnitRewriteOperationWithSourceRange() { @Override public void rewriteASTInternal(final CompilationUnitRewrite cuRewrite, final LinkedProposalModelCore linkedModel) diff --git a/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/AbstractTool.java b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/AbstractTool.java index afcd2687..668a66a7 100644 --- a/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/AbstractTool.java +++ b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/AbstractTool.java @@ -44,8 +44,7 @@ protected static boolean isOfType(ITypeBinding typeBinding, String typename) { } public abstract void find(JUnitCleanUpFixCore fixcore, CompilationUnit compilationUnit, - Set operations, Set nodesprocessed, - boolean createForIfVarNotUsed); + Set operations, Set nodesprocessed); public abstract void rewrite(JUnitCleanUpFixCore useExplicitEncodingFixCore, T holder, CompilationUnitRewrite cuRewrite, TextEditGroup group); diff --git a/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/JUnitPlugin.java b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/BeforeJUnitPlugin.java similarity index 56% rename from sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/JUnitPlugin.java rename to sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/BeforeJUnitPlugin.java index 0a1a12e1..97846b0f 100644 --- a/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/JUnitPlugin.java +++ b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/BeforeJUnitPlugin.java @@ -15,17 +15,20 @@ 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; @@ -33,11 +36,10 @@ * * */ -public class JUnitPlugin extends -AbstractTool> { +public class BeforeJUnitPlugin extends AbstractTool> { - public static class MonitorHolder { - public MethodInvocation minv; + public static class JunitHolder { + public MarkerAnnotation minv; public String minvname; public Set setofcic = new HashSet<>(); public Set nodesprocessed; @@ -45,30 +47,49 @@ public static class MonitorHolder { @Override public void find(JUnitCleanUpFixCore fixcore, CompilationUnit compilationUnit, - Set operations, Set nodesprocessed, - boolean createForOnlyIfVarUsed) { - ReferenceHolder dataholder = new ReferenceHolder<>(); + Set operations, Set nodesprocessed) { + ReferenceHolder dataholder = new ReferenceHolder<>(); + HelperVisitor.callMarkerAnnotationVisitor("Before", compilationUnit, dataholder, nodesprocessed, + (visited, aholder) -> processFoundNode(fixcore, operations, visited, aholder)); + } + private boolean processFoundNode(JUnitCleanUpFixCore fixcore, + Set operations, MarkerAnnotation node, + ReferenceHolder 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 hit, + public void rewrite(JUnitCleanUpFixCore upp, final ReferenceHolder hit, final CompilationUnitRewrite cuRewrite, TextEditGroup group) { ASTRewrite rewrite = cuRewrite.getASTRewrite(); AST ast = cuRewrite.getRoot().getAST(); ImportRewrite importRemover = cuRewrite.getImportRewrite(); - + for (Entry 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$ } } diff --git a/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/fix/JUnitCleanUpCore.java b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/fix/JUnitCleanUpCore.java index 7a155d5a..c0c0e80f 100644 --- a/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/fix/JUnitCleanUpCore.java +++ b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/fix/JUnitCleanUpCore.java @@ -71,8 +71,7 @@ public ICleanUpFix createFix(final CleanUpContext context) throws CoreException } Set operations= new LinkedHashSet<>(); Set 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; } diff --git a/sandbox_junit_cleanup_test/src/org/sandbox/jdt/ui/tests/quickfix/Java8CleanUpTest.java b/sandbox_junit_cleanup_test/src/org/sandbox/jdt/ui/tests/quickfix/Java8CleanUpTest.java index e9d02b48..f3963891 100644 --- a/sandbox_junit_cleanup_test/src/org/sandbox/jdt/ui/tests/quickfix/Java8CleanUpTest.java +++ b/sandbox_junit_cleanup_test/src/org/sandbox/jdt/ui/tests/quickfix/Java8CleanUpTest.java @@ -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; /** * @@ -113,7 +113,7 @@ public static void tearDownAfterClass() throws Exception { /** * @throws java.lang.Exception */ - @Before + @BeforeEach public void setUp() throws Exception { } @@ -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; /** * @@ -214,7 +214,7 @@ public static void tearDownAfterClass() throws Exception { /** * @throws java.lang.Exception */ - @Before + @BeforeEach public void setUp() throws Exception { }