Skip to content

Commit ecffe65

Browse files
vogellajukzi
authored andcommitted
Reduce usage of SubProgressMonitor
Replaces some usage of SubProgressMonitor with SubMonitor - SubMonitor does not need to call done() - split will check for cancellation and throw OperationCanceledException - newChild will NOT check for cancellation CompositeChange#initializeValidationData also consumed more ticks than allocated, this has been corrected.
1 parent bc46354 commit ecffe65

File tree

6 files changed

+128
-164
lines changed

6 files changed

+128
-164
lines changed

bundles/org.eclipse.ltk.core.refactoring/src/org/eclipse/ltk/core/refactoring/Change.java

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,39 +21,34 @@
2121
import org.eclipse.core.runtime.Platform;
2222

2323
/**
24-
* An abstract base implementation for object representing a generic change
25-
* to the workbench. A <code>Change</code> object is typically created by
26-
* calling {@link Refactoring#createChange(IProgressMonitor)}. This class should be
27-
* subclassed by clients wishing to provide new changes.
24+
* An abstract base implementation for object representing a generic change to the workbench. A
25+
* <code>Change</code> object is typically created by calling
26+
* {@link Refactoring#createChange(IProgressMonitor)}. This class should be subclassed by clients
27+
* wishing to provide new changes.
2828
* <p>
29-
* Changes are best executed by using a {@link PerformChangeOperation}. If clients
30-
* execute a change directly then the following life cycle has to be honored:</p>
29+
* Changes are best executed by using a {@link PerformChangeOperation}. If clients execute a change
30+
* directly then the following life cycle has to be honored:
31+
* </p>
3132
* <ul>
32-
* <li>After a single change or a tree of changes has been created, the
33-
* method <code>initializeValidationData</code> has to be called.</li>
34-
* <li>The method <code>isValid</code> can be used to determine if a change
35-
* can still be applied to the workspace. If the method returns a {@link
36-
* RefactoringStatus} with a severity of FATAL then the change has to be
37-
* treated as invalid. Performing an invalid change isn't allowed and
38-
* results in an unspecified result. This method can be called multiple
39-
* times.
40-
* <li>Then the method <code>perform</code> can be called. A disabled change
41-
* must not be executed. The <code>perform</code> method can only be called
42-
* once. After a change has been executed, only the method <code>dispose</code>
43-
* must be called.</li>
44-
* <li>the method <code>dispose</code> has to be called either after the
45-
* <code>perform</code> method
46-
* has been called or if a change is no longer needed. The second case
47-
* for example occurs when the undo stack gets flushed and all change
48-
* objects managed by the undo stack are no longer needed. The method
49-
* <code>dispose</code> is typically implemented to unregister listeners
50-
* registered during the
51-
* method <code>initializeValidationData</code>. There is no guarantee
52-
* that <code>initializeValidationData</code>, <code>isValid</code>,
53-
* or <code>perform</code> has been called before <code>dispose</code>
54-
* is called.
33+
* <li>After a single change or a tree of changes has been created, the method
34+
* <code>initializeValidationData</code> has to be called.</li>
35+
* <li>The method <code>isValid</code> can be used to determine if a change can still be applied to
36+
* the workspace. If the method returns a {@link RefactoringStatus} with a severity of FATAL then
37+
* the change has to be treated as invalid. Performing an invalid change isn't allowed and results
38+
* in an unspecified result. This method can be called multiple times.
39+
* <li>Then the method <code>perform</code> can be called. A disabled change must not be executed.
40+
* The <code>perform</code> method can only be called once. After a change has been executed, only
41+
* the method <code>dispose</code> must be called.</li>
42+
* <li>the method <code>dispose</code> has to be called either after the <code>perform</code> method
43+
* has been called or if a change is no longer needed. The second case for example occurs when the
44+
* undo stack gets flushed and all change objects managed by the undo stack are no longer needed.
45+
* The method <code>dispose</code> is typically implemented to unregister listeners registered
46+
* during the method <code>initializeValidationData</code>. There is no guarantee that
47+
* <code>initializeValidationData</code>, <code>isValid</code>, or <code>perform</code> has been
48+
* called before <code>dispose</code> is called.
5549
* </ul>
5650
* Here is a code snippet that can be used to execute a change:
51+
*
5752
* <pre>
5853
* Change change= createChange();
5954
* try {
@@ -63,26 +58,25 @@
6358
*
6459
* if (!change.isEnabled())
6560
* return;
66-
* RefactoringStatus valid= change.isValid(new SubProgressMonitor(pm, 1));
61+
* RefactoringStatus valid= change.isValid(subMonitor.newChild(1));
6762
* if (valid.hasFatalError())
6863
* return;
69-
* Change undo= change.perform(new SubProgressMonitor(pm, 1));
64+
* Change undo= change.perform(subMonitor.newChild(1));
7065
* if (undo != null) {
71-
* undo.initializeValidationData(new SubProgressMonitor(pm, 1));
66+
* undo.initializeValidationData(subMonitor.newChild(1));
7267
* // do something with the undo object
7368
* }
7469
* } finally {
7570
* change.dispose();
7671
* }
7772
* </pre>
7873
* <p>
79-
* It is important that implementors of this abstract class provide an adequate
80-
* implementation of <code>isValid</code> and that they provide an undo change
81-
* via the return value of the method <code>perform</code>. If no undo can be
82-
* provided then the <code>perform</code> method is allowed to return <code>null</code>. But
83-
* implementors should be aware that not providing an undo object for a change
84-
* object that is part of a larger change tree will result in the fact that for
85-
* the whole change tree no undo object will be present.
74+
* It is important that implementors of this abstract class provide an adequate implementation of
75+
* <code>isValid</code> and that they provide an undo change via the return value of the method
76+
* <code>perform</code>. If no undo can be provided then the <code>perform</code> method is allowed
77+
* to return <code>null</code>. But implementors should be aware that not providing an undo object
78+
* for a change object that is part of a larger change tree will result in the fact that for the
79+
* whole change tree no undo object will be present.
8680
* </p>
8781
* <p>
8882
* Changes which are returned as top-level changes (e.g. by <code>Refactoring.createChange()</code>)

bundles/org.eclipse.ltk.core.refactoring/src/org/eclipse/ltk/core/refactoring/CompositeChange.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.eclipse.core.runtime.OperationCanceledException;
2727
import org.eclipse.core.runtime.SafeRunner;
2828
import org.eclipse.core.runtime.SubMonitor;
29-
import org.eclipse.core.runtime.SubProgressMonitor;
3029

3130
import org.eclipse.ltk.internal.core.refactoring.RefactoringCoreMessages;
3231
import org.eclipse.ltk.internal.core.refactoring.RefactoringCorePlugin;
@@ -213,10 +212,9 @@ public void setEnabled(boolean enabled) {
213212
*/
214213
@Override
215214
public void initializeValidationData(IProgressMonitor pm) {
216-
pm.beginTask("", fChanges.size()); //$NON-NLS-1$
215+
SubMonitor subMonitor= SubMonitor.convert(pm, fChanges.size());
217216
for (Change change : fChanges) {
218-
change.initializeValidationData(new SubProgressMonitor(pm, 1));
219-
pm.worked(1);
217+
change.initializeValidationData(subMonitor.newChild(1));
220218
}
221219
}
222220

@@ -235,17 +233,16 @@ public void initializeValidationData(IProgressMonitor pm) {
235233
@Override
236234
public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException {
237235
RefactoringStatus result= new RefactoringStatus();
238-
pm.beginTask("", fChanges.size()); //$NON-NLS-1$
236+
237+
SubMonitor subMonitor= SubMonitor.convert(pm, fChanges.size());
239238
for (Iterator<Change> iter= fChanges.iterator(); iter.hasNext() && !result.hasFatalError();) {
240239
Change change= iter.next();
241-
if (change.isEnabled())
242-
result.merge(change.isValid(new SubProgressMonitor(pm, 1)));
243-
else
240+
if (change.isEnabled()) {
241+
result.merge(change.isValid(subMonitor.split(1)));
242+
} else {
244243
pm.worked(1);
245-
if (pm.isCanceled())
246-
throw new OperationCanceledException();
244+
}
247245
}
248-
pm.done();
249246
return result;
250247
}
251248

bundles/org.eclipse.ltk.core.refactoring/src/org/eclipse/ltk/core/refactoring/resource/DeleteResourceChange.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,16 +164,14 @@ public Change perform(IProgressMonitor pm) throws CoreException {
164164

165165
private static void saveFileIfNeeded(IFile file, IProgressMonitor pm) throws CoreException {
166166
ITextFileBuffer buffer= FileBuffers.getTextFileBufferManager().getTextFileBuffer(file.getFullPath(), LocationKind.IFILE);
167+
SubMonitor subMonitor= SubMonitor.convert(pm, 2);
167168
if (buffer != null && buffer.isDirty() && buffer.isStateValidated() && buffer.isSynchronized()) {
168-
SubMonitor subMonitor= SubMonitor.convert(pm, 2);
169-
pm.beginTask("", 2); //$NON-NLS-1$
170169
buffer.commit(subMonitor.newChild(1), false);
171170
file.refreshLocal(IResource.DEPTH_ONE, subMonitor.newChild(1));
172-
pm.done();
171+
buffer.commit(subMonitor.newChild(1), false);
172+
file.refreshLocal(IResource.DEPTH_ONE, subMonitor.newChild(1));
173173
} else {
174-
pm.beginTask("", 1); //$NON-NLS-1$
175-
pm.worked(1);
176-
pm.done();
174+
subMonitor.worked(2);
177175
}
178176
}
179177

bundles/org.eclipse.ltk.core.refactoring/src/org/eclipse/ltk/core/refactoring/resource/MoveRenameResourceChange.java

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.eclipse.core.runtime.IProgressMonitor;
2020
import org.eclipse.core.runtime.OperationCanceledException;
2121
import org.eclipse.core.runtime.SubMonitor;
22-
import org.eclipse.core.runtime.SubProgressMonitor;
2322

2423
import org.eclipse.core.resources.IContainer;
2524
import org.eclipse.core.resources.IResource;
@@ -135,30 +134,23 @@ public final Change perform(IProgressMonitor monitor) throws CoreException, Oper
135134
}
136135

137136
private Change performDestinationDelete(IResource newResource, IProgressMonitor monitor) throws CoreException {
138-
monitor.beginTask(RefactoringCoreMessages.MoveResourceChange_progress_delete_destination, 3);
139-
try {
140-
DeleteResourceChange deleteChange= new DeleteResourceChange(newResource.getFullPath(), true);
141-
deleteChange.initializeValidationData(new SubProgressMonitor(monitor, 1));
142-
RefactoringStatus deleteStatus= deleteChange.isValid(new SubProgressMonitor(monitor, 1));
143-
if (!deleteStatus.hasFatalError()) {
144-
return deleteChange.perform(new SubProgressMonitor(monitor, 1));
145-
}
146-
return null;
147-
} finally {
148-
monitor.done();
137+
SubMonitor subMonitor= SubMonitor.convert(monitor, RefactoringCoreMessages.MoveResourceChange_progress_delete_destination, 3);
138+
139+
DeleteResourceChange deleteChange= new DeleteResourceChange(newResource.getFullPath(), true);
140+
deleteChange.initializeValidationData(subMonitor.newChild(1));
141+
RefactoringStatus deleteStatus= deleteChange.isValid(subMonitor.newChild(1));
142+
if (!deleteStatus.hasFatalError()) {
143+
return deleteChange.perform(subMonitor.newChild(1));
149144
}
145+
return null;
150146
}
151147

152148
private void performSourceRestore(IProgressMonitor monitor) throws CoreException {
153-
monitor.beginTask(RefactoringCoreMessages.MoveResourceChange_progress_restore_source, 3);
154-
try {
155-
fRestoreSourceChange.initializeValidationData(new SubProgressMonitor(monitor, 1));
156-
RefactoringStatus restoreStatus= fRestoreSourceChange.isValid(new SubProgressMonitor(monitor, 1));
157-
if (!restoreStatus.hasFatalError()) {
158-
fRestoreSourceChange.perform(new SubProgressMonitor(monitor, 1));
159-
}
160-
} finally {
161-
monitor.done();
149+
SubMonitor subMonitor= SubMonitor.convert(monitor, RefactoringCoreMessages.MoveResourceChange_progress_restore_source, 3);
150+
fRestoreSourceChange.initializeValidationData(subMonitor.newChild(1));
151+
RefactoringStatus restoreStatus= fRestoreSourceChange.isValid(subMonitor.newChild(1));
152+
if (!restoreStatus.hasFatalError()) {
153+
fRestoreSourceChange.perform(subMonitor.newChild(1));
162154
}
163155
}
164156

bundles/org.eclipse.ltk.core.refactoring/src/org/eclipse/ltk/core/refactoring/resource/MoveResourceChange.java

Lines changed: 43 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616
import org.eclipse.core.runtime.CoreException;
1717
import org.eclipse.core.runtime.IPath;
1818
import org.eclipse.core.runtime.IProgressMonitor;
19-
import org.eclipse.core.runtime.NullProgressMonitor;
2019
import org.eclipse.core.runtime.OperationCanceledException;
21-
import org.eclipse.core.runtime.SubProgressMonitor;
20+
import org.eclipse.core.runtime.SubMonitor;
2221

2322
import org.eclipse.core.resources.IContainer;
2423
import org.eclipse.core.resources.IResource;
@@ -96,70 +95,56 @@ public void setDescriptor(ChangeDescriptor descriptor) {
9695

9796
@Override
9897
public final Change perform(IProgressMonitor monitor) throws CoreException, OperationCanceledException {
99-
try {
100-
if (monitor == null)
101-
monitor= new NullProgressMonitor();
102-
103-
monitor.beginTask(getName(), 4);
104-
105-
Change deleteUndo= null;
106-
107-
// delete destination if required
108-
IResource resourceAtDestination= fTarget.findMember(fSource.getName());
109-
if (resourceAtDestination != null && resourceAtDestination.exists()) {
110-
deleteUndo= performDestinationDelete(resourceAtDestination, new SubProgressMonitor(monitor, 1));
111-
} else {
112-
monitor.worked(1);
113-
}
114-
115-
// move resource
116-
long currentStamp= fSource.getModificationStamp();
117-
IPath destinationPath= fTarget.getFullPath().append(fSource.getName());
118-
fSource.move(destinationPath, IResource.KEEP_HISTORY | IResource.SHALLOW, new SubProgressMonitor(monitor, 2));
119-
resourceAtDestination= ResourcesPlugin.getWorkspace().getRoot().findMember(destinationPath);
120-
121-
// restore timestamp at destination
122-
if (fStampToRestore != IResource.NULL_STAMP) {
123-
resourceAtDestination.revertModificationStamp(fStampToRestore);
124-
}
125-
126-
// restore file at source
127-
if (fRestoreSourceChange != null) {
128-
performSourceRestore(new SubProgressMonitor(monitor, 1));
129-
} else {
130-
monitor.worked(1);
131-
}
132-
return new MoveResourceChange(resourceAtDestination, fSource.getParent(), currentStamp, deleteUndo);
133-
} finally {
134-
monitor.done();
98+
SubMonitor subMonitor= SubMonitor.convert(monitor, getName(), 4);
99+
100+
Change deleteUndo= null;
101+
102+
// delete destination if required
103+
IResource resourceAtDestination= fTarget.findMember(fSource.getName());
104+
if (resourceAtDestination != null && resourceAtDestination.exists()) {
105+
deleteUndo= performDestinationDelete(resourceAtDestination, subMonitor.newChild(1));
106+
} else {
107+
subMonitor.worked(1);
108+
}
109+
110+
// move resource
111+
long currentStamp= fSource.getModificationStamp();
112+
IPath destinationPath= fTarget.getFullPath().append(fSource.getName());
113+
fSource.move(destinationPath, IResource.KEEP_HISTORY | IResource.SHALLOW, subMonitor.newChild(2));
114+
resourceAtDestination= ResourcesPlugin.getWorkspace().getRoot().findMember(destinationPath);
115+
116+
// restore timestamp at destination
117+
if (fStampToRestore != IResource.NULL_STAMP) {
118+
resourceAtDestination.revertModificationStamp(fStampToRestore);
119+
}
120+
121+
// restore file at source
122+
if (fRestoreSourceChange != null) {
123+
performSourceRestore(subMonitor.newChild(1));
124+
} else {
125+
subMonitor.worked(1);
135126
}
127+
return new MoveResourceChange(resourceAtDestination, fSource.getParent(), currentStamp, deleteUndo);
128+
136129
}
137130

138131
private Change performDestinationDelete(IResource newResource, IProgressMonitor monitor) throws CoreException {
139-
monitor.beginTask(RefactoringCoreMessages.MoveResourceChange_progress_delete_destination, 3);
140-
try {
141-
DeleteResourceChange deleteChange= new DeleteResourceChange(newResource.getFullPath(), true);
142-
deleteChange.initializeValidationData(new SubProgressMonitor(monitor, 1));
143-
RefactoringStatus deleteStatus= deleteChange.isValid(new SubProgressMonitor(monitor, 1));
144-
if (!deleteStatus.hasFatalError()) {
145-
return deleteChange.perform(new SubProgressMonitor(monitor, 1));
146-
}
147-
return null;
148-
} finally {
149-
monitor.done();
132+
SubMonitor subMonitor= SubMonitor.convert(monitor, RefactoringCoreMessages.MoveResourceChange_progress_delete_destination, 3);
133+
DeleteResourceChange deleteChange= new DeleteResourceChange(newResource.getFullPath(), true);
134+
deleteChange.initializeValidationData(subMonitor.newChild(1));
135+
RefactoringStatus deleteStatus= deleteChange.isValid(subMonitor.newChild(1));
136+
if (!deleteStatus.hasFatalError()) {
137+
return deleteChange.perform(subMonitor.newChild(1));
150138
}
139+
return null;
151140
}
152141

153142
private void performSourceRestore(IProgressMonitor monitor) throws CoreException {
154-
monitor.beginTask(RefactoringCoreMessages.MoveResourceChange_progress_restore_source, 3);
155-
try {
156-
fRestoreSourceChange.initializeValidationData(new SubProgressMonitor(monitor, 1));
157-
RefactoringStatus restoreStatus= fRestoreSourceChange.isValid(new SubProgressMonitor(monitor, 1));
158-
if (!restoreStatus.hasFatalError()) {
159-
fRestoreSourceChange.perform(new SubProgressMonitor(monitor, 1));
160-
}
161-
} finally {
162-
monitor.done();
143+
SubMonitor subMonitor= SubMonitor.convert(monitor, RefactoringCoreMessages.MoveResourceChange_progress_restore_source, 3);
144+
fRestoreSourceChange.initializeValidationData(subMonitor.newChild(1));
145+
RefactoringStatus restoreStatus= fRestoreSourceChange.isValid(subMonitor.newChild(1));
146+
if (!restoreStatus.hasFatalError()) {
147+
fRestoreSourceChange.perform(subMonitor.newChild(1));
163148
}
164149
}
165150

0 commit comments

Comments
 (0)