use of org.eclipse.text.edits.TextEditGroup in project che by eclipse.
the class SelfEncapsulateFieldRefactoring method checkFinalConditions.
@Override
public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException {
pm.beginTask(NO_NAME, 12);
pm.setTaskName(RefactoringCoreMessages.SelfEncapsulateField_checking_preconditions);
RefactoringStatus result = new RefactoringStatus();
fRewriter = ASTRewrite.create(fRoot.getAST());
fChangeManager.clear();
boolean usingLocalGetter = isUsingLocalGetter();
boolean usingLocalSetter = isUsingLocalSetter();
result.merge(checkMethodNames(usingLocalGetter, usingLocalSetter));
pm.worked(1);
if (result.hasFatalError())
return result;
pm.setTaskName(RefactoringCoreMessages.SelfEncapsulateField_searching_for_cunits);
final SubProgressMonitor subPm = new SubProgressMonitor(pm, 5);
ICompilationUnit[] affectedCUs = RefactoringSearchEngine.findAffectedCompilationUnits(SearchPattern.createPattern(fField, IJavaSearchConstants.REFERENCES), RefactoringScopeFactory.create(fField, fConsiderVisibility), subPm, result, true);
checkInHierarchy(result, usingLocalGetter, usingLocalSetter);
if (result.hasFatalError())
return result;
pm.setTaskName(RefactoringCoreMessages.SelfEncapsulateField_analyzing);
IProgressMonitor sub = new SubProgressMonitor(pm, 5);
sub.beginTask(NO_NAME, affectedCUs.length);
IVariableBinding fieldIdentifier = fFieldDeclaration.resolveBinding();
ITypeBinding declaringClass = ((AbstractTypeDeclaration) ASTNodes.getParent(fFieldDeclaration, AbstractTypeDeclaration.class)).resolveBinding();
List<TextEditGroup> ownerDescriptions = new ArrayList<TextEditGroup>();
ICompilationUnit owner = fField.getCompilationUnit();
fImportRewrite = StubUtility.createImportRewrite(fRoot, true);
for (int i = 0; i < affectedCUs.length; i++) {
ICompilationUnit unit = affectedCUs[i];
sub.subTask(BasicElementLabels.getFileName(unit));
CompilationUnit root = null;
ASTRewrite rewriter = null;
ImportRewrite importRewrite;
List<TextEditGroup> descriptions;
if (owner.equals(unit)) {
root = fRoot;
rewriter = fRewriter;
importRewrite = fImportRewrite;
descriptions = ownerDescriptions;
} else {
root = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(unit, true);
rewriter = ASTRewrite.create(root.getAST());
descriptions = new ArrayList<TextEditGroup>();
importRewrite = StubUtility.createImportRewrite(root, true);
}
checkCompileErrors(result, root, unit);
AccessAnalyzer analyzer = new AccessAnalyzer(this, unit, fieldIdentifier, declaringClass, rewriter, importRewrite);
root.accept(analyzer);
result.merge(analyzer.getStatus());
if (!fSetterMustReturnValue)
fSetterMustReturnValue = analyzer.getSetterMustReturnValue();
if (result.hasFatalError()) {
fChangeManager.clear();
return result;
}
descriptions.addAll(analyzer.getGroupDescriptions());
if (!owner.equals(unit))
createEdits(unit, rewriter, descriptions, importRewrite);
sub.worked(1);
if (pm.isCanceled())
throw new OperationCanceledException();
}
ownerDescriptions.addAll(addGetterSetterChanges(fRoot, fRewriter, owner.findRecommendedLineSeparator(), usingLocalSetter, usingLocalGetter));
createEdits(owner, fRewriter, ownerDescriptions, fImportRewrite);
sub.done();
IFile[] filesToBeModified = ResourceUtil.getFiles(fChangeManager.getAllCompilationUnits());
result.merge(Checks.validateModifiesFiles(filesToBeModified, getValidationContext()));
if (result.hasFatalError())
return result;
ResourceChangeChecker.checkFilesToBeChanged(filesToBeModified, new SubProgressMonitor(pm, 1));
return result;
}
use of org.eclipse.text.edits.TextEditGroup in project che by eclipse.
the class SelfEncapsulateFieldRefactoring method createEdits.
private void createEdits(ICompilationUnit unit, ASTRewrite rewriter, List<TextEditGroup> groups, ImportRewrite importRewrite) throws CoreException {
TextChange change = fChangeManager.get(unit);
MultiTextEdit root = new MultiTextEdit();
change.setEdit(root);
root.addChild(importRewrite.rewriteImports(null));
root.addChild(rewriter.rewriteAST());
for (Iterator<TextEditGroup> iter = groups.iterator(); iter.hasNext(); ) {
change.addTextEditGroup(iter.next());
}
}
use of org.eclipse.text.edits.TextEditGroup in project che by eclipse.
the class CompilationUnitRewrite method attachChange.
/**
* Attaches the changes of this compilation unit rewrite to the given CU Change. The given
* change <b>must</b> either have no root edit, or a MultiTextEdit as a root edit.
* The edits in the given change <b>must not</b> overlap with the changes of
* this compilation unit.
*
* @param cuChange existing CompilationUnitChange with a MultiTextEdit root or no root at all.
* @param generateGroups <code>true</code> to generate text edit groups, <code>false</code> otherwise
* @param monitor the progress monitor or <code>null</code>
* @return a change combining the changes of this rewrite and the given rewrite, or <code>null</code> for an empty change
* @throws CoreException when text buffer acquisition or import rewrite text edit creation fails
*/
public CompilationUnitChange attachChange(CompilationUnitChange cuChange, boolean generateGroups, IProgressMonitor monitor) throws CoreException {
try {
// TODO: do we need something like ASTRewrite#hasChanges() here?
boolean needsAstRewrite = fRewrite != null;
boolean needsImportRemoval = fImportRemover != null && fImportRemover.hasRemovedNodes();
boolean needsImportRewrite = fImportRewrite != null && fImportRewrite.hasRecordedChanges() || needsImportRemoval;
if (!needsAstRewrite && !needsImportRemoval && !needsImportRewrite)
return null;
MultiTextEdit multiEdit = (MultiTextEdit) cuChange.getEdit();
if (multiEdit == null) {
multiEdit = new MultiTextEdit();
cuChange.setEdit(multiEdit);
}
if (needsAstRewrite) {
// clean up garbage from earlier calls to ASTRewrite#rewriteAST(..), see https://bugs.eclipse.org/bugs/show_bug.cgi?id=408334#c2
clearGroupDescriptionEdits();
TextEdit rewriteEdit;
if (fRememberContent != null) {
rewriteEdit = fRewrite.rewriteAST(fRememberContent, fCu.getJavaProject().getOptions(true));
} else {
rewriteEdit = fRewrite.rewriteAST();
}
if (!isEmptyEdit(rewriteEdit)) {
multiEdit.addChild(rewriteEdit);
if (generateGroups) {
for (Iterator<TextEditGroup> iter = fTextEditGroups.iterator(); iter.hasNext(); ) {
TextEditGroup group = iter.next();
cuChange.addTextEditGroup(group);
}
}
}
}
if (needsImportRemoval) {
fImportRemover.applyRemoves(getImportRewrite());
}
if (needsImportRewrite) {
TextEdit importsEdit = fImportRewrite.rewriteImports(monitor);
if (!isEmptyEdit(importsEdit)) {
multiEdit.addChild(importsEdit);
String importUpdateName = RefactoringCoreMessages.ASTData_update_imports;
cuChange.addTextEditGroup(new TextEditGroup(importUpdateName, importsEdit));
}
} else {
}
if (isEmptyEdit(multiEdit))
return null;
return cuChange;
} finally {
if (monitor != null)
monitor.done();
}
}
use of org.eclipse.text.edits.TextEditGroup in project che by eclipse.
the class CompilationUnitRewrite method createGroupDescription.
public TextEditGroup createGroupDescription(String name) {
TextEditGroup result = new TextEditGroup(name);
fTextEditGroups.add(result);
return result;
}
Aggregations