-
Notifications
You must be signed in to change notification settings - Fork 86
/
RefactoringExecutionHelper.java
226 lines (200 loc) · 8.53 KB
/
RefactoringExecutionHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*******************************************************************************
* Copyright (c) 2000, 2009 IBM Corporation and others.
*
* 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:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.internal.ui.refactoring;
import java.lang.reflect.InvocationTargetException;
import org.eclipse.swt.custom.BusyIndicator;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.jobs.IJobManager;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableContext;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.PerformChangeOperation;
import org.eclipse.ltk.core.refactoring.Refactoring;
import org.eclipse.ltk.core.refactoring.RefactoringCore;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.ui.refactoring.RefactoringUI;
import org.eclipse.jdt.internal.corext.util.Messages;
import org.eclipse.jdt.ui.refactoring.RefactoringSaveHelper;
import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter;
import org.eclipse.jdt.internal.ui.util.Progress;
/**
* A helper class to execute a refactoring. The class takes care of pushing the
* undo change onto the undo stack and folding editor edits into one editor
* undo object.
*/
public class RefactoringExecutionHelper {
private final Refactoring fRefactoring;
private final Shell fParent;
private final IRunnableContext fExecContext;
private final int fStopSeverity;
private final int fSaveMode;
private class Operation implements IWorkspaceRunnable {
public Change fChange;
public PerformChangeOperation fPerformChangeOperation;
private final boolean fForked;
private final boolean fForkChangeExecution;
public Operation(boolean forked, boolean forkChangeExecution) {
fForked= forked;
fForkChangeExecution= forkChangeExecution;
}
@Override
public void run(IProgressMonitor pm) throws CoreException {
try {
pm.beginTask("", fForked && !fForkChangeExecution ? 7 : 11); //$NON-NLS-1$
pm.subTask(""); //$NON-NLS-1$
final RefactoringStatus status= fRefactoring.checkAllConditions(Progress.subMonitorPrepend(pm, 4));
if (status.getSeverity() >= fStopSeverity) {
final boolean[] canceled= { false };
if (fForked) {
fParent.getDisplay().syncExec(() -> canceled[0]= showStatusDialog(status));
} else {
canceled[0]= showStatusDialog(status);
}
if (canceled[0]) {
throw new OperationCanceledException();
}
}
fChange= fRefactoring.createChange(Progress.subMonitorPrepend(pm, 2));
fChange.initializeValidationData(Progress.subMonitorPrepend(pm, 1));
fPerformChangeOperation= new PerformChangeOperation(fChange);//RefactoringUI.createUIAwareChangeOperation(fChange);
fPerformChangeOperation.setUndoManager(RefactoringCore.getUndoManager(), fRefactoring.getName());
if (fRefactoring instanceof IScheduledRefactoring)
fPerformChangeOperation.setSchedulingRule(((IScheduledRefactoring)fRefactoring).getSchedulingRule());
if (!fForked || fForkChangeExecution)
fPerformChangeOperation.run(Progress.subMonitorPrepend(pm, 4));
} finally {
pm.done();
}
}
/**
* @param status the status to show
* @return <code>true</code> iff the operation should be cancelled
*/
private boolean showStatusDialog(RefactoringStatus status) {
Dialog dialog= RefactoringUI.createRefactoringStatusDialog(status, fParent, fRefactoring.getName(), false);
return dialog.open() == IDialogConstants.CANCEL_ID;
}
}
/**
* Creates a new refactoring execution helper.
*
* @param refactoring the refactoring
* @param stopSeverity a refactoring status constant from {@link RefactoringStatus}
* @param saveMode a save mode from {@link RefactoringSaveHelper}
* @param parent the parent shell
* @param context the runnable context
*/
public RefactoringExecutionHelper(Refactoring refactoring, int stopSeverity, int saveMode, Shell parent, IRunnableContext context) {
super();
Assert.isNotNull(refactoring);
Assert.isNotNull(parent);
Assert.isNotNull(context);
fRefactoring= refactoring;
fStopSeverity= stopSeverity;
fParent= parent;
fExecContext= context;
fSaveMode= saveMode;
}
/**
* Must be called in the UI thread.
* @param fork if set, the operation will be forked
* @param cancelable if set, the operation will be cancelable
* @throws InterruptedException thrown when the operation is cancelled
* @throws InvocationTargetException thrown when the operation failed to execute
*/
public void perform(boolean fork, boolean cancelable) throws InterruptedException, InvocationTargetException {
perform(fork, false, cancelable);
}
/**
* Must be called in the UI thread.<br>
* <strong>Use {@link #perform(boolean, boolean)} unless you know exactly what you are doing!</strong>
*
* @param fork if set, the operation will be forked
* @param forkChangeExecution if the change should not be executed in the UI thread: This may not work in any case
* @param cancelable if set, the operation will be cancelable
* @throws InterruptedException thrown when the operation is cancelled
* @throws InvocationTargetException thrown when the operation failed to execute
*/
public void perform(boolean fork, boolean forkChangeExecution, boolean cancelable) throws InterruptedException, InvocationTargetException {
Assert.isTrue(Display.getCurrent() != null);
final IJobManager manager= Job.getJobManager();
final ISchedulingRule rule;
if (fRefactoring instanceof IScheduledRefactoring) {
rule= ((IScheduledRefactoring)fRefactoring).getSchedulingRule();
} else {
rule= ResourcesPlugin.getWorkspace().getRoot();
}
try {
try {
Runnable r= () -> manager.beginRule(rule, null);
BusyIndicator.showWhile(fParent.getDisplay(), r);
} catch (OperationCanceledException e) {
throw new InterruptedException(e.getMessage());
}
RefactoringSaveHelper saveHelper= new RefactoringSaveHelper(fSaveMode);
if (!saveHelper.saveEditors(fParent))
throw new InterruptedException();
final Operation op= new Operation(fork, forkChangeExecution);
fRefactoring.setValidationContext(fParent);
try{
fExecContext.run(fork, cancelable, new WorkbenchRunnableAdapter(op, rule, true));
if (fork && !forkChangeExecution && op.fPerformChangeOperation != null)
fExecContext.run(false, false, new WorkbenchRunnableAdapter(op.fPerformChangeOperation, rule, true));
if (op.fPerformChangeOperation != null) {
RefactoringStatus validationStatus= op.fPerformChangeOperation.getValidationStatus();
if (validationStatus != null && validationStatus.hasFatalError()) {
MessageDialog.openError(fParent, fRefactoring.getName(),
Messages.format(
RefactoringMessages.RefactoringExecutionHelper_cannot_execute,
validationStatus.getMessageMatchingSeverity(RefactoringStatus.FATAL)));
throw new InterruptedException();
}
}
} catch (InvocationTargetException e) {
PerformChangeOperation pco= op.fPerformChangeOperation;
if (pco != null && pco.changeExecutionFailed()) {
ChangeExceptionHandler handler= new ChangeExceptionHandler(fParent, fRefactoring);
Throwable inner= e.getTargetException();
if (inner instanceof RuntimeException) {
handler.handle(pco.getChange(), (RuntimeException)inner);
} else if (inner instanceof CoreException) {
handler.handle(pco.getChange(), (CoreException)inner);
} else {
throw e;
}
} else {
throw e;
}
} catch (OperationCanceledException e) {
throw new InterruptedException(e.getMessage());
} finally {
saveHelper.triggerIncrementalBuild();
}
} finally {
manager.endRule(rule);
fRefactoring.setValidationContext(null);
}
}
}