Search in sources :

Example 1 with TextChange

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();
    }
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) DynamicValidationRefactoringChange(org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange) ArrayList(java.util.ArrayList) TextChange(org.eclipse.ltk.core.refactoring.TextChange) TextChange(org.eclipse.ltk.core.refactoring.TextChange) Change(org.eclipse.ltk.core.refactoring.Change) DynamicValidationRefactoringChange(org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange) IType(org.eclipse.jdt.core.IType) JDTRefactoringDescriptorComment(org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment) IJavaProject(org.eclipse.jdt.core.IJavaProject) RenameJavaElementDescriptor(org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor)

Example 2 with TextChange

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();
}
Also used : CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ArrayList(java.util.ArrayList) TextChange(org.eclipse.ltk.core.refactoring.TextChange) MethodDeclarationMatch(org.eclipse.jdt.core.search.MethodDeclarationMatch) DelegateMethodCreator(org.eclipse.jdt.internal.corext.refactoring.delegates.DelegateMethodCreator) DelegateCreator(org.eclipse.jdt.internal.corext.refactoring.delegates.DelegateCreator)

Example 3 with TextChange

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;
    }
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IJavaProject(org.eclipse.jdt.core.IJavaProject) DynamicValidationRefactoringChange(org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange) RenameResourceChange(org.eclipse.ltk.core.refactoring.resource.RenameResourceChange) TextChange(org.eclipse.ltk.core.refactoring.TextChange) RenameJavaElementDescriptor(org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) IResource(org.eclipse.core.resources.IResource) JDTRefactoringDescriptorComment(org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment) RenameCompilationUnitChange(org.eclipse.jdt.internal.corext.refactoring.changes.RenameCompilationUnitChange)

Example 4 with TextChange

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.
		 */
}
Also used : RefactoringProcessor(org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor) TextChange(org.eclipse.ltk.core.refactoring.TextChange) TextChangeManager(org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager)

Example 5 with TextChange

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

Aggregations

TextChange (org.eclipse.ltk.core.refactoring.TextChange)44 TextEdit (org.eclipse.text.edits.TextEdit)14 TextFileChange (org.eclipse.ltk.core.refactoring.TextFileChange)13 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)13 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)10 CompositeChange (org.eclipse.ltk.core.refactoring.CompositeChange)10 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)8 IFile (org.eclipse.core.resources.IFile)7 IDocument (org.eclipse.jface.text.IDocument)7 ArrayList (java.util.ArrayList)6 JavaModelException (org.eclipse.jdt.core.JavaModelException)6 CompilationUnitChange (org.eclipse.jdt.core.refactoring.CompilationUnitChange)6 Change (org.eclipse.ltk.core.refactoring.Change)6 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)6 Iterator (java.util.Iterator)5 DynamicValidationRefactoringChange (org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange)5 HashMap (java.util.HashMap)4 IJavaProject (org.eclipse.jdt.core.IJavaProject)4 EditorDocumentChange (org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange)4 IOException (java.io.IOException)3