Search in sources :

Example 31 with TextEditGroup

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;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IFile(org.eclipse.core.resources.IFile) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ArrayList(java.util.ArrayList) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) RefactoringASTParser(org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 32 with TextEditGroup

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());
    }
}
Also used : TextChange(org.eclipse.ltk.core.refactoring.TextChange) TextEditGroup(org.eclipse.text.edits.TextEditGroup) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Example 33 with TextEditGroup

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();
    }
}
Also used : MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) CategorizedTextEditGroup(org.eclipse.ltk.core.refactoring.CategorizedTextEditGroup) TextEditGroup(org.eclipse.text.edits.TextEditGroup) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Example 34 with TextEditGroup

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;
}
Also used : CategorizedTextEditGroup(org.eclipse.ltk.core.refactoring.CategorizedTextEditGroup) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Aggregations

TextEditGroup (org.eclipse.text.edits.TextEditGroup)34 ASTNode (org.eclipse.jdt.core.dom.ASTNode)12 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)12 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)11 TextEdit (org.eclipse.text.edits.TextEdit)10 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)6 AST (org.eclipse.jdt.core.dom.AST)6 CategorizedTextEditGroup (org.eclipse.ltk.core.refactoring.CategorizedTextEditGroup)6 SimpleName (org.eclipse.jdt.core.dom.SimpleName)5 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)4 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)4 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)4 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)4 CompilationUnitChange (org.eclipse.jdt.core.refactoring.CompilationUnitChange)4 ArrayList (java.util.ArrayList)3 IFile (org.eclipse.core.resources.IFile)3 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)3 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)3 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)3 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)3