Skip to content

Commit

Permalink
empty skeleton
Browse files Browse the repository at this point in the history
  • Loading branch information
carstenartur committed Oct 13, 2024
1 parent dbe6890 commit e000d49
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 224 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
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.CompilationUnitRewriteOperation;
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;
Expand Down Expand Up @@ -57,15 +57,15 @@ public String getPreview(boolean i) {
* loop var used within
*/
public void findOperations(final CompilationUnit compilationUnit,
final Set<CompilationUnitRewriteOperation> operations, final Set<ASTNode> nodesprocessed,
final Set<CompilationUnitRewriteOperationWithSourceRange> operations, final Set<ASTNode> nodesprocessed,
boolean createForOnlyIfVarUsed) {
junitfound.find(this, compilationUnit, operations, nodesprocessed, createForOnlyIfVarUsed);
}

public CompilationUnitRewriteOperation rewrite(final ReferenceHolder<Integer, MonitorHolder> hit) {
return new CompilationUnitRewriteOperation() {
public CompilationUnitRewriteOperationWithSourceRange rewrite(final ReferenceHolder<Integer, MonitorHolder> hit) {
return new CompilationUnitRewriteOperationWithSourceRange() {
@Override
public void rewriteAST(final CompilationUnitRewrite cuRewrite, final LinkedProposalModelCore linkedModel)
public void rewriteASTInternal(final CompilationUnitRewrite cuRewrite, final LinkedProposalModelCore linkedModel)
throws CoreException {
TextEditGroup group= createTextEditGroup(MultiFixMessages.JUnitCleanUp_description, cuRewrite);
TightSourceRangeComputer rangeComputer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
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.CompilationUnitRewriteOperation;
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;
Expand All @@ -44,7 +44,7 @@ protected static boolean isOfType(ITypeBinding typeBinding, String typename) {
}

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

public abstract void rewrite(JUnitCleanUpFixCore useExplicitEncodingFixCore, T holder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,69 +14,28 @@
package org.sandbox.jdt.internal.corext.fix.helper;

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

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.IBinding;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
import org.eclipse.jdt.internal.corext.dom.ASTNodeFactory;
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation;
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.ASTProcessor;
import org.sandbox.jdt.internal.common.ReferenceHolder;
import org.sandbox.jdt.internal.corext.fix.JUnitCleanUpFixCore;

/**
*
* SubProgressMonitor has been deprecated What is affected: Clients that refer
* to org.eclipse.core.runtime.SubProgressMonitor.
*
* Description: org.eclipse.core.runtime.SubProgressMonitor has been deprecated
* and replaced by org.eclipse.core.runtime.SubMonitor.
*
* Action required:
*
* Calls to IProgressMonitor.beginTask on the root monitor should be replaced by
* a call to SubMonitor.convert. Keep the returned SubMonitor around as a local
* variable and refer to it instead of the root monitor for the remainder of the
* method. All calls to SubProgressMonitor(IProgressMonitor, int) should be
* replaced by calls to SubMonitor.split(int). If a SubProgressMonitor is
* constructed using the SUPPRESS_SUBTASK_LABEL flag, replace it with the
* two-argument version of SubMonitor.split(int, int) using
* SubMonitor.SUPPRESS_SUBTASK as the second argument. It is not necessary to
* call done on an instance of SubMonitor. Example:
*
* Consider the following example: void someMethod(IProgressMonitor pm) {
* pm.beginTask("Main Task", 100); SubProgressMonitor subMonitor1= new
* SubProgressMonitor(pm, 60); try { doSomeWork(subMonitor1); } finally {
* subMonitor1.done(); } SubProgressMonitor subMonitor2= new
* SubProgressMonitor(pm, 40); try { doSomeMoreWork(subMonitor2); } finally {
* subMonitor2.done(); } } The above code should be refactored to this: void
* someMethod(IProgressMonitor pm) { SubMonitor subMonitor =
* SubMonitor.convert(pm, "Main Task", 100); doSomeWork(subMonitor.split(60));
* doSomeMoreWork(subMonitor.split(40)); }
*
*/
public class JUnitPlugin extends
AbstractTool<ReferenceHolder<Integer, JUnitPlugin.MonitorHolder>> {

public static final String CLASS_INSTANCE_CREATION = "ClassInstanceCreation"; //$NON-NLS-1$
public static final String METHODINVOCATION = "MethodInvocation"; //$NON-NLS-1$

public static class MonitorHolder {
public MethodInvocation minv;
public String minvname;
Expand All @@ -86,42 +45,10 @@ public static class MonitorHolder {

@Override
public void find(JUnitCleanUpFixCore fixcore, CompilationUnit compilationUnit,
Set<CompilationUnitRewriteOperation> operations, Set<ASTNode> nodesprocessed,
Set<CompilationUnitRewriteOperationWithSourceRange> operations, Set<ASTNode> nodesprocessed,
boolean createForOnlyIfVarUsed) {
int i = 0;
ReferenceHolder<Integer, MonitorHolder> dataholder = new ReferenceHolder<>();

Check warning

Code scanning / PMD

Avoid unused local variables such as 'element'. Warning

Avoid unused local variables such as 'dataholder'.

Check notice

Code scanning / Pmd (reported by Codacy)

Avoid unused local variables such as 'dataholder'. Note

Avoid unused local variables such as 'dataholder'.

ASTProcessor<ReferenceHolder<Integer, MonitorHolder>, Integer, MonitorHolder> astp = new ASTProcessor<>(
dataholder, nodesprocessed);
astp.callMethodInvocationVisitor(IProgressMonitor.class, "beginTask", (node, holder) -> { //$NON-NLS-1$
if (node.arguments().size() != 2) {
return true;
}
System.out.println("begintask[" + node.getStartPosition() + "] " + node.getNodeType() + " :" + node); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
SimpleName sn = ASTNodes.as(node.getExpression(), SimpleName.class);
if (sn != null) {
IBinding ibinding = sn.resolveBinding();
String name = ibinding.getName();
MonitorHolder mh = new MonitorHolder();
mh.minv = node;
mh.minvname = name;
mh.nodesprocessed = nodesprocessed;
holder.put(holder.size(), mh);
}
return true;
}, s -> ASTNodes.getTypedAncestor(s, Block.class))
.callClassInstanceCreationVisitor(SubProgressMonitor.class, (node, holder) -> {
MonitorHolder mh = holder.get(holder.size() - 1);
List<?> arguments = node.arguments();
SimpleName simplename = (SimpleName) arguments.get(0);
if (!mh.minvname.equals(simplename.getIdentifier())) {
return true;
}
System.out.println("init[" + node.getStartPosition() + "] " + node.getNodeType() + " :" + node); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
mh.setofcic.add(node);
operations.add(fixcore.rewrite(holder));
return true;
}).build(compilationUnit);
}

@Override
Expand All @@ -130,83 +57,18 @@ public void rewrite(JUnitCleanUpFixCore upp, final ReferenceHolder<Integer, Moni
ASTRewrite rewrite = cuRewrite.getASTRewrite();

Check warning

Code scanning / PMD

Avoid unused local variables such as 'element'. Warning

Avoid unused local variables such as 'rewrite'.

Check notice

Code scanning / Pmd (reported by Codacy)

Avoid unused local variables such as 'rewrite'. Note

Avoid unused local variables such as 'rewrite'.
AST ast = cuRewrite.getRoot().getAST();

Check warning

Code scanning / PMD

Avoid unused local variables such as 'element'. Warning

Avoid unused local variables such as 'ast'.

Check notice

Code scanning / Pmd (reported by Codacy)

Avoid unused local variables such as 'ast'. Note

Avoid unused local variables such as 'ast'.
ImportRewrite importRemover = cuRewrite.getImportRewrite();

Check warning

Code scanning / PMD

Avoid unused local variables such as 'element'. Warning

Avoid unused local variables such as 'importRemover'.

Check notice

Code scanning / Pmd (reported by Codacy)

Avoid unused local variables such as 'importRemover'. Note

Avoid unused local variables such as 'importRemover'.
Set<ASTNode> nodesprocessed = hit.get(hit.size() - 1).nodesprocessed;
for (Entry<Integer, MonitorHolder> entry : hit.entrySet()) {

MonitorHolder mh = entry.getValue();
MethodInvocation minv = mh.minv;
String identifier = "subMonitor"; //$NON-NLS-1$
if (!nodesprocessed.contains(minv)) {
nodesprocessed.add(minv);
System.out.println("rewrite methodinvocation [" + minv.getStartPosition() + "] " + minv); //$NON-NLS-1$ //$NON-NLS-2$
List<ASTNode> arguments = minv.arguments();

/**
* Here we process the "beginTask" and change it to "SubMonitor.convert"
*
* monitor.beginTask(NewWizardMessages.NewSourceFolderWizardPage_operation, 3);
* SubMonitor subMonitor =
* SubMonitor.convert(monitor,NewWizardMessages.NewSourceFolderWizardPage_operation,
* 3);
*
*/

SingleVariableDeclaration newVariableDeclarationStatement = ast.newSingleVariableDeclaration();

newVariableDeclarationStatement.setName(ast.newSimpleName(identifier));
newVariableDeclarationStatement
.setType(ast.newSimpleType(addImport(SubMonitor.class.getCanonicalName(), cuRewrite, ast)));

MethodInvocation staticCall = ast.newMethodInvocation();
staticCall.setExpression(ASTNodeFactory.newName(ast, SubMonitor.class.getSimpleName()));
staticCall.setName(ast.newSimpleName("convert")); //$NON-NLS-1$
List<ASTNode> staticCallArguments = staticCall.arguments();
staticCallArguments.add(
ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(minv.getExpression())));
staticCallArguments
.add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(arguments.get(0))));
staticCallArguments
.add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(arguments.get(1))));
newVariableDeclarationStatement.setInitializer(staticCall);

ASTNodes.replaceButKeepComment(rewrite, minv, newVariableDeclarationStatement, group);
System.out.println("result " + staticCall); //$NON-NLS-1$
}
for (ClassInstanceCreation submon : mh.setofcic) {
ASTNode origarg = (ASTNode) submon.arguments().get(1);
System.out.println("rewrite spminstance [" + submon.getStartPosition() + "] " + submon); //$NON-NLS-1$ //$NON-NLS-2$
/**
*
* IProgressMonitor subProgressMonitor= new SubProgressMonitor(monitor, 1);
* IProgressMonitor subProgressMonitor= subMonitor.split(1);
*
*/
MethodInvocation newMethodInvocation2 = ast.newMethodInvocation();
newMethodInvocation2.setName(ast.newSimpleName("split")); //$NON-NLS-1$
newMethodInvocation2.setExpression(ASTNodeFactory.newName(ast, identifier));
List<ASTNode> splitCallArguments = newMethodInvocation2.arguments();

splitCallArguments
.add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(origarg)));
ASTNodes.replaceButKeepComment(rewrite, submon, newMethodInvocation2, group);
importRemover.removeImport(SubProgressMonitor.class.getCanonicalName());
}
}

}

@Override
public String getPreview(boolean afterRefactoring) {
if (!afterRefactoring) {
return """
monitor.beginTask(NewWizardMessages.NewSourceFolderWizardPage_operation, 3);
IProgressMonitor subProgressMonitor= new SubProgressMonitor(monitor, 1);
IProgressMonitor subProgressMonitor2= new SubProgressMonitor(monitor, 2);
;
"""; //$NON-NLS-1$
}
return """
SubMonitor subMonitor=SubMonitor.convert(monitor,NewWizardMessages.NewSourceFolderWizardPage_operation,3);
IProgressMonitor subProgressMonitor= subMonitor.split(1);
IProgressMonitor subProgressMonitor2= subMonitor.split(2);
;
"""; //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
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.CompilationUnitRewriteOperation;
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;
Expand Down Expand Up @@ -69,7 +69,7 @@ public ICleanUpFix createFix(final CleanUpContext context) throws CoreException
|| !JavaModelUtil.is1d8OrHigher(compilationUnit.getJavaElement().getJavaProject())) {
return null;
}
Set<CompilationUnitRewriteOperation> operations= new LinkedHashSet<>();
Set<CompilationUnitRewriteOperationWithSourceRange> operations= new LinkedHashSet<>();
Set<ASTNode> nodesprocessed= new HashSet<>();
computeFixSet.forEach(i -> i.findOperations(compilationUnit, operations, nodesprocessed,
true));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
JavaFeatureTabPage_CheckboxName_JUNIT_CLEANUP=JFACE_CLEANUP
JavaFeatureTabPage_CheckboxName_JUNIT_CLEANUP=JUNIT_CLEANUP
JavaFeatureTabPage_GroupName_Java1d8=Java 8
Loading

0 comments on commit e000d49

Please sign in to comment.