-
Notifications
You must be signed in to change notification settings - Fork 82
/
CreateCopyOfCompilationUnitChange.java
218 lines (196 loc) · 8.4 KB
/
CreateCopyOfCompilationUnitChange.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
/*******************************************************************************
* Copyright (c) 2000, 2016 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.corext.refactoring.reorg;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.mapping.ResourceMapping;
import org.eclipse.text.edits.ReplaceEdit;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.participants.ReorgExecutionLog;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.search.IJavaSearchConstants;
import org.eclipse.jdt.core.search.IJavaSearchScope;
import org.eclipse.jdt.core.search.SearchEngine;
import org.eclipse.jdt.core.search.SearchMatch;
import org.eclipse.jdt.core.search.SearchPattern;
import org.eclipse.jdt.internal.core.manipulation.JavaManipulationPlugin;
import org.eclipse.jdt.internal.core.manipulation.util.BasicElementLabels;
import org.eclipse.jdt.internal.corext.refactoring.IRefactoringSearchRequestor;
import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
import org.eclipse.jdt.internal.corext.refactoring.RefactoringSearchEngine;
import org.eclipse.jdt.internal.corext.refactoring.RefactoringSearchEngine2;
import org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup;
import org.eclipse.jdt.internal.corext.refactoring.changes.TextChangeCompatibility;
import org.eclipse.jdt.internal.corext.refactoring.nls.changes.CreateTextFileChange;
import org.eclipse.jdt.internal.corext.refactoring.rename.TypeOccurrenceCollector;
import org.eclipse.jdt.internal.corext.refactoring.util.JavaElementUtil;
import org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager;
import org.eclipse.jdt.internal.corext.util.JavaElementResourceMapping;
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
import org.eclipse.jdt.internal.corext.util.Messages;
import org.eclipse.jdt.internal.corext.util.SearchUtils;
import org.eclipse.jdt.internal.ui.util.Progress;
public final class CreateCopyOfCompilationUnitChange extends CreateTextFileChange {
private static TextChangeManager createChangeManager(IProgressMonitor monitor, ICompilationUnit copy, String newName) throws CoreException {
TextChangeManager manager= new TextChangeManager();
SearchResultGroup refs= getReferences(copy, monitor);
if (refs == null)
return manager;
if (refs.getCompilationUnit() == null)
return manager;
String name= RefactoringCoreMessages.CopyRefactoring_update_ref;
for (SearchMatch searchResult : refs.getSearchResults()) {
if (searchResult.getAccuracy() == SearchMatch.A_INACCURATE)
continue;
int offset= searchResult.getOffset();
int length= searchResult.getLength();
TextChangeCompatibility.addTextEdit(manager.get(copy), name, new ReplaceEdit(offset, length, newName));
}
return manager;
}
private static SearchPattern createSearchPattern(IType type) throws JavaModelException {
SearchPattern pattern= SearchPattern.createPattern(type, IJavaSearchConstants.ALL_OCCURRENCES, SearchUtils.GENERICS_AGNOSTIC_MATCH_RULE);
if (pattern == null) {
return null;
}
IMethod[] constructors= JavaElementUtil.getAllConstructors(type);
if (constructors.length == 0)
return pattern;
SearchPattern constructorDeclarationPattern= RefactoringSearchEngine.createOrPattern(constructors, IJavaSearchConstants.DECLARATIONS);
return SearchPattern.createOrPattern(pattern, constructorDeclarationPattern);
}
private static String getCopiedFileSource(IProgressMonitor monitor, ICompilationUnit unit, String newTypeName) throws CoreException {
ICompilationUnit copy= unit.getPrimary().getWorkingCopy(null);
try {
TextChangeManager manager= createChangeManager(monitor, copy, newTypeName);
String result= manager.get(copy).getPreviewContent(new NullProgressMonitor());
return result;
} finally {
copy.discardWorkingCopy();
}
}
private static SearchResultGroup getReferences(final ICompilationUnit copy, IProgressMonitor monitor) throws JavaModelException {
final ICompilationUnit[] copies= new ICompilationUnit[] { copy};
IJavaSearchScope scope= SearchEngine.createJavaSearchScope(copies);
final IType type= copy.findPrimaryType();
if (type == null)
return null;
SearchPattern pattern= createSearchPattern(type);
if (pattern == null) {
return null;
}
final RefactoringSearchEngine2 engine= new RefactoringSearchEngine2(pattern);
engine.setScope(scope);
engine.setWorkingCopies(copies);
engine.setRequestor(new IRefactoringSearchRequestor() {
TypeOccurrenceCollector fTypeOccurrenceCollector= new TypeOccurrenceCollector(type);
@Override
public SearchMatch acceptSearchMatch(SearchMatch match) {
try {
return fTypeOccurrenceCollector.acceptSearchMatch2(copy, match);
} catch (CoreException e) {
JavaManipulationPlugin.log(e);
return null;
}
}
});
engine.searchPattern(monitor);
// Assert.isTrue(results.length <= 1);
// just 1 file or none, but inaccurate matches can play bad here (see
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=106127)
for (Object result : engine.getResults()) {
SearchResultGroup group= (SearchResultGroup) result;
if (group.getCompilationUnit().equals(copy))
return group;
}
return null;
}
private final INewNameQuery fNameQuery;
private final ICompilationUnit fOldCu;
public CreateCopyOfCompilationUnitChange(IPath path, String source, ICompilationUnit oldCu, INewNameQuery nameQuery) {
super(path, source, null, "java"); //$NON-NLS-1$
fOldCu= oldCu;
fNameQuery= nameQuery;
setEncoding(oldCu);
}
@Override
public String getName() {
String cuName= BasicElementLabels.getResourceName(fOldCu.getElementName());
String cuContainerName= BasicElementLabels.getPathLabel(fOldCu.getParent().getPath(), false);
return Messages.format(RefactoringCoreMessages.CreateCopyOfCompilationUnitChange_create_copy, new String[] { cuName, cuContainerName});
}
@Override
protected IFile getOldFile(IProgressMonitor monitor) throws OperationCanceledException {
try {
monitor.beginTask("", 12); //$NON-NLS-1$
String oldSource= super.getSource();
IPath oldPath= super.getPath();
String newTypeName= fNameQuery.getNewName();
try {
String newSource= getCopiedFileSource(Progress.subMonitor(monitor, 9), fOldCu, newTypeName);
setSource(newSource);
setPath(fOldCu.getResource().getParent().getFullPath().append(JavaModelUtil.getRenamedCUName(fOldCu, newTypeName)));
return super.getOldFile(Progress.subMonitor(monitor, 1));
} catch (CoreException e) {
setSource(oldSource);
setPath(oldPath);
return super.getOldFile(Progress.subMonitor(monitor, 2));
}
} finally {
monitor.done();
}
}
private void markAsExecuted(ICompilationUnit unit, ResourceMapping mapping) {
ReorgExecutionLog log= getAdapter(ReorgExecutionLog.class);
if (log != null) {
log.markAsProcessed(unit);
log.markAsProcessed(mapping);
}
}
@Override
public Change perform(IProgressMonitor monitor) throws CoreException {
ResourceMapping mapping= JavaElementResourceMapping.create(fOldCu);
final Change result= super.perform(monitor);
markAsExecuted(fOldCu, mapping);
return result;
}
private void setEncoding(ICompilationUnit unit) {
IResource resource= unit.getResource();
// no file so the encoding is taken from the target
if (!(resource instanceof IFile))
return;
IFile file= (IFile) resource;
try {
String encoding= file.getCharset(false);
if (encoding != null) {
setEncoding(encoding, true);
} else {
encoding= file.getCharset(true);
if (encoding != null) {
setEncoding(encoding, false);
}
}
} catch (CoreException e) {
// Take encoding from target
}
}
}