Skip to content

Commit

Permalink
PullUp/PushDown: replace deprecated SubProgressMonitor
Browse files Browse the repository at this point in the history
  • Loading branch information
EcljpseB0T committed Dec 12, 2023
1 parent fc6a25a commit 00eb045
Show file tree
Hide file tree
Showing 6 changed files with 381 additions and 254 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.core.runtime.SubMonitor;

import org.eclipse.text.edits.MalformedTreeException;
import org.eclipse.text.edits.TextEdit;
Expand Down Expand Up @@ -91,6 +91,7 @@
import org.eclipse.jdt.core.search.SearchPattern;

import org.eclipse.jdt.internal.core.manipulation.JavaElementLabelsCore;
import org.eclipse.jdt.internal.core.manipulation.JavaManipulationPlugin;
import org.eclipse.jdt.internal.core.manipulation.StubUtility;
import org.eclipse.jdt.internal.core.manipulation.util.BasicElementLabels;
import org.eclipse.jdt.internal.core.manipulation.util.Strings;
Expand Down Expand Up @@ -120,7 +121,6 @@
import org.eclipse.jdt.internal.corext.util.Messages;
import org.eclipse.jdt.internal.corext.util.SearchUtils;

import org.eclipse.jdt.internal.core.manipulation.JavaManipulationPlugin;
import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings;


Expand Down Expand Up @@ -310,7 +310,7 @@ private static IJavaElement[] getReferencingElementsFromSameClass(IMember member
IType declaringType= member.getDeclaringType();
engine.setScope(SearchEngine.createJavaSearchScope(new IJavaElement[] { declaringType }));
engine.setStatus(status);
engine.searchPattern(new SubProgressMonitor(pm, 1));
engine.searchPattern(SubMonitor.convert(pm, 1).split(1));
Set<IJavaElement> result= new HashSet<>(3);
ICompilationUnit cu= member.getCompilationUnit();
String source= cu.getSource();
Expand Down Expand Up @@ -419,22 +419,16 @@ public PushDownRefactoringProcessor(JavaRefactoringArguments arguments, Refactor
}

private void addAllRequiredPushableMembers(List<IMember> queue, IMember member, IProgressMonitor monitor) throws JavaModelException {
monitor.beginTask(RefactoringCoreMessages.PushDownRefactoring_calculating_required, 2);
IProgressMonitor sub= new SubProgressMonitor(monitor, 1);
sub.beginTask(RefactoringCoreMessages.PushDownRefactoring_calculating_required, 2);
IMethod[] requiredMethods= ReferenceFinderUtil.getMethodsReferencedIn(new IJavaElement[] { member }, new SubProgressMonitor(sub, 1));
sub= new SubProgressMonitor(sub, 1);
sub.beginTask(RefactoringCoreMessages.PushDownRefactoring_calculating_required, requiredMethods.length);
SubMonitor sub= SubMonitor.convert(monitor,RefactoringCoreMessages.PushDownRefactoring_calculating_required, 2);
IMethod[] requiredMethods= ReferenceFinderUtil.getMethodsReferencedIn(new IJavaElement[] { member }, sub.split(1));
for (IMethod method : requiredMethods) {
if (!MethodChecks.isVirtual(method) && (method.getDeclaringType().equals(getDeclaringType()) && !queue.contains(method) && RefactoringAvailabilityTesterCore.isPushDownAvailable(method)))
queue.add(method);
}
sub.done();
for (IField field : ReferenceFinderUtil.getFieldsReferencedIn(new IJavaElement[] { member }, new SubProgressMonitor(monitor, 1))) {
for (IField field : ReferenceFinderUtil.getFieldsReferencedIn(new IJavaElement[] { member }, sub.split(1))) {
if (field.getDeclaringType().equals(getDeclaringType()) && !queue.contains(field) && RefactoringAvailabilityTesterCore.isPushDownAvailable(field))
queue.add(field);
}
monitor.done();
}

private RefactoringStatus checkAbstractMembersInDestinationClasses(IMember[] membersToPushDown, IType[] destinationClassesForAbstract) throws JavaModelException {
Expand Down Expand Up @@ -502,32 +496,32 @@ private RefactoringStatus checkAccessedTypes(IType[] subclasses, IProgressMonito

private RefactoringStatus checkElementsAccessedByModifiedMembers(IProgressMonitor pm) throws JavaModelException {
RefactoringStatus result= new RefactoringStatus();
pm.beginTask(RefactoringCoreMessages.PushDownRefactoring_check_references, 3);
IType[] subclasses= getAbstractDestinations(new SubProgressMonitor(pm, 1));
result.merge(checkAccessedTypes(subclasses, new SubProgressMonitor(pm, 1)));
result.merge(checkAccessedFields(subclasses, new SubProgressMonitor(pm, 1)));
result.merge(checkAccessedMethods(subclasses, new SubProgressMonitor(pm, 1)));
SubMonitor subMon= SubMonitor.convert(pm, RefactoringCoreMessages.PushDownRefactoring_check_references, 4);
IType[] subclasses= getAbstractDestinations(subMon.split(1));
result.merge(checkAccessedTypes(subclasses, subMon.split(1)));
result.merge(checkAccessedFields(subclasses, subMon.split(1)));
result.merge(checkAccessedMethods(subclasses, subMon.split(1)));
pm.done();
return result;
}

@Override
public RefactoringStatus checkFinalConditions(IProgressMonitor monitor, CheckConditionsContext context) throws CoreException, OperationCanceledException {
try {
monitor.beginTask(RefactoringCoreMessages.PushDownRefactoring_checking, 5);
SubMonitor subMon= SubMonitor.convert(monitor, RefactoringCoreMessages.PushDownRefactoring_checking, 5);
clearCaches();
ICompilationUnit unit= getDeclaringType().getCompilationUnit();
if (fLayer)
unit= unit.findWorkingCopy(fOwner);
resetWorkingCopies(unit);
final RefactoringStatus result= new RefactoringStatus();
result.merge(checkMembersInDestinationClasses(new SubProgressMonitor(monitor, 1)));
result.merge(checkElementsAccessedByModifiedMembers(new SubProgressMonitor(monitor, 1)));
result.merge(checkReferencesToPushedDownMembers(new SubProgressMonitor(monitor, 1)));
result.merge(checkMembersInDestinationClasses(subMon.split(1)));
result.merge(checkElementsAccessedByModifiedMembers(subMon.split(1)));
result.merge(checkReferencesToPushedDownMembers(subMon.split(1)));
if (!JdtFlags.isAbstract(getDeclaringType()) && getAbstractDeclarationInfos().length != 0)
result.merge(checkConstructorCalls(getDeclaringType(), new SubProgressMonitor(monitor, 1)));
result.merge(checkConstructorCalls(getDeclaringType(), subMon.split(1)));
else
monitor.worked(1);
subMon.setWorkRemaining(1);
if (result.hasFatalError())
return result;
List<IMember> members= new ArrayList<>(fMemberInfos.length);
Expand All @@ -537,7 +531,7 @@ public RefactoringStatus checkFinalConditions(IProgressMonitor monitor, CheckCon
}
}
fMembersToMove= members.toArray(new IMember[members.size()]);
fChangeManager= createChangeManager(new SubProgressMonitor(monitor, 1), result);
fChangeManager= createChangeManager(subMon.split(1), result);
if (result.hasFatalError())
return result;

Expand All @@ -552,12 +546,12 @@ public RefactoringStatus checkFinalConditions(IProgressMonitor monitor, CheckCon
@Override
public RefactoringStatus checkInitialConditions(IProgressMonitor monitor) throws CoreException, OperationCanceledException {
try {
monitor.beginTask(RefactoringCoreMessages.PushDownRefactoring_checking, 1);
SubMonitor subMon= SubMonitor.convert(monitor, RefactoringCoreMessages.PushDownRefactoring_checking, 2);
RefactoringStatus status= new RefactoringStatus();
status.merge(checkPossibleSubclasses(new SubProgressMonitor(monitor, 1)));
status.merge(checkPossibleSubclasses(subMon.split(1)));
if (status.hasFatalError())
return status;
status.merge(checkDeclaringType(new SubProgressMonitor(monitor, 1)));
status.merge(checkDeclaringType(subMon.split(1)));
if (status.hasFatalError())
return status;
status.merge(checkIfMembersExist());
Expand All @@ -576,13 +570,13 @@ public RefactoringStatus checkInitialConditions(IProgressMonitor monitor) throws
}

private RefactoringStatus checkMembersInDestinationClasses(IProgressMonitor monitor) throws JavaModelException {
monitor.beginTask(RefactoringCoreMessages.PushDownRefactoring_checking, 2);
SubMonitor subMon= SubMonitor.convert(monitor, RefactoringCoreMessages.PushDownRefactoring_checking, 2);
RefactoringStatus result= new RefactoringStatus();
IMember[] membersToPushDown= MemberActionInfo.getMembers(getInfosForMembersToBeCreatedInSubclassesOfDeclaringClass());

IType[] destinationClassesForNonAbstract= getAbstractDestinations(new SubProgressMonitor(monitor, 1));
IType[] destinationClassesForNonAbstract= getAbstractDestinations(subMon.split(1));
result.merge(checkNonAbstractMembersInDestinationClasses(membersToPushDown, destinationClassesForNonAbstract));
List<IMember> list= Arrays.asList(getAbstractMembers(getAbstractDestinations(new SubProgressMonitor(monitor, 1))));
List<IMember> list= Arrays.asList(getAbstractMembers(getAbstractDestinations(subMon.split(1))));

IType[] destinationClassesForAbstract= list.toArray(new IType[list.size()]);
result.merge(checkAbstractMembersInDestinationClasses(membersToPushDown, destinationClassesForAbstract));
Expand Down Expand Up @@ -619,10 +613,10 @@ private RefactoringStatus checkReferencesToPushedDownMembers(IProgressMonitor mo
IMember[] membersToPush= fields.toArray(new IMember[fields.size()]);
RefactoringStatus result= new RefactoringStatus();
List<IMember> movedMembers= Arrays.asList(MemberActionInfo.getMembers(getInfosForMembersToBeCreatedInSubclassesOfDeclaringClass()));
monitor.beginTask(RefactoringCoreMessages.PushDownRefactoring_check_references, membersToPush.length);
SubMonitor subMon= SubMonitor.convert(monitor, RefactoringCoreMessages.PushDownRefactoring_check_references, membersToPush.length);
for (IMember member : membersToPush) {
String label= createLabel(member);
for (IJavaElement element : getReferencingElementsFromSameClass(member, new SubProgressMonitor(monitor, 1), result)) {
for (IJavaElement element : getReferencingElementsFromSameClass(member, subMon.split(1), result)) {
if (movedMembers.contains(element))
continue;
if (!(element instanceof IMember))
Expand All @@ -633,7 +627,6 @@ private RefactoringStatus checkReferencesToPushedDownMembers(IProgressMonitor mo
result.addError(msg, JavaStatusContext.create(referencingMember));
}
}
monitor.done();
return result;
}

Expand Down Expand Up @@ -699,7 +692,7 @@ private void copyBodyOfPushedDownMethod(ASTRewrite targetRewrite, IMethod method

private void copyMembers(Collection<MemberVisibilityAdjustor> adjustors, Map<IMember, IncomingMemberVisibilityAdjustment> adjustments, Map<ICompilationUnit, CompilationUnitRewrite> rewrites, RefactoringStatus status, MemberActionInfo[] infos, IType[] destinations, CompilationUnitRewrite sourceRewriter, CompilationUnitRewrite unitRewriter, IProgressMonitor monitor) throws JavaModelException {
try {
monitor.beginTask(RefactoringCoreMessages.PushDownRefactoring_checking, 1);
SubMonitor subMon= SubMonitor.convert(monitor, RefactoringCoreMessages.PushDownRefactoring_checking, destinations.length * infos.length);
IType type= null;
TypeVariableMaplet[] mapping= null;
for (IType destination : destinations) {
Expand All @@ -723,7 +716,7 @@ private void copyMembers(Collection<MemberVisibilityAdjustor> adjustors, Map<IMe

adjustor.setStatus(status);
adjustor.setAdjustments(adjustments);
adjustor.adjustVisibility(new SubProgressMonitor(monitor, 1));
adjustor.adjustVisibility(subMon.split(1));
adjustments.remove(member);
adjustors.add(adjustor);
status.merge(checkProjectCompliance(getCompilationUnitRewrite(rewrites, getDeclaringType().getCompilationUnit()), type, new IMember[] {infos[offset].getMember()}));
Expand Down Expand Up @@ -803,12 +796,12 @@ private TextEditBasedChangeManager createChangeManager(final IProgressMonitor mo
Assert.isNotNull(monitor);
Assert.isNotNull(status);
try {
monitor.beginTask(RefactoringCoreMessages.PushDownRefactoring_checking, 7);
SubMonitor subMon= SubMonitor.convert(monitor, RefactoringCoreMessages.PushDownRefactoring_checking, 6);
final ICompilationUnit source= getDeclaringType().getCompilationUnit();
final CompilationUnitRewrite sourceRewriter= new CompilationUnitRewrite(source);
final Map<ICompilationUnit, CompilationUnitRewrite> rewrites= new HashMap<>(2);
rewrites.put(source, sourceRewriter);
IType[] types= getHierarchyOfDeclaringClass(new SubProgressMonitor(monitor, 1)).getSubclasses(getDeclaringType());
IType[] types= getHierarchyOfDeclaringClass(subMon.split(1)).getSubclasses(getDeclaringType());
final Set<ICompilationUnit> result= new HashSet<>(types.length + 1);
for (IType type : types) {
ICompilationUnit cu= type.getCompilationUnit();
Expand All @@ -820,9 +813,8 @@ private TextEditBasedChangeManager createChangeManager(final IProgressMonitor mo
final Map<IMember, IncomingMemberVisibilityAdjustment> adjustments= new HashMap<>();
final List<MemberVisibilityAdjustor> adjustors= new ArrayList<>();
CompilationUnitRewrite rewrite= null;
final IProgressMonitor sub= new SubProgressMonitor(monitor, 4);
try {
sub.beginTask(RefactoringCoreMessages.PushDownRefactoring_checking, result.size() * 4);
{
final SubMonitor sub= SubMonitor.convert(subMon.split(4), result.size() * 4);
for (ICompilationUnit unit : result) {
rewrite= getCompilationUnitRewrite(rewrites, unit);
if (unit.equals(sourceRewriter.getCu())) {
Expand All @@ -834,25 +826,23 @@ private TextEditBasedChangeManager createChangeManager(final IProgressMonitor mo
declareMethodAbstract(method, sourceRewriter, rewrite);
}
}
final IMember[] members= getAbstractMembers(getAbstractDestinations(new SubProgressMonitor(monitor, 1)));
final IMember[] members= getAbstractMembers(getAbstractDestinations(sub.split(1)));
final IType[] classes= new IType[members.length];
for (int offset= 0; offset < members.length; offset++) {
classes[offset]= (IType) members[offset];
}
copyMembers(adjustors, adjustments, rewrites, status, getAbstractMemberInfos(), classes, sourceRewriter, rewrite, sub);
copyMembers(adjustors, adjustments, rewrites, status, getAffectedMemberInfos(), getAbstractDestinations(new SubProgressMonitor(monitor, 1)), sourceRewriter, rewrite, sub);
copyMembers(adjustors, adjustments, rewrites, status, getAbstractMemberInfos(), classes, sourceRewriter, rewrite, sub.split(1));
IType[] abstractDestinations= getAbstractDestinations(sub.split(1));
copyMembers(adjustors, adjustments, rewrites, status, getAffectedMemberInfos(), abstractDestinations, sourceRewriter, rewrite, sub.split(1));
if (monitor.isCanceled()) {
throw new OperationCanceledException();
}
}
removeOverrideAnnotation(rewrites);

} finally {
sub.done();
}
if (!adjustors.isEmpty() && !adjustments.isEmpty()) {
final MemberVisibilityAdjustor adjustor= adjustors.get(0);
adjustor.rewriteVisibility(new SubProgressMonitor(monitor, 1));
adjustor.rewriteVisibility(subMon.split(1));
}
final TextEditBasedChangeManager manager= new TextEditBasedChangeManager();
for (Map.Entry<ICompilationUnit, CompilationUnitRewrite> entry : rewrites.entrySet()) {
Expand Down Expand Up @@ -1001,28 +991,21 @@ private MemberActionInfo[] getAbstractMemberInfos() throws JavaModelException {

public IMember[] getAdditionalRequiredMembers(IProgressMonitor monitor) throws JavaModelException {
IMember[] members= MemberActionInfo.getMembers(getInfosForMembersToBeCreatedInSubclassesOfDeclaringClass());
monitor.beginTask(RefactoringCoreMessages.PushDownRefactoring_calculating_required, members.length);// not
// true,
// but
// not
// easy
// to
// give
// anything
// better
List<IMember> queue= new ArrayList<>(members.length);
queue.addAll(Arrays.asList(members));
List<IMember> queue= new ArrayList<>(Arrays.asList(members));
SubMonitor submon= SubMonitor.convert(monitor, RefactoringCoreMessages.PushDownRefactoring_calculating_required, queue.size());
if (queue.isEmpty())
return new IMember[0];
int i= 0;
IMember current;
do {
current= queue.get(i);
addAllRequiredPushableMembers(queue, current, new SubProgressMonitor(monitor, 1));
addAllRequiredPushableMembers(queue, current, submon.split(1));
i++;
submon.setWorkRemaining(queue.size());
if (queue.size() == i)
current= null;
} while (current != null);
submon.worked(queue.size());
queue.removeAll(Arrays.asList(members));// report only additional
return queue.toArray(new IMember[queue.size()]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.util.Set;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.core.runtime.SubMonitor;

import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IJavaElement;
Expand Down Expand Up @@ -60,11 +60,10 @@ public static IType[] getTypesReferencedIn(IJavaElement[] elements, WorkingCopyO

private static SearchMatch[] getTypeReferencesIn(IJavaElement[] elements, WorkingCopyOwner owner, IProgressMonitor pm) throws JavaModelException {
List<SearchMatch> referencedTypes= new ArrayList<>();
pm.beginTask("", elements.length); //$NON-NLS-1$
SubMonitor subMon= SubMonitor.convert(pm,"", elements.length); //$NON-NLS-1$
for (IJavaElement element : elements) {
referencedTypes.addAll(getTypeReferencesIn(element, owner, new SubProgressMonitor(pm, 1)));
referencedTypes.addAll(getTypeReferencesIn(element, owner, subMon.newChild(1)));
}
pm.done();
return referencedTypes.toArray(new SearchMatch[referencedTypes.size()]);
}

Expand All @@ -91,11 +90,10 @@ public static IField[] getFieldsReferencedIn(IJavaElement[] elements, WorkingCop

private static SearchMatch[] getFieldReferencesIn(IJavaElement[] elements, WorkingCopyOwner owner, IProgressMonitor pm) throws JavaModelException {
List<SearchMatch> referencedFields= new ArrayList<>();
pm.beginTask("", elements.length); //$NON-NLS-1$
SubMonitor subMon= SubMonitor.convert(pm,"", elements.length); //$NON-NLS-1$
for (IJavaElement element : elements) {
referencedFields.addAll(getFieldReferencesIn(element, owner, new SubProgressMonitor(pm, 1)));
referencedFields.addAll(getFieldReferencesIn(element, owner,subMon.newChild(1)));
}
pm.done();
return referencedFields.toArray(new SearchMatch[referencedFields.size()]);
}

Expand All @@ -122,11 +120,10 @@ public static IMethod[] getMethodsReferencedIn(IJavaElement[] elements, WorkingC

private static SearchMatch[] getMethodReferencesIn(IJavaElement[] elements, WorkingCopyOwner owner, IProgressMonitor pm) throws JavaModelException {
List<SearchMatch> referencedMethods= new ArrayList<>();
pm.beginTask("", elements.length); //$NON-NLS-1$
SubMonitor subMon= SubMonitor.convert(pm,"", elements.length); //$NON-NLS-1$
for (IJavaElement element : elements) {
referencedMethods.addAll(getMethodReferencesIn(element, owner, new SubProgressMonitor(pm, 1)));
referencedMethods.addAll(getMethodReferencesIn(element, owner, subMon.newChild(1)));
}
pm.done();
return referencedMethods.toArray(new SearchMatch[referencedMethods.size()]);
}

Expand Down
3 changes: 2 additions & 1 deletion org.eclipse.jdt.ui.tests.refactoring/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ Require-Bundle:
org.eclipse.ui,
org.eclipse.ui.ide,
org.eclipse.ui.workbench.texteditor,
org.junit
org.junit,
org.eclipse.core.tests.harness
Bundle-RequiredExecutionEnvironment: JavaSE-17
Eclipse-BundleShape: dir
Import-Package: org.junit.jupiter.api
Loading

0 comments on commit 00eb045

Please sign in to comment.