-
Notifications
You must be signed in to change notification settings - Fork 82
/
ChangeCorrectionProposalCore.java
238 lines (217 loc) · 6.37 KB
/
ChangeCorrectionProposalCore.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
227
228
229
230
231
232
233
234
235
236
237
238
/*******************************************************************************
* Copyright (c) 2018, 2024 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
*
* Copied from /org.eclipse.jdt.ui/src/org/eclipse/jdt/ui/text/java/correction/ChangeCorrectionProposal.java
*
* Contributors:
* IBM Corporation - initial API and implementation
* Red Hat Inc. - copy and modify to be used as ChangeCorrectionProposalCore
*******************************************************************************/
package org.eclipse.jdt.core.manipulation;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.IUndoManager;
import org.eclipse.ltk.core.refactoring.NullChange;
import org.eclipse.ltk.core.refactoring.RefactoringCore;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
/**
* @since 1.11
*/
public class ChangeCorrectionProposalCore implements IAdaptable {
protected Change fChange;
protected String fName;
protected int fRelevance;
private String fCommandId;
/**
* Constructs a change correction proposal.
*
* @param name the name that is displayed in the proposal selection dialog
* @param change the change that is executed when the proposal is applied or <code>null</code>
* if the change will be created by implementors of {@link #createChange()}
* @param relevance the relevance of this proposal
*/
public ChangeCorrectionProposalCore(String name, Change change, int relevance) {
if (name == null) {
throw new IllegalArgumentException("Name must not be null"); //$NON-NLS-1$
}
fName= name;
fChange= change;
fRelevance= relevance;
}
public void apply() throws CoreException {
performChange();
}
/**
* Performs the change associated with this proposal.
* <p>
* Subclasses may extend, but must call the super implementation.
*
* @throws CoreException
* when the invocation of the change failed
*/
protected void performChange() throws CoreException {
Change change= null;
try {
change= getChange();
if (change != null) {
change.initializeValidationData(new NullProgressMonitor());
RefactoringStatus valid= change.isValid(new NullProgressMonitor());
if (valid.hasFatalError()) {
IStatus status = new Status(IStatus.ERROR, JavaManipulation.getPreferenceNodeId(), IStatus.ERROR,
valid.getMessageMatchingSeverity(RefactoringStatus.FATAL), null);
throw new CoreException(status);
} else {
IUndoManager manager= RefactoringCore.getUndoManager();
Change undoChange;
boolean successful= false;
try {
manager.aboutToPerformChange(change);
undoChange= change.perform(new NullProgressMonitor());
successful= true;
} finally {
manager.changePerformed(change, successful);
}
if (undoChange != null) {
undoChange.initializeValidationData(new NullProgressMonitor());
manager.addUndo(getName(), undoChange);
}
}
}
} finally {
if (change != null) {
change.dispose();
}
change= null;
}
}
@SuppressWarnings("unused")
public Object getAdditionalProposalInfo(IProgressMonitor monitor) {
StringBuilder buf= new StringBuilder();
buf.append("<p>"); //$NON-NLS-1$
try {
Change change= getChange();
if (change != null) {
String name= change.getName();
if (name.length() == 0) {
return null;
}
buf.append(name);
} else {
return null;
}
} catch (CoreException e) {
buf.append("Unexpected error when accessing this proposal:<p><pre>"); //$NON-NLS-1$
buf.append(e.getLocalizedMessage());
buf.append("</pre>"); //$NON-NLS-1$
}
buf.append("</p>"); //$NON-NLS-1$
return buf.toString();
}
/**
* Returns the name of the proposal.
*
* @return the name of the proposal
*/
public String getName() {
return fName;
}
/**
* Returns the change that will be executed when the proposal is applied.
* This method calls {@link #createChange()} to compute the change.
*
* @return the change for this proposal, can be <code>null</code> in rare cases if creation of
* the change failed
* @throws CoreException when the change could not be created
*/
public Change getChange() throws CoreException {
synchronized (this) {
if (fChange == null) {
fChange = createChange();
}
}
return fChange;
}
/**
* @since 1.20
* @return The current change, or null if not initialized yet
*/
public Change getCurrentChange() {
synchronized (this) {
return fChange;
}
}
/**
* Creates the change for this proposal.
* This method is only called once and only when no change has been passed in
* {#ChangeCorrectionProposal(String, Change, int, Image)}.
*
* Subclasses may override.
*
* @return the created change
* @throws CoreException if the creation of the change failed
*/
protected Change createChange() throws CoreException {
return new NullChange();
}
/**
* Sets the display name.
*
* @param name the name to set
*/
public void setDisplayName(String name) {
if (name == null) {
throw new IllegalArgumentException("Name must not be null"); //$NON-NLS-1$
}
fName= name;
}
public int getRelevance() {
return fRelevance;
}
/**
* Sets the relevance.
*
* @param relevance the relevance to set
*
* @see #getRelevance()
*/
public void setRelevance(int relevance) {
fRelevance= relevance;
}
/**
* @since 1.20
* @return the command id
*/
public String getCommandId() {
return fCommandId;
}
/**
* Set the proposal id to allow assigning a shortcut to the correction proposal.
*
* @since 1.20
* @param commandId The proposal id for this proposal or <code>null</code> if no command
* should be assigned to this proposal.
*/
public void setCommandId(String commandId) {
fCommandId= commandId;
}
@SuppressWarnings("unchecked")
@Override
public <T> T getAdapter(Class<T> adapter) {
if (adapter.isInstance(this)) {
return (T) this;
}
return null;
}
}