diff --git a/pom.xml b/pom.xml index 450358d5..206c0c7b 100644 --- a/pom.xml +++ b/pom.xml @@ -44,6 +44,9 @@ sandbox_jface_cleanup sandbox_jface_cleanup_feature sandbox_jface_cleanup_test + sandbox_junit_cleanup + sandbox_junit_cleanup_feature + sandbox_junit_cleanup_test sandbox_usage_view sandbox_usage_view_feature sandbox_product @@ -322,6 +325,21 @@ sandbox_jface_cleanup_test ${project.version} + + org.sandbox + sandbox_junit_cleanup + ${project.version} + + + org.sandbox + sandbox_junit_cleanup_feature + ${project.version} + + + org.sandbox + sandbox_junit_cleanup_test + ${project.version} + org.sandbox sandbox_usage_view 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..f77039fe 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 @@ -765,6 +781,17 @@ public BiPredicate addNormalAnnotation(BiPredicate addNormalAnnotation(String name, BiPredicate bs) { + predicatedata.put(VisitorEnum.NormalAnnotation, Map.ofEntries( + new AbstractMap.SimpleEntry<>(ANNOTATIONNAME, name) + )); + return predicatemap.put(VisitorEnum.NormalAnnotation, bs); + } /** * * @param bs @@ -945,7 +972,19 @@ public BiPredicate addSimpleType(BiPredicate addSingleMemberAnnotation(BiPredicate bs) { return predicatemap.put(VisitorEnum.SingleMemberAnnotation, bs); } - + + /** + * @param name + * @param bs + * @return old BiPredicate assigned for nodetype + */ + public BiPredicate addSingleMemberAnnotation(String name, BiPredicate bs) { + predicatedata.put(VisitorEnum.SingleMemberAnnotation, Map.ofEntries( + new AbstractMap.SimpleEntry<>(ANNOTATIONNAME, name) + )); + return predicatemap.put(VisitorEnum.SingleMemberAnnotation, bs); + } + /** * * @param bs @@ -4184,6 +4223,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 @@ -4407,6 +4463,23 @@ public static void callNormalAnnotationVisitor(ASTNode node, ReferenceHol hv.build(node); } + /** + * @param + * @param + * @param name + * @param node + * @param dataholder + * @param nodesprocessed + * @param bs + */ + public static void callNormalAnnotationVisitor(String name, ASTNode node, ReferenceHolder dataholder, Set nodesprocessed, + BiPredicate> bs) { + + HelperVisitor,V,T> hv= new HelperVisitor<>(nodesprocessed, dataholder); + hv.addNormalAnnotation(name, bs); + hv.build(node); + } + /** * * @param @@ -4733,6 +4806,23 @@ public static void callSingleMemberAnnotationVisitor(ASTNode node, Refere hv.build(node); } + /** + * @param + * @param + * @param name + * @param node + * @param dataholder + * @param nodesprocessed + * @param bs + */ + public static void callSingleMemberAnnotationVisitor(String name, ASTNode node, ReferenceHolder dataholder, Set nodesprocessed, + BiPredicate> bs) { + + HelperVisitor,V,T> hv= new HelperVisitor<>(nodesprocessed, dataholder); + hv.addSingleMemberAnnotation(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..9b859291 100644 --- a/sandbox_common/src/org/sandbox/jdt/internal/common/LambdaASTVisitor.java +++ b/sandbox_common/src/org/sandbox/jdt/internal/common/LambdaASTVisitor.java @@ -459,8 +459,21 @@ 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); + Map map=(Map) this.helperVisitor.getSupplierData().get(VisitorEnum.MarkerAnnotation); + if(map != null) { + String data=(String) map.get(HelperVisitor.ANNOTATIONNAME); + ITypeBinding binding = node.resolveTypeBinding(); + String fullyQualifiedName; + if (binding != null) { + fullyQualifiedName = binding.getQualifiedName(); + }else { + fullyQualifiedName = node.getTypeName().getFullyQualifiedName(); + } + if ((data!= null) && !fullyQualifiedName.equals(data)) { + return true; + } + } + return ((BiPredicate) (this.helperVisitor.predicatemap.get(VisitorEnum.MarkerAnnotation))).test(node, this.helperVisitor.dataholder); } return true; } @@ -577,8 +590,21 @@ public boolean visit(NameQualifiedType node) { @Override public boolean visit(NormalAnnotation node) { if (this.helperVisitor.predicatemap.containsKey(VisitorEnum.NormalAnnotation)) { - return ((BiPredicate) (this.helperVisitor.predicatemap.get(VisitorEnum.NormalAnnotation))) - .test(node, this.helperVisitor.dataholder); + Map map=(Map) this.helperVisitor.getSupplierData().get(VisitorEnum.NormalAnnotation); + if(map != null) { + String data=(String) map.get(HelperVisitor.ANNOTATIONNAME); + ITypeBinding binding = node.resolveTypeBinding(); + String fullyQualifiedName; + if (binding != null) { + fullyQualifiedName = binding.getQualifiedName(); + }else { + fullyQualifiedName = node.getTypeName().getFullyQualifiedName(); + } + if ((data!= null) && !fullyQualifiedName.equals(data)) { + return true; + } + } + return ((BiPredicate) (this.helperVisitor.predicatemap.get(VisitorEnum.NormalAnnotation))).test(node, this.helperVisitor.dataholder); } return true; } @@ -757,8 +783,21 @@ public boolean visit(SimpleType node) { @Override public boolean visit(SingleMemberAnnotation node) { if (this.helperVisitor.predicatemap.containsKey(VisitorEnum.SingleMemberAnnotation)) { - return ((BiPredicate) (this.helperVisitor.predicatemap - .get(VisitorEnum.SingleMemberAnnotation))).test(node, this.helperVisitor.dataholder); + Map map=(Map) this.helperVisitor.getSupplierData().get(VisitorEnum.SingleMemberAnnotation); + if(map != null) { + String data=(String) map.get(HelperVisitor.ANNOTATIONNAME); + ITypeBinding binding = node.resolveTypeBinding(); + String fullyQualifiedName; + if (binding != null) { + fullyQualifiedName = binding.getQualifiedName(); + }else { + fullyQualifiedName = node.getTypeName().getFullyQualifiedName(); + } + if ((data!= null) && !fullyQualifiedName.equals(data)) { + return true; + } + } + return ((BiPredicate) (this.helperVisitor.predicatemap.get(VisitorEnum.SingleMemberAnnotation))).test(node, this.helperVisitor.dataholder); } return true; } @@ -1378,6 +1417,20 @@ public void endVisit(LineComment node) { @Override public void endVisit(MarkerAnnotation node) { 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); + ITypeBinding binding = node.resolveTypeBinding(); + String fullyQualifiedName; + if (binding != null) { + fullyQualifiedName = binding.getQualifiedName(); + }else { + fullyQualifiedName = node.getTypeName().getFullyQualifiedName(); + } + if ((data!= null) && !fullyQualifiedName.equals(data)) { + return; + } + } ((BiConsumer) (this.helperVisitor.consumermap.get(VisitorEnum.MarkerAnnotation))).accept(node, this.helperVisitor.dataholder); } @@ -1476,6 +1529,20 @@ public void endVisit(NameQualifiedType node) { @Override public void endVisit(NormalAnnotation node) { if (this.helperVisitor.consumermap.containsKey(VisitorEnum.NormalAnnotation)) { + Map map=(Map) this.helperVisitor.getConsumerData().get(VisitorEnum.NormalAnnotation); + if(map != null) { + String data=(String) map.get(HelperVisitor.ANNOTATIONNAME); + ITypeBinding binding = node.resolveTypeBinding(); + String fullyQualifiedName; + if (binding != null) { + fullyQualifiedName = binding.getQualifiedName(); + }else { + fullyQualifiedName = node.getTypeName().getFullyQualifiedName(); + } + if ((data!= null) && !fullyQualifiedName.equals(data)) { + return; + } + } ((BiConsumer) (this.helperVisitor.consumermap.get(VisitorEnum.NormalAnnotation))).accept(node, this.helperVisitor.dataholder); } diff --git a/sandbox_common/src/org/sandbox/jdt/internal/corext/fix2/MYCleanUpConstants.java b/sandbox_common/src/org/sandbox/jdt/internal/corext/fix2/MYCleanUpConstants.java index 24813153..7c86c2b8 100644 --- a/sandbox_common/src/org/sandbox/jdt/internal/corext/fix2/MYCleanUpConstants.java +++ b/sandbox_common/src/org/sandbox/jdt/internal/corext/fix2/MYCleanUpConstants.java @@ -71,6 +71,10 @@ public class MYCleanUpConstants { * */ public static final String XML_CLEANUP= "cleanup.xmlcleanup"; //$NON-NLS-1$ + /** + * + */ + public static final String JUNIT_CLEANUP= "cleanup.junitcleanup"; //$NON-NLS-1$ /** * */ diff --git a/sandbox_jface_cleanup/.settings/org.eclipse.jdt.core.prefs b/sandbox_jface_cleanup/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 00000000..62ef3488 --- /dev/null +++ b/sandbox_jface_cleanup/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,9 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=17 +org.eclipse.jdt.core.compiler.compliance=17 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=17 diff --git a/sandbox_junit_cleanup/.classpath b/sandbox_junit_cleanup/.classpath new file mode 100644 index 00000000..7931ec26 --- /dev/null +++ b/sandbox_junit_cleanup/.classpath @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/sandbox_junit_cleanup/.gitignore b/sandbox_junit_cleanup/.gitignore new file mode 100644 index 00000000..b83d2226 --- /dev/null +++ b/sandbox_junit_cleanup/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/sandbox_junit_cleanup/.project b/sandbox_junit_cleanup/.project new file mode 100644 index 00000000..e0994cde --- /dev/null +++ b/sandbox_junit_cleanup/.project @@ -0,0 +1,34 @@ + + + sandbox_junit_cleanup + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.pde.ManifestBuilder + + + + + org.eclipse.pde.SchemaBuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.m2e.core.maven2Nature + org.eclipse.pde.PluginNature + org.eclipse.jdt.core.javanature + + diff --git a/sandbox_junit_cleanup/META-INF/MANIFEST.MF b/sandbox_junit_cleanup/META-INF/MANIFEST.MF new file mode 100644 index 00000000..983e3087 --- /dev/null +++ b/sandbox_junit_cleanup/META-INF/MANIFEST.MF @@ -0,0 +1,26 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: junit_cleanup +Bundle-SymbolicName: sandbox_junit_cleanup;singleton:=true +Bundle-Version: 1.1.0.qualifier +Bundle-RequiredExecutionEnvironment: JavaSE-17 +Require-Bundle: org.eclipse.core.runtime, + org.eclipse.core.filebuffers, + org.eclipse.core.filesystem, + org.eclipse.core.resources, + org.eclipse.core.expressions, + org.eclipse.jdt.core, + org.eclipse.jdt.ui, + org.eclipse.ltk.core.refactoring, + org.eclipse.text, + org.eclipse.swt, + org.eclipse.jface, + org.eclipse.ui.workbench, + org.eclipse.jdt.core.manipulation, + sandbox_common +Bundle-ActivationPolicy: lazy +Eclipse-RegisterBuddy: org.eclipse.jdt.core.manipulation +Import-Package: org.sandbox.jdt.internal.common, + org.sandbox.jdt.internal.corext.fix2 +Eclipse-BuddyPolicy: global +Automatic-Module-Name: sandbox.junit.cleanup diff --git a/sandbox_junit_cleanup/build.properties b/sandbox_junit_cleanup/build.properties new file mode 100644 index 00000000..8be33378 --- /dev/null +++ b/sandbox_junit_cleanup/build.properties @@ -0,0 +1,5 @@ +source.. = src/ +bin.includes = META-INF/,\ + .,\ + plugin.xml,\ + build.properties diff --git a/sandbox_junit_cleanup/plugin.xml b/sandbox_junit_cleanup/plugin.xml new file mode 100644 index 00000000..5b981c33 --- /dev/null +++ b/sandbox_junit_cleanup/plugin.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/sandbox_junit_cleanup/pom.xml b/sandbox_junit_cleanup/pom.xml new file mode 100644 index 00000000..f7ab6315 --- /dev/null +++ b/sandbox_junit_cleanup/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + org.sandbox + central + 1.1.0-SNAPSHOT + + sandbox_junit_cleanup + Sandbox junit cleanup + eclipse-plugin + + ${project.basedir}/src + + \ No newline at end of file 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 new file mode 100644 index 00000000..1d2f3586 --- /dev/null +++ b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/JUnitCleanUpFixCore.java @@ -0,0 +1,97 @@ +/******************************************************************************* + * Copyright (c) 2021 Carsten Hammer. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Carsten Hammer + *******************************************************************************/ +package org.sandbox.jdt.internal.corext.fix; + +import java.util.Set; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.jdt.core.dom.ASTNode; +import org.eclipse.jdt.core.dom.CompilationUnit; +import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; +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.refactoring.util.TightSourceRangeComputer; +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.AfterClassJUnitPlugin; +import org.sandbox.jdt.internal.corext.fix.helper.AfterJUnitPlugin; +import org.sandbox.jdt.internal.corext.fix.helper.BeforeClassJUnitPlugin; +import org.sandbox.jdt.internal.corext.fix.helper.BeforeJUnitPlugin; +import org.sandbox.jdt.internal.corext.fix.helper.IgnoreJUnitPlugin; +import org.sandbox.jdt.internal.corext.fix.helper.JunitHolder; +import org.sandbox.jdt.internal.corext.fix.helper.RunWithJUnitPlugin; +import org.sandbox.jdt.internal.corext.fix.helper.TestJUnitPlugin; +import org.sandbox.jdt.internal.ui.fix.MultiFixMessages; + +public enum JUnitCleanUpFixCore { + + BEFORE(new BeforeJUnitPlugin()), + AFTER(new AfterJUnitPlugin()), + TEST(new TestJUnitPlugin()), + BEFORECLASS(new BeforeClassJUnitPlugin()), + AFTERCLASS(new AfterClassJUnitPlugin()), + IGNORE(new IgnoreJUnitPlugin()) +// , +// RUNWITH(new RunWithJUnitPlugin()) + ; + + AbstractTool> junitfound; + + @SuppressWarnings("unchecked") + JUnitCleanUpFixCore(AbstractTool> xmlsimplify) { + this.junitfound= (AbstractTool>) xmlsimplify; + } + + public String getPreview(boolean i) { + return junitfound.getPreview(i); + } + + /** + * Compute set of CompilationUnitRewriteOperation to refactor supported + * situations + * + * @param compilationUnit unit to search in + * @param operations set of all CompilationUnitRewriteOperations + * created already + * @param nodesprocessed list to remember nodes already processed + * @param createForOnlyIfVarUsed true if for loop should be created only only if + * loop var used within + */ + public void findOperations(final CompilationUnit compilationUnit, + final Set operations, final Set nodesprocessed) { + junitfound.find(this, compilationUnit, operations, nodesprocessed); + } + + public CompilationUnitRewriteOperationWithSourceRange rewrite(final ReferenceHolder hit) { + return new CompilationUnitRewriteOperationWithSourceRange() { + @Override + public void rewriteASTInternal(final CompilationUnitRewrite cuRewrite, final LinkedProposalModelCore linkedModel) + throws CoreException { + TextEditGroup group= createTextEditGroup(MultiFixMessages.JUnitCleanUp_description, cuRewrite); + TightSourceRangeComputer rangeComputer; + ASTRewrite rewrite= cuRewrite.getASTRewrite(); + if (rewrite.getExtendedSourceRangeComputer() instanceof TightSourceRangeComputer) { + rangeComputer= (TightSourceRangeComputer) rewrite.getExtendedSourceRangeComputer(); + } else { + rangeComputer= new TightSourceRangeComputer(); + } + rangeComputer.addTightSourceNode( hit.get(0).minv); + rewrite.setTargetSourceRangeComputer(rangeComputer); + junitfound.rewrite(JUnitCleanUpFixCore.this, hit, cuRewrite, group); + } + }; + } +} 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 new file mode 100644 index 00000000..668a66a7 --- /dev/null +++ b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/AbstractTool.java @@ -0,0 +1,73 @@ +/******************************************************************************* + * Copyright (c) 2021 Carsten Hammer. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Carsten Hammer + *******************************************************************************/ +package org.sandbox.jdt.internal.corext.fix.helper; + +import java.util.Collection; +import java.util.Set; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ASTNode; +import org.eclipse.jdt.core.dom.CompilationUnit; +import org.eclipse.jdt.core.dom.ITypeBinding; +import org.eclipse.jdt.core.dom.Name; +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.text.edits.TextEditGroup; +import org.sandbox.jdt.internal.corext.fix.JUnitCleanUpFixCore; + +/** + * @param Type found in Visitor + */ +public abstract class AbstractTool { + + protected static boolean isOfType(ITypeBinding typeBinding, String typename) { + if (typeBinding == null) { + throw new AbortSearchException(); + } + if (typeBinding.isArray()) { + typeBinding= typeBinding.getElementType(); + } + return typeBinding.getQualifiedName().equals(typename); + } + + public abstract void find(JUnitCleanUpFixCore fixcore, CompilationUnit compilationUnit, + Set operations, Set nodesprocessed); + + public abstract void rewrite(JUnitCleanUpFixCore useExplicitEncodingFixCore, T holder, + CompilationUnitRewrite cuRewrite, TextEditGroup group); + + /** + * Adds an import to the class. This method should be used for every class + * reference added to the generated code. + * + * @param typeName a fully qualified name of a type + * @param cuRewrite CompilationUnitRewrite + * @param ast AST + * @return simple name of a class if the import was added and fully qualified + * name if there was a conflict + */ + protected Name addImport(String typeName, final CompilationUnitRewrite cuRewrite, AST ast) { + String importedName= cuRewrite.getImportRewrite().addImport(typeName); + return ast.newName(importedName); + } + + public abstract String getPreview(boolean afterRefactoring); + + public static Collection getUsedVariableNames(ASTNode node) { + CompilationUnit root= (CompilationUnit) node.getRoot(); + return new ScopeAnalyzer(root).getUsedVariableNames(node.getStartPosition(), node.getLength()); + } +} diff --git a/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/AfterClassJUnitPlugin.java b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/AfterClassJUnitPlugin.java new file mode 100644 index 00000000..9bbc6d2d --- /dev/null +++ b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/AfterClassJUnitPlugin.java @@ -0,0 +1,91 @@ +/******************************************************************************* + * Copyright (c) 2021 Carsten Hammer. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Carsten Hammer + *******************************************************************************/ +package org.sandbox.jdt.internal.corext.fix.helper; + +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.Annotation; +import org.eclipse.jdt.core.dom.CompilationUnit; +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 AfterClassJUnitPlugin extends AbstractTool> { + + private static final String ORG_JUNIT_AFTERCLASS = "org.junit.AfterClass"; + private static final String ORG_JUNIT_JUPITER_API_AFTER_ALL = "org.junit.jupiter.api.AfterAll"; + private static final String AFTER_ALL = "AfterAll"; + + @Override + public void find(JUnitCleanUpFixCore fixcore, CompilationUnit compilationUnit, + Set operations, Set nodesprocessed) { + ReferenceHolder dataholder = new ReferenceHolder<>(); + HelperVisitor.callMarkerAnnotationVisitor(ORG_JUNIT_AFTERCLASS, 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, + 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(); + Annotation minv = mh.minv; + MarkerAnnotation newAnnotation = ast.newMarkerAnnotation(); + newAnnotation.setTypeName(ast.newSimpleName(AFTER_ALL)); + addImport(ORG_JUNIT_JUPITER_API_AFTER_ALL, cuRewrite, ast); + ASTNodes.replaceButKeepComment(rewrite, minv, newAnnotation, group); + importRemover.removeImport(ORG_JUNIT_AFTERCLASS); + } + } + + @Override + public String getPreview(boolean afterRefactoring) { + if (!afterRefactoring) { + return """ + ; + """; //$NON-NLS-1$ + } + return """ + ; + """; //$NON-NLS-1$ + } +} diff --git a/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/AfterJUnitPlugin.java b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/AfterJUnitPlugin.java new file mode 100644 index 00000000..5df733f3 --- /dev/null +++ b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/AfterJUnitPlugin.java @@ -0,0 +1,91 @@ +/******************************************************************************* + * Copyright (c) 2021 Carsten Hammer. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Carsten Hammer + *******************************************************************************/ +package org.sandbox.jdt.internal.corext.fix.helper; + +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.Annotation; +import org.eclipse.jdt.core.dom.CompilationUnit; +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 AfterJUnitPlugin extends AbstractTool> { + + private static final String ORG_JUNIT_AFTER = "org.junit.After"; + private static final String ORG_JUNIT_JUPITER_API_AFTER_EACH = "org.junit.jupiter.api.AfterEach"; + private static final String AFTER_EACH = "AfterEach"; + + @Override + public void find(JUnitCleanUpFixCore fixcore, CompilationUnit compilationUnit, + Set operations, Set nodesprocessed) { + ReferenceHolder dataholder = new ReferenceHolder<>(); + HelperVisitor.callMarkerAnnotationVisitor(ORG_JUNIT_AFTER, 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, + 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(); + Annotation minv = mh.minv; + MarkerAnnotation newAnnotation = ast.newMarkerAnnotation(); + newAnnotation.setTypeName(ast.newSimpleName(AFTER_EACH)); + addImport(ORG_JUNIT_JUPITER_API_AFTER_EACH, cuRewrite, ast); + ASTNodes.replaceButKeepComment(rewrite, minv, newAnnotation, group); + importRemover.removeImport(ORG_JUNIT_AFTER); + } + } + + @Override + public String getPreview(boolean afterRefactoring) { + if (!afterRefactoring) { + return """ + ; + """; //$NON-NLS-1$ + } + return """ + ; + """; //$NON-NLS-1$ + } +} diff --git a/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/BeforeClassJUnitPlugin.java b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/BeforeClassJUnitPlugin.java new file mode 100644 index 00000000..30340152 --- /dev/null +++ b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/BeforeClassJUnitPlugin.java @@ -0,0 +1,93 @@ +/******************************************************************************* + * Copyright (c) 2021 Carsten Hammer. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Carsten Hammer + *******************************************************************************/ +package org.sandbox.jdt.internal.corext.fix.helper; + +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.Annotation; +import org.eclipse.jdt.core.dom.ClassInstanceCreation; +import org.eclipse.jdt.core.dom.CompilationUnit; +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 BeforeClassJUnitPlugin extends AbstractTool> { + + private static final String ORG_JUNIT_BEFORECLASS = "org.junit.BeforeClass"; + private static final String ORG_JUNIT_JUPITER_API_BEFORE_ALL = "org.junit.jupiter.api.BeforeAll"; + private static final String BEFORE_ALL = "BeforeAll"; + + @Override + public void find(JUnitCleanUpFixCore fixcore, CompilationUnit compilationUnit, + Set operations, Set nodesprocessed) { + ReferenceHolder dataholder = new ReferenceHolder<>(); + HelperVisitor.callMarkerAnnotationVisitor(ORG_JUNIT_BEFORECLASS, 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, + 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(); + Annotation minv = mh.minv; + MarkerAnnotation newAnnotation = ast.newMarkerAnnotation(); + newAnnotation.setTypeName(ast.newSimpleName(BEFORE_ALL)); + addImport(ORG_JUNIT_JUPITER_API_BEFORE_ALL, cuRewrite, ast); + ASTNodes.replaceButKeepComment(rewrite, minv, newAnnotation, group); + importRemover.removeImport(ORG_JUNIT_BEFORECLASS); + } + } + + @Override + public String getPreview(boolean afterRefactoring) { + if (!afterRefactoring) { + return """ + ; + """; //$NON-NLS-1$ + } + return """ + ; + """; //$NON-NLS-1$ + } +} diff --git a/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/BeforeJUnitPlugin.java b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/BeforeJUnitPlugin.java new file mode 100644 index 00000000..7ac8548f --- /dev/null +++ b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/BeforeJUnitPlugin.java @@ -0,0 +1,93 @@ +/******************************************************************************* + * Copyright (c) 2021 Carsten Hammer. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Carsten Hammer + *******************************************************************************/ +package org.sandbox.jdt.internal.corext.fix.helper; + +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.Annotation; +import org.eclipse.jdt.core.dom.ClassInstanceCreation; +import org.eclipse.jdt.core.dom.CompilationUnit; +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 BeforeJUnitPlugin extends AbstractTool> { + + private static final String ORG_JUNIT_BEFORE = "org.junit.Before"; + private static final String ORG_JUNIT_JUPITER_API_BEFORE_EACH = "org.junit.jupiter.api.BeforeEach"; + private static final String BEFORE_EACH = "BeforeEach"; + + @Override + public void find(JUnitCleanUpFixCore fixcore, CompilationUnit compilationUnit, + Set operations, Set nodesprocessed) { + ReferenceHolder dataholder = new ReferenceHolder<>(); + HelperVisitor.callMarkerAnnotationVisitor(ORG_JUNIT_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, + 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(); + Annotation minv = mh.minv; + MarkerAnnotation newAnnotation = ast.newMarkerAnnotation(); + newAnnotation.setTypeName(ast.newSimpleName(BEFORE_EACH)); + addImport(ORG_JUNIT_JUPITER_API_BEFORE_EACH, 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$ + } + return """ + ; + """; //$NON-NLS-1$ + } +} diff --git a/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/IgnoreJUnitPlugin.java b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/IgnoreJUnitPlugin.java new file mode 100644 index 00000000..e215e798 --- /dev/null +++ b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/IgnoreJUnitPlugin.java @@ -0,0 +1,106 @@ +/******************************************************************************* + * Copyright (c) 2021 Carsten Hammer. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Carsten Hammer + *******************************************************************************/ +package org.sandbox.jdt.internal.corext.fix.helper; + +import java.util.Map.Entry; +import java.util.Set; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ASTNode; +import org.eclipse.jdt.core.dom.Annotation; +import org.eclipse.jdt.core.dom.CompilationUnit; +import org.eclipse.jdt.core.dom.Expression; +import org.eclipse.jdt.core.dom.SingleMemberAnnotation; +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 IgnoreJUnitPlugin extends AbstractTool> { + + private static final String ORG_JUNIT_IGNORE = "org.junit.Ignore"; + private static final String ORG_JUNIT_JUPITER_DISABLED = "org.junit.jupiter.api.Disabled"; + private static final String DISABLED = "Disabled"; + + @Override + public void find(JUnitCleanUpFixCore fixcore, CompilationUnit compilationUnit, + Set operations, Set nodesprocessed) { + ReferenceHolder dataholder = new ReferenceHolder<>(); + HelperVisitor.callMarkerAnnotationVisitor(ORG_JUNIT_IGNORE, compilationUnit, dataholder, nodesprocessed, + (visited, aholder) -> processFoundNode(fixcore, operations, visited, aholder)); + HelperVisitor.callNormalAnnotationVisitor(ORG_JUNIT_IGNORE, compilationUnit, dataholder, nodesprocessed, + (visited, aholder) -> processFoundNode(fixcore, operations, visited, aholder)); + HelperVisitor.callSingleMemberAnnotationVisitor(ORG_JUNIT_IGNORE, compilationUnit, dataholder, nodesprocessed, + (visited, aholder) -> processFoundNode(fixcore, operations, visited, aholder)); + } + + private boolean processFoundNode(JUnitCleanUpFixCore fixcore, + Set operations, Annotation node, + ReferenceHolder dataholder) { + JunitHolder mh = new JunitHolder(); + mh.minv = node; + mh.minvname = node.getTypeName().getFullyQualifiedName(); + if(node instanceof SingleMemberAnnotation mynode) { + Expression value = mynode.getValue(); + mh.value=value.toString(); + } + dataholder.put(dataholder.size(), mh); + operations.add(fixcore.rewrite(dataholder)); + return false; + } + + @Override + 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(); + Annotation minv = mh.minv; + Annotation newAnnotation = null; + if(minv instanceof SingleMemberAnnotation mynode) { + newAnnotation = ast.newSingleMemberAnnotation(); + ((SingleMemberAnnotation)newAnnotation).setValue(ASTNodes.createMoveTarget(rewrite, mynode.getValue())); + } else { + newAnnotation = ast.newMarkerAnnotation(); + } + newAnnotation.setTypeName(ast.newSimpleName(DISABLED)); + addImport(ORG_JUNIT_JUPITER_DISABLED, cuRewrite, ast); + ASTNodes.replaceButKeepComment(rewrite, minv, newAnnotation, group); + importRemover.removeImport(ORG_JUNIT_IGNORE); + } + } + + @Override + public String getPreview(boolean afterRefactoring) { + if (!afterRefactoring) { + return """ + ; + """; //$NON-NLS-1$ + } + return """ + ; + """; //$NON-NLS-1$ + } +} diff --git a/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/JunitHolder.java b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/JunitHolder.java new file mode 100644 index 00000000..9da31d0b --- /dev/null +++ b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/JunitHolder.java @@ -0,0 +1,13 @@ +package org.sandbox.jdt.internal.corext.fix.helper; + +import java.util.Set; + +import org.eclipse.jdt.core.dom.ASTNode; +import org.eclipse.jdt.core.dom.Annotation; + +public class JunitHolder { + public Annotation minv; + public String minvname; + public Set nodesprocessed; + public String value; +} \ No newline at end of file diff --git a/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/RunWithJUnitPlugin.java b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/RunWithJUnitPlugin.java new file mode 100644 index 00000000..2460bdeb --- /dev/null +++ b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/RunWithJUnitPlugin.java @@ -0,0 +1,118 @@ +/******************************************************************************* + * Copyright (c) 2021 Carsten Hammer. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Carsten Hammer + *******************************************************************************/ +package org.sandbox.jdt.internal.corext.fix.helper; + +import java.util.Map.Entry; +import java.util.Set; + +import org.eclipse.jdt.core.dom.AST; +import org.eclipse.jdt.core.dom.ASTNode; +import org.eclipse.jdt.core.dom.Annotation; +import org.eclipse.jdt.core.dom.CompilationUnit; +import org.eclipse.jdt.core.dom.Expression; +import org.eclipse.jdt.core.dom.ITypeBinding; +import org.eclipse.jdt.core.dom.NormalAnnotation; +import org.eclipse.jdt.core.dom.SingleMemberAnnotation; +import org.eclipse.jdt.core.dom.TypeLiteral; +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 RunWithJUnitPlugin extends AbstractTool> { + + private static final String ORG_JUNIT_RUNWITH = "org.junit.runner.RunWith"; + private static final String ORG_JUNIT_JUPITER_SUITE = "org.junit.platform.suite.api.Suite"; + private static final String SUITE = "Suite"; + + @Override + public void find(JUnitCleanUpFixCore fixcore, CompilationUnit compilationUnit, + Set operations, Set nodesprocessed) { + ReferenceHolder dataholder = new ReferenceHolder<>(); + HelperVisitor.callMarkerAnnotationVisitor(ORG_JUNIT_RUNWITH, compilationUnit, dataholder, nodesprocessed, + (visited, aholder) -> processFoundNode(fixcore, operations, visited, aholder)); + HelperVisitor.callNormalAnnotationVisitor(ORG_JUNIT_RUNWITH, compilationUnit, dataholder, nodesprocessed, + (visited, aholder) -> processFoundNode(fixcore, operations, visited, aholder)); + HelperVisitor.callSingleMemberAnnotationVisitor(ORG_JUNIT_RUNWITH, compilationUnit, dataholder, nodesprocessed, + (visited, aholder) -> processFoundNode(fixcore, operations, visited, aholder)); + } + + private boolean processFoundNode(JUnitCleanUpFixCore fixcore, + Set operations, Annotation node, + ReferenceHolder dataholder) { + JunitHolder mh = new JunitHolder(); + mh.minv = node; + mh.minvname = node.getTypeName().getFullyQualifiedName(); + if(node instanceof SingleMemberAnnotation mynode) { + Expression value = mynode.getValue(); + if(value instanceof TypeLiteral myvalue) { + + ITypeBinding typeBinding = myvalue.resolveTypeBinding(); + if (typeBinding != null) { + mh.value = typeBinding.getQualifiedName(); + if (!"org.junit.runners.Suite".equals(mh.value)) { + return false; + } + dataholder.put(dataholder.size(), mh); + operations.add(fixcore.rewrite(dataholder)); + } + } + } + return false; + } + + @Override + 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(); + Annotation minv = mh.minv; + Annotation newAnnotation = null; + if(minv instanceof SingleMemberAnnotation mynode) { + newAnnotation = ast.newSingleMemberAnnotation(); + ((SingleMemberAnnotation)newAnnotation).setValue(ASTNodes.createMoveTarget(rewrite, mynode.getValue())); + } else { + newAnnotation = ast.newMarkerAnnotation(); + } + newAnnotation.setTypeName(ast.newSimpleName(SUITE)); + addImport(ORG_JUNIT_JUPITER_SUITE, cuRewrite, ast); + ASTNodes.replaceButKeepComment(rewrite, minv, newAnnotation, group); + importRemover.removeImport(ORG_JUNIT_RUNWITH); + } + } + + @Override + public String getPreview(boolean afterRefactoring) { + if (!afterRefactoring) { + return """ + ; + """; //$NON-NLS-1$ + } + return """ + ; + """; //$NON-NLS-1$ + } +} diff --git a/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/TestJUnitPlugin.java b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/TestJUnitPlugin.java new file mode 100644 index 00000000..ccd70154 --- /dev/null +++ b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/corext/fix/helper/TestJUnitPlugin.java @@ -0,0 +1,91 @@ +/******************************************************************************* + * Copyright (c) 2021 Carsten Hammer. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Carsten Hammer + *******************************************************************************/ +package org.sandbox.jdt.internal.corext.fix.helper; + +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.Annotation; +import org.eclipse.jdt.core.dom.CompilationUnit; +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 TestJUnitPlugin extends AbstractTool> { + + private static final String ORG_JUNIT_TEST = "org.junit.Test"; + private static final String ORG_JUNIT_JUPITER_TEST = "org.junit.jupiter.api.Test"; + private static final String TEST = "Test"; + + @Override + public void find(JUnitCleanUpFixCore fixcore, CompilationUnit compilationUnit, + Set operations, Set nodesprocessed) { + ReferenceHolder dataholder = new ReferenceHolder<>(); + HelperVisitor.callMarkerAnnotationVisitor(ORG_JUNIT_TEST, 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, + 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(); + Annotation minv = mh.minv; + MarkerAnnotation newAnnotation = ast.newMarkerAnnotation(); + newAnnotation.setTypeName(ast.newSimpleName(TEST)); + ASTNodes.replaceButKeepComment(rewrite, minv, newAnnotation, group); + importRemover.removeImport(ORG_JUNIT_TEST); + addImport(ORG_JUNIT_JUPITER_TEST, cuRewrite, ast); + } + } + + @Override + public String getPreview(boolean afterRefactoring) { + if (!afterRefactoring) { + return """ + ; + """; //$NON-NLS-1$ + } + return """ + ; + """; //$NON-NLS-1$ + } +} diff --git a/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/fix/JUnitCleanUp.java b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/fix/JUnitCleanUp.java new file mode 100644 index 00000000..0e763d54 --- /dev/null +++ b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/fix/JUnitCleanUp.java @@ -0,0 +1,31 @@ +/******************************************************************************* + * Copyright (c) 2021 Carsten Hammer. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Carsten Hammer + *******************************************************************************/ +package org.sandbox.jdt.internal.ui.fix; + +import java.util.Collections; +import java.util.Map; + +import org.eclipse.jdt.internal.ui.fix.AbstractCleanUpCoreWrapper; + +/** + */ +public class JUnitCleanUp extends AbstractCleanUpCoreWrapper { + public JUnitCleanUp(final Map options) { + super(options, new JUnitCleanUpCore()); + } + + public JUnitCleanUp() { + this(Collections.emptyMap()); + } +} 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 new file mode 100644 index 00000000..c0c0e80f --- /dev/null +++ b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/fix/JUnitCleanUpCore.java @@ -0,0 +1,110 @@ +/******************************************************************************* + * Copyright (c) 2021 Carsten Hammer. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Carsten Hammer + *******************************************************************************/ +package org.sandbox.jdt.internal.ui.fix; + +import static org.sandbox.jdt.internal.corext.fix2.MYCleanUpConstants.JUNIT_CLEANUP; +import static org.sandbox.jdt.internal.ui.fix.MultiFixMessages.JUnitCleanUpFix_refactor; +import static org.sandbox.jdt.internal.ui.fix.MultiFixMessages.JUnitCleanUp_description; + +import java.util.ArrayList; +import java.util.EnumSet; +import java.util.HashSet; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.jdt.core.dom.ASTNode; +import org.eclipse.jdt.core.dom.CompilationUnit; +import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore; +import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperationWithSourceRange; +import org.eclipse.jdt.internal.corext.util.JavaModelUtil; +import org.eclipse.jdt.internal.corext.util.Messages; +import org.eclipse.jdt.internal.ui.fix.AbstractCleanUp; +import org.eclipse.jdt.ui.cleanup.CleanUpContext; +import org.eclipse.jdt.ui.cleanup.CleanUpRequirements; +import org.eclipse.jdt.ui.cleanup.ICleanUpFix; +import org.sandbox.jdt.internal.corext.fix.JUnitCleanUpFixCore; + +public class JUnitCleanUpCore extends AbstractCleanUp { + public JUnitCleanUpCore(final Map options) { + super(options); + } + /** + * + */ + public JUnitCleanUpCore() { + } + + @Override + public CleanUpRequirements getRequirements() { + return new CleanUpRequirements(requireAST(), false, false, null); + } + + public boolean requireAST() { + return isEnabled(JUNIT_CLEANUP); + } + + @Override + public ICleanUpFix createFix(final CleanUpContext context) throws CoreException { + CompilationUnit compilationUnit= context.getAST(); + if (compilationUnit == null) { + return null; + } + EnumSet computeFixSet= computeFixSet(); + if (!isEnabled(JUNIT_CLEANUP) || computeFixSet.isEmpty() + || !JavaModelUtil.is1d8OrHigher(compilationUnit.getJavaElement().getJavaProject())) { + return null; + } + Set operations= new LinkedHashSet<>(); + Set nodesprocessed= new HashSet<>(); + computeFixSet.forEach(i -> i.findOperations(compilationUnit, operations, nodesprocessed)); + if (operations.isEmpty()) { + return null; + } + return new CompilationUnitRewriteOperationsFixCore(JUnitCleanUpFix_refactor, compilationUnit, + operations.toArray(new CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation[0])); + } + + @Override + public String[] getStepDescriptions() { + List result= new ArrayList<>(); + if (isEnabled(JUNIT_CLEANUP)) { + result.add(Messages.format(JUnitCleanUp_description, new Object[] { String.join(",", //$NON-NLS-1$ + computeFixSet().stream().map(JUnitCleanUpFixCore::toString) + .collect(Collectors.toList())) })); + } + return result.toArray(new String[0]); + } + + @Override + public String getPreview() { + StringBuilder sb= new StringBuilder(); + EnumSet computeFixSet= computeFixSet(); + EnumSet.allOf(JUnitCleanUpFixCore.class) + .forEach(e -> sb.append(e.getPreview(computeFixSet.contains(e)))); + return sb.toString(); + } + + private EnumSet computeFixSet() { + EnumSet fixSet= EnumSet.noneOf(JUnitCleanUpFixCore.class); + + if (isEnabled(JUNIT_CLEANUP)) { + fixSet= EnumSet.allOf(JUnitCleanUpFixCore.class); + } + return fixSet; + } +} diff --git a/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/fix/MultiFixMessages.java b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/fix/MultiFixMessages.java new file mode 100644 index 00000000..0d54bab8 --- /dev/null +++ b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/fix/MultiFixMessages.java @@ -0,0 +1,15 @@ +package org.sandbox.jdt.internal.ui.fix; + +import org.eclipse.osgi.util.NLS; + +public class MultiFixMessages { + private static final String BUNDLE_NAME= "org.sandbox.jdt.internal.ui.fix.MultiFixMessages"; //$NON-NLS-1$ + + public static String JUnitCleanUp_description; + public static String JUnitCleanUpFix_refactor; + + static { + // initialize resource bundle + NLS.initializeMessages(BUNDLE_NAME, MultiFixMessages.class); + } +} diff --git a/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/fix/MultiFixMessages.properties b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/fix/MultiFixMessages.properties new file mode 100644 index 00000000..c3d64092 --- /dev/null +++ b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/fix/MultiFixMessages.properties @@ -0,0 +1,2 @@ +JUnitCleanUp_description=Update junit ''{0}'' +JUnitCleanUpFix_refactor=Update junit \ No newline at end of file diff --git a/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/preferences/cleanup/CleanUpMessages.java b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/preferences/cleanup/CleanUpMessages.java new file mode 100644 index 00000000..ca6684b7 --- /dev/null +++ b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/preferences/cleanup/CleanUpMessages.java @@ -0,0 +1,27 @@ +/******************************************************************************* + * Copyright (c) 2021 Carsten Hammer. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Carsten Hammer + *******************************************************************************/ +package org.sandbox.jdt.internal.ui.preferences.cleanup; + +import org.eclipse.osgi.util.NLS; + +public class CleanUpMessages { + private static final String BUNDLE_NAME= "org.sandbox.jdt.internal.ui.preferences.cleanup.CleanUpMessages"; //$NON-NLS-1$ + public static String JavaFeatureTabPage_GroupName_Java1d8; + public static String JavaFeatureTabPage_CheckboxName_JUNIT_CLEANUP; + + static { + // initialize resource bundle + NLS.initializeMessages(BUNDLE_NAME, CleanUpMessages.class); + } +} diff --git a/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/preferences/cleanup/CleanUpMessages.properties b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/preferences/cleanup/CleanUpMessages.properties new file mode 100644 index 00000000..4434c43c --- /dev/null +++ b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/preferences/cleanup/CleanUpMessages.properties @@ -0,0 +1,2 @@ +JavaFeatureTabPage_CheckboxName_JUNIT_CLEANUP=JUNIT_CLEANUP +JavaFeatureTabPage_GroupName_Java1d8=Java 8 \ No newline at end of file diff --git a/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/preferences/cleanup/DefaultCleanUpOptionsInitializer.java b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/preferences/cleanup/DefaultCleanUpOptionsInitializer.java new file mode 100644 index 00000000..07060059 --- /dev/null +++ b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/preferences/cleanup/DefaultCleanUpOptionsInitializer.java @@ -0,0 +1,26 @@ +/******************************************************************************* + * Copyright (c) 2021 Carsten Hammer. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Carsten Hammer + *******************************************************************************/ +package org.sandbox.jdt.internal.ui.preferences.cleanup; + +import org.eclipse.jdt.ui.cleanup.CleanUpOptions; +import org.eclipse.jdt.ui.cleanup.ICleanUpOptionsInitializer; +import org.sandbox.jdt.internal.corext.fix2.MYCleanUpConstants; + +public class DefaultCleanUpOptionsInitializer implements ICleanUpOptionsInitializer { + + @Override + public void setDefaultOptions(CleanUpOptions options) { + options.setOption(MYCleanUpConstants.JUNIT_CLEANUP, CleanUpOptions.FALSE); + } +} diff --git a/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/preferences/cleanup/SandboxCodeTabPage.java b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/preferences/cleanup/SandboxCodeTabPage.java new file mode 100644 index 00000000..b5ad350b --- /dev/null +++ b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/preferences/cleanup/SandboxCodeTabPage.java @@ -0,0 +1,49 @@ +/******************************************************************************* + * Copyright (c) 2021 Carsten Hammer. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Carsten Hammer + *******************************************************************************/ +package org.sandbox.jdt.internal.ui.preferences.cleanup; + +import java.util.Map; + +import org.eclipse.jdt.internal.ui.fix.AbstractCleanUp; +import org.eclipse.jdt.internal.ui.preferences.cleanup.AbstractCleanUpTabPage; +import org.eclipse.jdt.ui.cleanup.CleanUpOptions; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Group; +import org.sandbox.jdt.internal.corext.fix2.MYCleanUpConstants; +import org.sandbox.jdt.internal.ui.fix.JUnitCleanUp; + +public class SandboxCodeTabPage extends AbstractCleanUpTabPage { + + /** + * Constant array for boolean selection + */ + static final String[] FALSE_TRUE= { CleanUpOptions.FALSE, CleanUpOptions.TRUE }; + + public static final String ID= "org.eclipse.jdt.ui.cleanup.tabpage.sandbox"; //$NON-NLS-1$ + + @Override + protected AbstractCleanUp[] createPreviewCleanUps(Map values) { + return new AbstractCleanUp[] { new JUnitCleanUp(values) }; + } + + @Override + protected void doCreatePreferences(Composite composite, int numColumns) { + Group java1d8Group= createGroup(numColumns, composite, CleanUpMessages.JavaFeatureTabPage_GroupName_Java1d8); + final CheckboxPreference xmlsimplify= createCheckboxPref(java1d8Group, numColumns, + CleanUpMessages.JavaFeatureTabPage_CheckboxName_JUNIT_CLEANUP, + MYCleanUpConstants.JUNIT_CLEANUP, FALSE_TRUE); + intent(java1d8Group); + registerPreference(xmlsimplify); + } +} \ No newline at end of file diff --git a/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/preferences/cleanup/SaveActionCleanUpOptionsInitializer.java b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/preferences/cleanup/SaveActionCleanUpOptionsInitializer.java new file mode 100644 index 00000000..3acdacc0 --- /dev/null +++ b/sandbox_junit_cleanup/src/org/sandbox/jdt/internal/ui/preferences/cleanup/SaveActionCleanUpOptionsInitializer.java @@ -0,0 +1,26 @@ +/******************************************************************************* + * Copyright (c) 2021 Carsten Hammer. + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Carsten Hammer + *******************************************************************************/ +package org.sandbox.jdt.internal.ui.preferences.cleanup; + +import org.eclipse.jdt.ui.cleanup.CleanUpOptions; +import org.eclipse.jdt.ui.cleanup.ICleanUpOptionsInitializer; +import org.sandbox.jdt.internal.corext.fix2.MYCleanUpConstants; + +public class SaveActionCleanUpOptionsInitializer implements ICleanUpOptionsInitializer { + + @Override + public void setDefaultOptions(CleanUpOptions options) { + options.setOption(MYCleanUpConstants.JUNIT_CLEANUP, CleanUpOptions.FALSE); + } +} diff --git a/sandbox_junit_cleanup_feature/.gitignore b/sandbox_junit_cleanup_feature/.gitignore new file mode 100644 index 00000000..b83d2226 --- /dev/null +++ b/sandbox_junit_cleanup_feature/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/sandbox_junit_cleanup_feature/.project b/sandbox_junit_cleanup_feature/.project new file mode 100644 index 00000000..e4825b41 --- /dev/null +++ b/sandbox_junit_cleanup_feature/.project @@ -0,0 +1,23 @@ + + + sandbox_junit_cleanup_feature + + + + + + org.eclipse.pde.FeatureBuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.m2e.core.maven2Nature + org.eclipse.pde.FeatureNature + + diff --git a/sandbox_junit_cleanup_feature/build.properties b/sandbox_junit_cleanup_feature/build.properties new file mode 100644 index 00000000..e4762c45 --- /dev/null +++ b/sandbox_junit_cleanup_feature/build.properties @@ -0,0 +1 @@ +bin.includes=. \ No newline at end of file diff --git a/sandbox_junit_cleanup_feature/feature.xml b/sandbox_junit_cleanup_feature/feature.xml new file mode 100644 index 00000000..cc52a77f --- /dev/null +++ b/sandbox_junit_cleanup_feature/feature.xml @@ -0,0 +1,50 @@ + + + + + %description + + + + %copyright + + + + %license + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sandbox_junit_cleanup_feature/pom.xml b/sandbox_junit_cleanup_feature/pom.xml new file mode 100644 index 00000000..51001e58 --- /dev/null +++ b/sandbox_junit_cleanup_feature/pom.xml @@ -0,0 +1,14 @@ + + + 4.0.0 + + org.sandbox + central + 1.1.0-SNAPSHOT + + sandbox_junit_cleanup_feature + Sandbox junit cleanup Feature + eclipse-feature + \ No newline at end of file diff --git a/sandbox_junit_cleanup_test/.classpath b/sandbox_junit_cleanup_test/.classpath new file mode 100644 index 00000000..7931ec26 --- /dev/null +++ b/sandbox_junit_cleanup_test/.classpath @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/sandbox_junit_cleanup_test/.gitignore b/sandbox_junit_cleanup_test/.gitignore new file mode 100644 index 00000000..0f630157 --- /dev/null +++ b/sandbox_junit_cleanup_test/.gitignore @@ -0,0 +1,2 @@ +/target/ +/bin/ diff --git a/sandbox_junit_cleanup_test/.project b/sandbox_junit_cleanup_test/.project new file mode 100644 index 00000000..3ab272cb --- /dev/null +++ b/sandbox_junit_cleanup_test/.project @@ -0,0 +1,34 @@ + + + sandbox_junit_cleanup_test + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.pde.ManifestBuilder + + + + + org.eclipse.pde.SchemaBuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.m2e.core.maven2Nature + org.eclipse.pde.PluginNature + org.eclipse.jdt.core.javanature + + diff --git a/sandbox_junit_cleanup_test/META-INF/MANIFEST.MF b/sandbox_junit_cleanup_test/META-INF/MANIFEST.MF new file mode 100644 index 00000000..9928de47 --- /dev/null +++ b/sandbox_junit_cleanup_test/META-INF/MANIFEST.MF @@ -0,0 +1,28 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: junit cleanup tests +Bundle-SymbolicName: sandbox_junit_cleanup_test;singleton:=true +Bundle-Version: 1.1.0.qualifier +Fragment-Host: sandbox_junit_cleanup +Bundle-RequiredExecutionEnvironment: JavaSE-17 +Require-Bundle: org.junit, + sandbox_test_commons +Bundle-ActivationPolicy: lazy +Eclipse-BundleShape: dir +Eclipse-BuddyPolicy: global +Import-Package: org.junit.jupiter.api;version="5.7.1", + org.junit.jupiter.api.condition;version="5.7.1", + org.junit.jupiter.api.extension;version="5.7.1", + org.junit.jupiter.api.extension.support;version="5.7.1", + org.junit.jupiter.api.function;version="5.7.1", + org.junit.jupiter.api.io;version="5.7.1", + org.junit.jupiter.api.parallel;version="5.7.1", + org.junit.jupiter.params;version="5.7.1", + org.junit.jupiter.params.aggregator;version="5.7.1", + org.junit.jupiter.params.converter;version="5.7.1", + org.junit.jupiter.params.provider;version="5.7.1", + org.junit.jupiter.params.support;version="5.7.1", + org.opentest4j;version="1.2.0", + org.sandbox.jdt.internal.common, + org.sandbox.jdt.internal.corext.fix2 +Automatic-Module-Name: sandbox.junit.cleanup.test diff --git a/sandbox_junit_cleanup_test/build.properties b/sandbox_junit_cleanup_test/build.properties new file mode 100644 index 00000000..99ca9c2f --- /dev/null +++ b/sandbox_junit_cleanup_test/build.properties @@ -0,0 +1,5 @@ +source.. = src/ +bin.includes = META-INF/,\ + .,\ + fragment.xml,\ + build.properties diff --git a/sandbox_junit_cleanup_test/fragment.xml b/sandbox_junit_cleanup_test/fragment.xml new file mode 100644 index 00000000..79151e03 --- /dev/null +++ b/sandbox_junit_cleanup_test/fragment.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/sandbox_junit_cleanup_test/pom.xml b/sandbox_junit_cleanup_test/pom.xml new file mode 100644 index 00000000..57e98bcb --- /dev/null +++ b/sandbox_junit_cleanup_test/pom.xml @@ -0,0 +1,44 @@ + + + 4.0.0 + + org.sandbox + central + 1.1.0-SNAPSHOT + + sandbox_junit_cleanup_test + Sandbox junit cleanup test + eclipse-test-plugin + + + org.sandbox + sandbox_junit_cleanup + + + + src + + + org.eclipse.tycho + tycho-surefire-plugin + ${tycho-version} + + BREE + false + true + + + eclipse-plugin + org.sandbox + sandbox_junit_cleanup + + 0.0.0 + + + + + + + \ No newline at end of file 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 new file mode 100644 index 00000000..e87bb38c --- /dev/null +++ b/sandbox_junit_cleanup_test/src/org/sandbox/jdt/ui/tests/quickfix/Java8CleanUpTest.java @@ -0,0 +1,239 @@ +/******************************************************************************* + * Copyright (c) 2022 + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Carsten Hammer initial implementation + *******************************************************************************/ +package org.sandbox.jdt.ui.tests.quickfix; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.Path; +import org.eclipse.jdt.core.IClasspathEntry; +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.IPackageFragment; +import org.eclipse.jdt.core.IPackageFragmentRoot; +import org.eclipse.jdt.core.JavaCore; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; +import org.sandbox.jdt.internal.corext.fix2.MYCleanUpConstants; +import org.sandbox.jdt.ui.tests.quickfix.rules.AbstractEclipseJava; +import org.sandbox.jdt.ui.tests.quickfix.rules.EclipseJava8; + + +public class Java8CleanUpTest { + + @RegisterExtension + AbstractEclipseJava context= new EclipseJava8(); + + public static final String JUNIT_CONTAINER_ID= "org.eclipse.jdt.junit.JUNIT_CONTAINER"; //$NON-NLS-1$ + public final static IPath JUNIT4_CONTAINER_PATH= new Path(JUNIT_CONTAINER_ID).append("4"); //$NON-NLS-1$ + + IPackageFragmentRoot fRoot; + @BeforeEach + public void setup() throws CoreException { + IJavaProject fProject = context.javaProject; + fProject.setRawClasspath(context.getDefaultClasspath(), null); + IClasspathEntry cpe= JavaCore.newContainerEntry(JUNIT4_CONTAINER_PATH); + AbstractEclipseJava.addToClasspath(fProject, cpe); + fRoot = AbstractEclipseJava.addSourceContainer(fProject, "src"); + } + + enum JUnitCleanupCases{ + PositiveCase(""" +package test; +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +/** + * + */ +@RunWith(Suite.class) +@Suite.SuiteClasses({ + MyTest.class +}) +public class MyTest { + + /** + * @throws java.lang.Exception + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception { + } + + @Ignore + @Test + public void test() { + fail("Not yet implemented"); + } + + @Ignore("not implemented") + @Test + public void test2() { + fail("Not yet implemented"); + } +} + """, //$NON-NLS-1$ + + """ +package test; +import static org.junit.Assert.*; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +/** + * + */ +@RunWith(Suite.class) +@Suite.SuiteClasses({ + MyTest.class +}) +public class MyTest { + + /** + * @throws java.lang.Exception + */ + @BeforeAll + public static void setUpBeforeClass() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @AfterAll + public static void tearDownAfterClass() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @BeforeEach + public void setUp() throws Exception { + } + + /** + * @throws java.lang.Exception + */ + @AfterEach + public void tearDown() throws Exception { + } + + @Disabled + @Test + public void test() { + fail("Not yet implemented"); + } + + @Disabled("not implemented") + @Test + public void test2() { + fail("Not yet implemented"); + } +} + """); //$NON-NLS-1$ + + String given; + String expected; + + JUnitCleanupCases(String given, String expected) { + this.given=given; + this.expected=expected; + } + } + + // @Disabled + @ParameterizedTest + @EnumSource(JUnitCleanupCases.class) + public void testJUnitCleanupParametrized(JUnitCleanupCases test) throws CoreException { + IPackageFragment pack= fRoot.createPackageFragment("test", true, null); +// IPackageFragment pack= context.getfSourceFolder().createPackageFragment("test", false, null); //$NON-NLS-1$ + ICompilationUnit cu= pack.createCompilationUnit("Test.java", test.given, false, null); //$NON-NLS-1$ + context.enable(MYCleanUpConstants.JUNIT_CLEANUP); + context.assertRefactoringResultAsExpected(new ICompilationUnit[] {cu}, new String[] {test.expected}, null); + } + + enum NOJUnitCleanupCases { + + NOCase( + """ + package test; + import java.util.*; + public class Test { + void m(List strings) { + Iterator it = strings.iterator(); + while (it.hasNext()) { + String s = (String) it.next(); + if (s.isEmpty()) { + it.remove(); + } else { + System.out.println(s); + } + } + } + } + """) //$NON-NLS-1$ + ; + + NOJUnitCleanupCases(String given) { + this.given=given; + } + + String given; + } + + // @Disabled + @ParameterizedTest + @EnumSource(NOJUnitCleanupCases.class) + public void testJUnitCleanupdonttouch(NOJUnitCleanupCases test) throws CoreException { + IPackageFragment pack= context.getfSourceFolder().createPackageFragment("test", false, null); //$NON-NLS-1$ + ICompilationUnit cu= pack.createCompilationUnit("Test.java",test.given,false, null); //$NON-NLS-1$ + context.enable(MYCleanUpConstants.JUNIT_CLEANUP); + context.assertRefactoringHasNoChange(new ICompilationUnit[] { cu }); + } +} diff --git a/sandbox_junit_cleanup_test/testresources/rtstubs9.jar b/sandbox_junit_cleanup_test/testresources/rtstubs9.jar new file mode 100644 index 00000000..21fe246e Binary files /dev/null and b/sandbox_junit_cleanup_test/testresources/rtstubs9.jar differ diff --git a/sandbox_oomph/sandbox.setup b/sandbox_oomph/sandbox.setup index 64841e86..1b3d15e8 100644 --- a/sandbox_oomph/sandbox.setup +++ b/sandbox_oomph/sandbox.setup @@ -78,7 +78,7 @@ value="${installation.location/}${installation.relativeProductFolder}"/> + requiredJavaVersion="21"> @@ -219,7 +219,7 @@ + url="http://download.eclipse.org/releases/2024-06"/> diff --git a/sandbox_oomph/sandboxproject.setup b/sandbox_oomph/sandboxproject.setup index eafa0e0f..9df8a34f 100644 --- a/sandbox_oomph/sandboxproject.setup +++ b/sandbox_oomph/sandboxproject.setup @@ -62,8 +62,8 @@ label="Git Clone location"/> + version="JavaSE-21" + location="${jre.location-21}"> Define the JRE needed to compile and run the Java projects of ${scope.project.label} The dynamic working sets for ${scope.project.label} - - - - diff --git a/sandbox_test_commons/src/org/sandbox/jdt/ui/tests/quickfix/rules/AbstractEclipseJava.java b/sandbox_test_commons/src/org/sandbox/jdt/ui/tests/quickfix/rules/AbstractEclipseJava.java index 6f20daf7..1c196cac 100644 --- a/sandbox_test_commons/src/org/sandbox/jdt/ui/tests/quickfix/rules/AbstractEclipseJava.java +++ b/sandbox_test_commons/src/org/sandbox/jdt/ui/tests/quickfix/rules/AbstractEclipseJava.java @@ -78,8 +78,9 @@ public class AbstractEclipseJava implements AfterEachCallback, BeforeEachCallbac private final String testresources_stubs; private final String compliance; private static final String TEST_SETUP_PROJECT= "TestSetupProject"; //$NON-NLS-1$ - private IPackageFragmentRoot fSourceFolder; + private IPackageFragmentRoot fSourceFolder; private CustomProfile fProfile; + public IJavaProject javaProject; public AbstractEclipseJava(String stubs, String compilerversion) { this.testresources_stubs= stubs; @@ -88,7 +89,7 @@ public AbstractEclipseJava(String stubs, String compilerversion) { @Override public void beforeEach(ExtensionContext context) throws CoreException { - IJavaProject javaProject= createJavaProject(TEST_SETUP_PROJECT, "bin"); //$NON-NLS-1$ + javaProject= createJavaProject(TEST_SETUP_PROJECT, "bin"); //$NON-NLS-1$ javaProject.setRawClasspath(getDefaultClasspath(), null); Map options= javaProject.getOptions(false); JavaCore.setComplianceOptions(compliance, options); @@ -290,6 +291,56 @@ public static IPackageFragmentRoot addSourceContainer(IJavaProject jproject, Str return root; } + /** + * Adds a source container to a IJavaProject. + * @param jproject The parent project + * @param containerName The name of the new source container + * @return The handle to the new source container + * @throws CoreException Creation failed + */ + public static IPackageFragmentRoot addSourceContainer(IJavaProject jproject, String containerName) throws CoreException { + return addSourceContainer(jproject, containerName, new Path[0]); + } + + /** + * Adds a source container to a IJavaProject. + * @param jproject The parent project + * @param containerName The name of the new source container + * @param exclusionFilters Exclusion filters to set + * @return The handle to the new source container + * @throws CoreException Creation failed + */ + public static IPackageFragmentRoot addSourceContainer(IJavaProject jproject, String containerName, IPath[] exclusionFilters) throws CoreException { + return addSourceContainer(jproject, containerName, new Path[0], exclusionFilters); + } + + /** + * Adds a source container to a IJavaProject. + * @param jproject The parent project + * @param containerName The name of the new source container + * @param inclusionFilters Inclusion filters to set + * @param exclusionFilters Exclusion filters to set + * @return The handle to the new source container + * @throws CoreException Creation failed + */ + public static IPackageFragmentRoot addSourceContainer(IJavaProject jproject, String containerName, IPath[] inclusionFilters, IPath[] exclusionFilters) throws CoreException { + return addSourceContainer(jproject, containerName, inclusionFilters, exclusionFilters, null); + } + + /** + * Adds a source container to a IJavaProject. + * @param jproject The parent project + * @param containerName The name of the new source container + * @param inclusionFilters Inclusion filters to set + * @param exclusionFilters Exclusion filters to set + * @param outputLocation The location where class files are written to, null for project output folder + * @return The handle to the new source container + * @throws CoreException Creation failed + */ + public static IPackageFragmentRoot addSourceContainer(IJavaProject jproject, String containerName, IPath[] inclusionFilters, IPath[] exclusionFilters, String outputLocation) throws CoreException { + return addSourceContainer(jproject, containerName, inclusionFilters, exclusionFilters, outputLocation, + new IClasspathAttribute[0]); + } public static void addToClasspath(IJavaProject jproject, IClasspathEntry cpe) throws JavaModelException { IClasspathEntry[] oldEntries= jproject.getRawClasspath(); for (IClasspathEntry oldEntry : oldEntries) { diff --git a/sandbox_web/pom.xml b/sandbox_web/pom.xml index 4f3fe4ab..2aec0a3a 100644 --- a/sandbox_web/pom.xml +++ b/sandbox_web/pom.xml @@ -24,6 +24,10 @@ org.sandbox sandbox_jface_cleanup_feature + + org.sandbox + sandbox_junit_cleanup_feature + org.sandbox sandbox_platform_helper_feature