Search in sources :

Example 6 with TextChangeManager

use of org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager in project che by eclipse.

the class CreateCopyOfCompilationUnitChange method getCopiedFileSource.

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();
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) TextChangeManager(org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager)

Example 7 with TextChangeManager

use of org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager in project che by eclipse.

the class CreateCopyOfCompilationUnitChange method createChangeManager.

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;
    SearchMatch[] results = refs.getSearchResults();
    for (int j = 0; j < results.length; j++) {
        SearchMatch searchResult = results[j];
        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;
}
Also used : SearchMatch(org.eclipse.jdt.core.search.SearchMatch) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) SearchResultGroup(org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup) TextChangeManager(org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager)

Example 8 with TextChangeManager

use of org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager in project che by eclipse.

the class ChangeSignatureProcessor method createChangeManager.

private TextChangeManager createChangeManager(IProgressMonitor pm, RefactoringStatus result) throws CoreException {
    pm.beginTask(RefactoringCoreMessages.ChangeSignatureRefactoring_preview, 2);
    fChangeManager = new TextChangeManager();
    boolean isNoArgConstructor = isNoArgConstructor();
    Map<ICompilationUnit, Set<IType>> namedSubclassMapping = null;
    if (isNoArgConstructor) {
        //create only when needed;
        namedSubclassMapping = createNamedSubclassMapping(new SubProgressMonitor(pm, 1));
    } else {
        pm.worked(1);
    }
    for (int i = 0; i < fOccurrences.length; i++) {
        if (pm.isCanceled())
            throw new OperationCanceledException();
        SearchResultGroup group = fOccurrences[i];
        ICompilationUnit cu = group.getCompilationUnit();
        if (cu == null)
            continue;
        CompilationUnitRewrite cuRewrite;
        if (cu.equals(getCu())) {
            cuRewrite = fBaseCuRewrite;
        } else {
            cuRewrite = new CompilationUnitRewrite(cu);
            cuRewrite.getASTRewrite().setTargetSourceRangeComputer(new TightSourceRangeComputer());
        }
        ASTNode[] nodes = ASTNodeSearchUtil.findNodes(group.getSearchResults(), cuRewrite.getRoot());
        //IntroduceParameterObjectRefactoring needs to update declarations first:
        List<OccurrenceUpdate<? extends ASTNode>> deferredUpdates = new ArrayList<OccurrenceUpdate<? extends ASTNode>>();
        for (int j = 0; j < nodes.length; j++) {
            OccurrenceUpdate<? extends ASTNode> update = createOccurrenceUpdate(nodes[j], cuRewrite, result);
            if (update instanceof DeclarationUpdate) {
                update.updateNode();
            } else {
                deferredUpdates.add(update);
            }
        }
        for (Iterator<OccurrenceUpdate<? extends ASTNode>> iter = deferredUpdates.iterator(); iter.hasNext(); ) {
            iter.next().updateNode();
        }
        if (isNoArgConstructor && namedSubclassMapping.containsKey(cu)) {
            //only non-anonymous subclasses may have noArgConstructors to modify - see bug 43444
            Set<IType> subtypes = namedSubclassMapping.get(cu);
            for (Iterator<IType> iter = subtypes.iterator(); iter.hasNext(); ) {
                IType subtype = iter.next();
                AbstractTypeDeclaration subtypeNode = ASTNodeSearchUtil.getAbstractTypeDeclarationNode(subtype, cuRewrite.getRoot());
                if (subtypeNode != null)
                    modifyImplicitCallsToNoArgConstructor(subtypeNode, cuRewrite);
            }
        }
        TextChange change = cuRewrite.createChange(true);
        if (change != null)
            fChangeManager.manage(cu, change);
    }
    pm.done();
    return fChangeManager;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) Set(java.util.Set) HashSet(java.util.HashSet) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ArrayList(java.util.ArrayList) TextChange(org.eclipse.ltk.core.refactoring.TextChange) SearchResultGroup(org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) IType(org.eclipse.jdt.core.IType) TightSourceRangeComputer(org.eclipse.jdt.internal.corext.refactoring.util.TightSourceRangeComputer) ASTNode(org.eclipse.jdt.core.dom.ASTNode) TextChangeManager(org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 9 with TextChangeManager

use of org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager in project che by eclipse.

the class IntroduceIndirectionRefactoring method checkFinalConditions.

@Override
public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException, OperationCanceledException {
    RefactoringStatus result = new RefactoringStatus();
    fTextChangeManager = new TextChangeManager();
    fIntermediaryFirstParameterType = null;
    fIntermediaryTypeBinding = null;
    for (Iterator<CompilationUnitRewrite> iter = fRewrites.values().iterator(); iter.hasNext(); ) iter.next().clearASTAndImportRewrites();
    int startupTicks = 5;
    int hierarchyTicks = 5;
    int visibilityTicks = 5;
    int referenceTicks = fUpdateReferences ? 30 : 5;
    int creationTicks = 5;
    //$NON-NLS-1$
    pm.beginTask("", startupTicks + hierarchyTicks + visibilityTicks + referenceTicks + creationTicks);
    pm.setTaskName(RefactoringCoreMessages.IntroduceIndirectionRefactoring_checking_conditions);
    result.merge(Checks.checkMethodName(fIntermediaryMethodName, fIntermediaryType));
    if (result.hasFatalError())
        return result;
    if (fIntermediaryType == null)
        return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_cannot_run_without_intermediary_type);
    // intermediary type is already non binary/non-enum
    CompilationUnitRewrite imRewrite = getCachedCURewrite(fIntermediaryType.getCompilationUnit());
    fIntermediaryTypeBinding = typeToBinding(fIntermediaryType, imRewrite.getRoot());
    fAdjustor = new MemberVisibilityAdjustor(fIntermediaryType, fIntermediaryType);
    fIntermediaryAdjustments = new HashMap<IMember, IncomingMemberVisibilityAdjustment>();
    // check static method in non-static nested type
    if (fIntermediaryTypeBinding.isNested() && !Modifier.isStatic(fIntermediaryTypeBinding.getModifiers()))
        return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_cannot_create_in_nested_nonstatic, JavaStatusContext.create(fIntermediaryType));
    pm.worked(startupTicks);
    if (pm.isCanceled())
        throw new OperationCanceledException();
    if (fUpdateReferences) {
        //$NON-NLS-1$
        pm.setTaskName(RefactoringCoreMessages.IntroduceIndirectionRefactoring_checking_conditions + " " + RefactoringCoreMessages.IntroduceIndirectionRefactoring_looking_for_references);
        result.merge(updateReferences(new NoOverrideProgressMonitor(pm, referenceTicks)));
        pm.setTaskName(RefactoringCoreMessages.IntroduceIndirectionRefactoring_checking_conditions);
    } else {
        // only update the declaration and/or a selected method invocation
        if (fSelectionMethodInvocation != null) {
            fIntermediaryFirstParameterType = getExpressionType(fSelectionMethodInvocation);
            final IMember enclosing = getEnclosingInitialSelectionMember();
            // create an edit for this particular call
            result.merge(updateMethodInvocation(fSelectionMethodInvocation, enclosing, getCachedCURewrite(fSelectionCompilationUnit)));
            if (!isRewriteKept(fSelectionCompilationUnit))
                createChangeAndDiscardRewrite(fSelectionCompilationUnit);
            // does call see the intermediary method?
            // => increase visibility of the type of the intermediary method.
            result.merge(adjustVisibility(fIntermediaryType, enclosing.getDeclaringType(), new NoOverrideProgressMonitor(pm, 0)));
        }
        pm.worked(referenceTicks);
    }
    if (pm.isCanceled())
        throw new OperationCanceledException();
    if (fIntermediaryFirstParameterType == null)
        fIntermediaryFirstParameterType = fTargetMethodBinding.getDeclaringClass();
    // The target type and method may have changed - update them
    IType actualTargetType = (IType) fIntermediaryFirstParameterType.getJavaElement();
    if (!fTargetMethod.getDeclaringType().equals(actualTargetType)) {
        IMethod actualTargetMethod = new MethodOverrideTester(actualTargetType, actualTargetType.newSupertypeHierarchy(null)).findOverriddenMethodInHierarchy(actualTargetType, fTargetMethod);
        fTargetMethod = actualTargetMethod;
        fTargetMethodBinding = findMethodBindingInHierarchy(fIntermediaryFirstParameterType, actualTargetMethod);
        Assert.isNotNull(fTargetMethodBinding);
    }
    result.merge(checkCanCreateIntermediaryMethod());
    createIntermediaryMethod();
    pm.worked(creationTicks);
    //$NON-NLS-1$
    pm.setTaskName(RefactoringCoreMessages.IntroduceIndirectionRefactoring_checking_conditions + " " + RefactoringCoreMessages.IntroduceIndirectionRefactoring_adjusting_visibility);
    result.merge(updateTargetVisibility(new NoOverrideProgressMonitor(pm, 0)));
    result.merge(updateIntermediaryVisibility(new NoOverrideProgressMonitor(pm, 0)));
    pm.worked(visibilityTicks);
    pm.setTaskName(RefactoringCoreMessages.IntroduceIndirectionRefactoring_checking_conditions);
    createChangeAndDiscardRewrite(fIntermediaryType.getCompilationUnit());
    result.merge(Checks.validateModifiesFiles(getAllFilesToModify(), getValidationContext()));
    pm.done();
    return result;
}
Also used : CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite) IncomingMemberVisibilityAdjustment(org.eclipse.jdt.internal.corext.refactoring.structure.MemberVisibilityAdjustor.IncomingMemberVisibilityAdjustment) MethodOverrideTester(org.eclipse.jdt.internal.corext.util.MethodOverrideTester) MemberVisibilityAdjustor(org.eclipse.jdt.internal.corext.refactoring.structure.MemberVisibilityAdjustor) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IMember(org.eclipse.jdt.core.IMember) IType(org.eclipse.jdt.core.IType) IMethod(org.eclipse.jdt.core.IMethod) TextChangeManager(org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager)

Example 10 with TextChangeManager

use of org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager in project che by eclipse.

the class RenamePackageProcessor method doCheckFinalConditions.

@Override
protected RefactoringStatus doCheckFinalConditions(IProgressMonitor pm, CheckConditionsContext context) throws CoreException {
    try {
        //$NON-NLS-1$
        pm.beginTask("", 23 + (fUpdateQualifiedNames ? 10 : 0) + (fUpdateTextualMatches ? 10 : 0));
        pm.setTaskName(RefactoringCoreMessages.RenamePackageRefactoring_checking);
        RefactoringStatus result = new RefactoringStatus();
        result.merge(checkNewElementName(getNewElementName()));
        pm.worked(1);
        result.merge(checkForMainAndNativeMethods());
        pm.worked(2);
        if (fPackage.isReadOnly()) {
            String message = Messages.format(RefactoringCoreMessages.RenamePackageRefactoring_Packagered_only, getElementLabel(fPackage));
            result.addFatalError(message);
        } else if (Resources.isReadOnly(fPackage.getResource())) {
            String message = Messages.format(RefactoringCoreMessages.RenamePackageRefactoring_resource_read_only, getElementLabel(fPackage));
            result.addError(message);
        }
        result.merge(checkPackageName(getNewElementName()));
        if (result.hasFatalError())
            return result;
        fChangeManager = new TextChangeManager();
        fImportsManager = new ImportsManager();
        SubProgressMonitor subPm = new SubProgressMonitor(pm, 16);
        if (fRenameSubpackages) {
            IPackageFragment[] allSubpackages = JavaElementUtil.getPackageAndSubpackages(fPackage);
            //$NON-NLS-1$
            subPm.beginTask("", allSubpackages.length);
            for (int i = 0; i < allSubpackages.length; i++) {
                new PackageRenamer(allSubpackages[i], this, fChangeManager, fImportsManager).doRename(new SubProgressMonitor(subPm, 1), result);
            }
            subPm.done();
        } else {
            new PackageRenamer(fPackage, this, fChangeManager, fImportsManager).doRename(subPm, result);
        }
        fImportsManager.rewriteImports(fChangeManager, new SubProgressMonitor(pm, 3));
        if (fUpdateTextualMatches) {
            pm.subTask(RefactoringCoreMessages.RenamePackageRefactoring_searching_text);
            TextMatchUpdater.perform(new SubProgressMonitor(pm, 10), RefactoringScopeFactory.create(fPackage), this, fChangeManager, new SearchResultGroup[0]);
        }
        if (fUpdateQualifiedNames)
            computeQualifiedNameMatches(new SubProgressMonitor(pm, 10));
        return result;
    } finally {
        pm.done();
    }
}
Also used : IPackageFragment(org.eclipse.jdt.core.IPackageFragment) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) TextChangeManager(org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager)

Aggregations

TextChangeManager (org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager)11 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)7 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)6 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)6 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)5 IFile (org.eclipse.core.resources.IFile)3 ASTNode (org.eclipse.jdt.core.dom.ASTNode)3 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)3 ArrayList (java.util.ArrayList)2 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)2 IType (org.eclipse.jdt.core.IType)2 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)2 ImportRewrite (org.eclipse.jdt.core.dom.rewrite.ImportRewrite)2 CompilationUnitChange (org.eclipse.jdt.core.refactoring.CompilationUnitChange)2 SearchResultGroup (org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup)2 ReferencesInBinaryContext (org.eclipse.jdt.internal.corext.refactoring.base.ReferencesInBinaryContext)2 TextChange (org.eclipse.ltk.core.refactoring.TextChange)2 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)2 TextEdit (org.eclipse.text.edits.TextEdit)2 TextEditGroup (org.eclipse.text.edits.TextEditGroup)2