use of org.eclipse.ltk.core.refactoring.TextChange in project che by eclipse.
the class RenameMethodProcessor method createChange.
@Override
public Change createChange(IProgressMonitor monitor) throws CoreException {
try {
final TextChange[] changes = fChangeManager.getAllChanges();
final List<TextChange> list = new ArrayList<TextChange>(changes.length);
list.addAll(Arrays.asList(changes));
String project = null;
IJavaProject javaProject = fMethod.getJavaProject();
if (javaProject != null)
project = javaProject.getElementName();
int flags = JavaRefactoringDescriptor.JAR_MIGRATION | JavaRefactoringDescriptor.JAR_REFACTORING | RefactoringDescriptor.STRUCTURAL_CHANGE;
try {
if (!Flags.isPrivate(fMethod.getFlags()))
flags |= RefactoringDescriptor.MULTI_CHANGE;
} catch (JavaModelException exception) {
JavaPlugin.log(exception);
}
final IType declaring = fMethod.getDeclaringType();
try {
if (declaring.isAnonymous() || declaring.isLocal())
flags |= JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT;
} catch (JavaModelException exception) {
JavaPlugin.log(exception);
}
final String description = Messages.format(RefactoringCoreMessages.RenameMethodProcessor_descriptor_description_short, BasicElementLabels.getJavaElementName(fMethod.getElementName()));
final String header = Messages.format(RefactoringCoreMessages.RenameMethodProcessor_descriptor_description, new String[] { JavaElementLabels.getTextLabel(fMethod, JavaElementLabels.ALL_FULLY_QUALIFIED), BasicElementLabels.getJavaElementName(getNewElementName()) });
final String comment = new JDTRefactoringDescriptorComment(project, this, header).asString();
final RenameJavaElementDescriptor descriptor = RefactoringSignatureDescriptorFactory.createRenameJavaElementDescriptor(IJavaRefactorings.RENAME_METHOD);
descriptor.setProject(project);
descriptor.setDescription(description);
descriptor.setComment(comment);
descriptor.setFlags(flags);
descriptor.setJavaElement(fMethod);
descriptor.setNewName(getNewElementName());
descriptor.setUpdateReferences(fUpdateReferences);
descriptor.setKeepOriginal(fDelegateUpdating);
descriptor.setDeprecateDelegate(fDelegateDeprecation);
return new DynamicValidationRefactoringChange(descriptor, RefactoringCoreMessages.RenameMethodProcessor_change_name, list.toArray(new Change[list.size()]));
} finally {
monitor.done();
}
}
use of org.eclipse.ltk.core.refactoring.TextChange in project che by eclipse.
the class RenameMethodProcessor method addOccurrences.
/**
* Add occurrences
*
* @param manager the text change manager
* @param pm the progress monitor
* @param status the status
* @throws CoreException if change creation failed
*/
protected void addOccurrences(TextChangeManager manager, IProgressMonitor pm, RefactoringStatus status) throws CoreException /*thrown in subtype*/
{
//$NON-NLS-1$
pm.beginTask("", fOccurrences.length);
for (int i = 0; i < fOccurrences.length; i++) {
ICompilationUnit cu = fOccurrences[i].getCompilationUnit();
if (cu == null)
continue;
SearchMatch[] results = fOccurrences[i].getSearchResults();
// Split matches into declaration and non-declaration matches
List<SearchMatch> declarationsInThisCu = new ArrayList<SearchMatch>();
List<SearchMatch> referencesInThisCu = new ArrayList<SearchMatch>();
for (int j = 0; j < results.length; j++) {
if (results[j] instanceof MethodDeclarationMatch)
declarationsInThisCu.add(results[j]);
else
referencesInThisCu.add(results[j]);
}
// First, handle the declarations
if (declarationsInThisCu.size() > 0) {
if (fDelegateUpdating) {
// Update with delegates
CompilationUnitRewrite rewrite = new CompilationUnitRewrite(cu);
rewrite.setResolveBindings(true);
for (Iterator<SearchMatch> iter = declarationsInThisCu.iterator(); iter.hasNext(); ) {
SearchMatch element = iter.next();
MethodDeclaration method = ASTNodeSearchUtil.getMethodDeclarationNode((IMethod) element.getElement(), rewrite.getRoot());
DelegateCreator creator = new DelegateMethodCreator();
creator.setDeclareDeprecated(fDelegateDeprecation);
creator.setDeclaration(method);
creator.setSourceRewrite(rewrite);
creator.setNewElementName(getNewElementName());
creator.prepareDelegate();
creator.createEdit();
}
// Need to handle all delegates first as this
// creates a completely new change object.
TextChange changeForThisCu = rewrite.createChange(true);
changeForThisCu.setKeepPreviewEdits(true);
manager.manage(cu, changeForThisCu);
}
// Update the normal methods
for (Iterator<SearchMatch> iter = declarationsInThisCu.iterator(); iter.hasNext(); ) {
SearchMatch element = iter.next();
simpleUpdate(element, cu, manager.get(cu));
}
}
// Second, handle references
if (fUpdateReferences) {
for (Iterator<SearchMatch> iter = referencesInThisCu.iterator(); iter.hasNext(); ) {
SearchMatch element = iter.next();
simpleUpdate(element, cu, manager.get(cu));
}
}
pm.worked(1);
if (pm.isCanceled())
throw new OperationCanceledException();
}
pm.done();
}
use of org.eclipse.ltk.core.refactoring.TextChange in project che by eclipse.
the class RenameTypeProcessor method createChange.
@Override
public Change createChange(IProgressMonitor monitor) throws CoreException {
try {
monitor.beginTask(RefactoringCoreMessages.RenameTypeRefactoring_creating_change, 4);
String project = null;
IJavaProject javaProject = fType.getJavaProject();
if (javaProject != null)
project = javaProject.getElementName();
int flags = JavaRefactoringDescriptor.JAR_MIGRATION | JavaRefactoringDescriptor.JAR_REFACTORING | RefactoringDescriptor.STRUCTURAL_CHANGE;
try {
if (!Flags.isPrivate(fType.getFlags()))
flags |= RefactoringDescriptor.MULTI_CHANGE;
if (fType.isAnonymous() || fType.isLocal())
flags |= JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT;
} catch (JavaModelException exception) {
JavaPlugin.log(exception);
}
final String description = Messages.format(RefactoringCoreMessages.RenameTypeProcessor_descriptor_description_short, BasicElementLabels.getJavaElementName(fType.getElementName()));
final String header = Messages.format(RefactoringCoreMessages.RenameTypeProcessor_descriptor_description, new String[] { JavaElementLabels.getElementLabel(fType, JavaElementLabels.ALL_FULLY_QUALIFIED), getNewElementLabel() });
final String comment = new JDTRefactoringDescriptorComment(project, this, header).asString();
final RenameJavaElementDescriptor descriptor = RefactoringSignatureDescriptorFactory.createRenameJavaElementDescriptor(IJavaRefactorings.RENAME_TYPE);
descriptor.setProject(project);
descriptor.setDescription(description);
descriptor.setComment(comment);
descriptor.setFlags(flags);
descriptor.setJavaElement(fType);
descriptor.setNewName(getNewElementName());
descriptor.setUpdateQualifiedNames(fUpdateQualifiedNames);
descriptor.setUpdateTextualOccurrences(fUpdateTextualMatches);
descriptor.setUpdateReferences(fUpdateReferences);
if (//$NON-NLS-1$
fUpdateQualifiedNames && fFilePatterns != null && !"".equals(fFilePatterns))
descriptor.setFileNamePatterns(fFilePatterns);
descriptor.setUpdateSimilarDeclarations(fUpdateSimilarElements);
descriptor.setMatchStrategy(fRenamingStrategy);
final DynamicValidationRefactoringChange result = new DynamicValidationRefactoringChange(descriptor, RefactoringCoreMessages.RenameTypeProcessor_change_name);
if (fChangeManager.containsChangesIn(fType.getCompilationUnit())) {
TextChange textChange = fChangeManager.get(fType.getCompilationUnit());
if (textChange instanceof TextFileChange) {
((TextFileChange) textChange).setSaveMode(TextFileChange.FORCE_SAVE);
}
}
result.addAll(fChangeManager.getAllChanges());
if (willRenameCU()) {
IResource resource = fType.getCompilationUnit().getResource();
if (resource != null && resource.isLinked()) {
String ext = resource.getFileExtension();
String renamedResourceName;
if (ext == null)
renamedResourceName = getNewElementName();
else
renamedResourceName = getNewElementName() + '.' + ext;
result.add(new RenameResourceChange(fType.getCompilationUnit().getPath(), renamedResourceName));
} else {
String renamedCUName = JavaModelUtil.getRenamedCUName(fType.getCompilationUnit(), getNewElementName());
result.add(new RenameCompilationUnitChange(fType.getCompilationUnit(), renamedCUName));
}
}
monitor.worked(1);
return result;
} finally {
fChangeManager = null;
}
}
use of org.eclipse.ltk.core.refactoring.TextChange in project che by eclipse.
the class RenameTypeProcessor method checkCUCompleteConditions.
private void checkCUCompleteConditions(final RefactoringStatus status, CompilationUnit currentResolvedCU, ICompilationUnit currentCU, List<RefactoringProcessor> processors) throws CoreException {
// check local variable conditions
List<RefactoringProcessor> locals = getProcessorsOfType(processors, RenameLocalVariableProcessor.class);
if (!locals.isEmpty()) {
RenameAnalyzeUtil.LocalAnalyzePackage[] analyzePackages = new RenameAnalyzeUtil.LocalAnalyzePackage[locals.size()];
TextChangeManager manager = new TextChangeManager();
int current = 0;
TextChange textChange = manager.get(currentCU);
textChange.setKeepPreviewEdits(true);
for (Iterator<RefactoringProcessor> iterator = locals.iterator(); iterator.hasNext(); ) {
RenameLocalVariableProcessor localProcessor = (RenameLocalVariableProcessor) iterator.next();
RenameAnalyzeUtil.LocalAnalyzePackage analyzePackage = localProcessor.getLocalAnalyzePackage();
analyzePackages[current] = analyzePackage;
for (int i = 0; i < analyzePackage.fOccurenceEdits.length; i++) {
//$NON-NLS-1$
TextChangeCompatibility.addTextEdit(textChange, "", analyzePackage.fOccurenceEdits[i], GroupCategorySet.NONE);
}
current++;
}
status.merge(RenameAnalyzeUtil.analyzeLocalRenames(analyzePackages, textChange, currentResolvedCU, false));
}
/*
* There is room for performance improvement here: One could move
* shadowing analyzes out of the field and method processors and perform
* it here, thus saving on working copy creation. Drawback is increased
* heap consumption.
*/
}
use of org.eclipse.ltk.core.refactoring.TextChange in project che by eclipse.
the class DeleteChangeCreator method addTextEditFromRewrite.
private static TextChange addTextEditFromRewrite(TextChangeManager manager, ICompilationUnit cu, ASTRewrite rewrite) throws CoreException {
try {
ITextFileBuffer buffer = RefactoringFileBuffers.acquire(cu);
TextEdit resultingEdits = rewrite.rewriteAST(buffer.getDocument(), cu.getJavaProject().getOptions(true));
TextChange textChange = manager.get(cu);
if (textChange instanceof TextFileChange) {
TextFileChange tfc = (TextFileChange) textChange;
tfc.setSaveMode(TextFileChange.KEEP_SAVE_STATE);
}
String message = RefactoringCoreMessages.DeleteChangeCreator_1;
TextChangeCompatibility.addTextEdit(textChange, message, resultingEdits);
return textChange;
} finally {
RefactoringFileBuffers.release(cu);
}
}
Aggregations