Search in sources :

Example 6 with TextChange

use of org.eclipse.ltk.core.refactoring.TextChange 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 7 with TextChange

use of org.eclipse.ltk.core.refactoring.TextChange in project che by eclipse.

the class ChangeSignatureProcessor method checkCompilationofDeclaringCu.

private RefactoringStatus checkCompilationofDeclaringCu() throws CoreException {
    ICompilationUnit cu = getCu();
    TextChange change = fChangeManager.get(cu);
    String newCuSource = change.getPreviewContent(new NullProgressMonitor());
    CompilationUnit newCUNode = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(newCuSource, cu, true, false, null);
    IProblem[] problems = RefactoringAnalyzeUtil.getIntroducedCompileProblems(newCUNode, fBaseCuRewrite.getRoot());
    RefactoringStatus result = new RefactoringStatus();
    for (int i = 0; i < problems.length; i++) {
        IProblem problem = problems[i];
        if (shouldReport(problem, newCUNode))
            result.addEntry(new RefactoringStatusEntry((problem.isError() ? RefactoringStatus.ERROR : RefactoringStatus.WARNING), problem.getMessage(), new JavaStringStatusContext(newCuSource, SourceRangeFactory.create(problem))));
    }
    return result;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) RefactoringASTParser(org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser) RefactoringStatusEntry(org.eclipse.ltk.core.refactoring.RefactoringStatusEntry) TextChange(org.eclipse.ltk.core.refactoring.TextChange) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) JavaStringStatusContext(org.eclipse.jdt.internal.corext.refactoring.base.JavaStringStatusContext) IProblem(org.eclipse.jdt.core.compiler.IProblem)

Example 8 with TextChange

use of org.eclipse.ltk.core.refactoring.TextChange in project che by eclipse.

the class MoveCuUpdateCreator method addReferenceUpdates.

private void addReferenceUpdates(TextChangeManager changeManager, ICompilationUnit movedUnit, IProgressMonitor pm, RefactoringStatus status) throws JavaModelException, CoreException {
    List<ICompilationUnit> cuList = Arrays.asList(fCus);
    SearchResultGroup[] references = getReferences(movedUnit, pm, status);
    for (int i = 0; i < references.length; i++) {
        SearchResultGroup searchResultGroup = references[i];
        ICompilationUnit referencingCu = searchResultGroup.getCompilationUnit();
        if (referencingCu == null)
            continue;
        boolean simpleReferencesNeedNewImport = simpleReferencesNeedNewImport(movedUnit, referencingCu, cuList);
        SearchMatch[] results = searchResultGroup.getSearchResults();
        for (int j = 0; j < results.length; j++) {
            // TODO: should update type references with results from addImport
            TypeReference reference = (TypeReference) results[j];
            if (reference.isImportDeclaration()) {
                ImportRewrite rewrite = getImportRewrite(referencingCu);
                IImportDeclaration importDecl = (IImportDeclaration) SearchUtils.getEnclosingJavaElement(results[j]);
                if (Flags.isStatic(importDecl.getFlags())) {
                    rewrite.removeStaticImport(importDecl.getElementName());
                    addStaticImport(movedUnit, importDecl, rewrite);
                } else {
                    rewrite.removeImport(importDecl.getElementName());
                    rewrite.addImport(createStringForNewImport(movedUnit, importDecl));
                }
            } else if (reference.isQualified()) {
                TextChange textChange = changeManager.get(referencingCu);
                String changeName = RefactoringCoreMessages.MoveCuUpdateCreator_update_references;
                TextEdit replaceEdit = new ReplaceEdit(reference.getOffset(), reference.getSimpleNameStart() - reference.getOffset(), fNewPackage);
                TextChangeCompatibility.addTextEdit(textChange, changeName, replaceEdit);
            } else if (simpleReferencesNeedNewImport) {
                ImportRewrite importEdit = getImportRewrite(referencingCu);
                String typeName = reference.getSimpleName();
                importEdit.addImport(getQualifiedType(fDestination.getElementName(), typeName));
            }
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) TextChange(org.eclipse.ltk.core.refactoring.TextChange) SearchResultGroup(org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup) IImportDeclaration(org.eclipse.jdt.core.IImportDeclaration) TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit)

Example 9 with TextChange

use of org.eclipse.ltk.core.refactoring.TextChange in project che by eclipse.

the class SelfEncapsulateFieldRefactoring method createChange.

@Override
public Change createChange(IProgressMonitor pm) throws CoreException {
    final Map<String, String> arguments = new HashMap<String, String>();
    String project = null;
    IJavaProject javaProject = fField.getJavaProject();
    if (javaProject != null)
        project = javaProject.getElementName();
    int flags = JavaRefactoringDescriptor.JAR_MIGRATION | JavaRefactoringDescriptor.JAR_REFACTORING | RefactoringDescriptor.STRUCTURAL_CHANGE | RefactoringDescriptor.MULTI_CHANGE;
    final IType declaring = fField.getDeclaringType();
    try {
        if (declaring.isAnonymous() || declaring.isLocal())
            flags |= JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT;
    } catch (JavaModelException exception) {
        JavaPlugin.log(exception);
    }
    final String description = Messages.format(RefactoringCoreMessages.SelfEncapsulateField_descriptor_description_short, BasicElementLabels.getJavaElementName(fField.getElementName()));
    final String header = Messages.format(RefactoringCoreMessages.SelfEncapsulateFieldRefactoring_descriptor_description, new String[] { JavaElementLabels.getElementLabel(fField, JavaElementLabels.ALL_FULLY_QUALIFIED), JavaElementLabels.getElementLabel(declaring, JavaElementLabels.ALL_FULLY_QUALIFIED) });
    final JDTRefactoringDescriptorComment comment = new JDTRefactoringDescriptorComment(project, this, header);
    comment.addSetting(Messages.format(RefactoringCoreMessages.SelfEncapsulateField_original_pattern, JavaElementLabels.getElementLabel(fField, JavaElementLabels.ALL_FULLY_QUALIFIED)));
    comment.addSetting(Messages.format(RefactoringCoreMessages.SelfEncapsulateField_getter_pattern, BasicElementLabels.getJavaElementName(fGetterName)));
    comment.addSetting(Messages.format(RefactoringCoreMessages.SelfEncapsulateField_setter_pattern, BasicElementLabels.getJavaElementName(fSetterName)));
    String visibility = JdtFlags.getVisibilityString(fVisibility);
    if (//$NON-NLS-1$
    "".equals(visibility))
        visibility = RefactoringCoreMessages.SelfEncapsulateField_default_visibility;
    comment.addSetting(Messages.format(RefactoringCoreMessages.SelfEncapsulateField_visibility_pattern, visibility));
    if (fEncapsulateDeclaringClass)
        comment.addSetting(RefactoringCoreMessages.SelfEncapsulateField_use_accessors);
    else
        comment.addSetting(RefactoringCoreMessages.SelfEncapsulateField_do_not_use_accessors);
    if (fGenerateJavadoc)
        comment.addSetting(RefactoringCoreMessages.SelfEncapsulateField_generate_comments);
    final EncapsulateFieldDescriptor descriptor = RefactoringSignatureDescriptorFactory.createEncapsulateFieldDescriptor(project, description, comment.asString(), arguments, flags);
    arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT, JavaRefactoringDescriptorUtil.elementToHandle(project, fField));
    arguments.put(ATTRIBUTE_VISIBILITY, new Integer(fVisibility).toString());
    arguments.put(ATTRIBUTE_INSERTION, new Integer(fInsertionIndex).toString());
    arguments.put(ATTRIBUTE_SETTER, fSetterName);
    arguments.put(ATTRIBUTE_GETTER, fGetterName);
    arguments.put(ATTRIBUTE_COMMENTS, Boolean.valueOf(fGenerateJavadoc).toString());
    arguments.put(ATTRIBUTE_DECLARING, Boolean.valueOf(fEncapsulateDeclaringClass).toString());
    final DynamicValidationRefactoringChange result = new DynamicValidationRefactoringChange(descriptor, getName());
    TextChange[] changes = fChangeManager.getAllChanges();
    pm.beginTask(NO_NAME, changes.length);
    pm.setTaskName(RefactoringCoreMessages.SelfEncapsulateField_create_changes);
    for (int i = 0; i < changes.length; i++) {
        result.add(changes[i]);
        pm.worked(1);
    }
    pm.done();
    return result;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) EncapsulateFieldDescriptor(org.eclipse.jdt.core.refactoring.descriptors.EncapsulateFieldDescriptor) HashMap(java.util.HashMap) DynamicValidationRefactoringChange(org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange) TextChange(org.eclipse.ltk.core.refactoring.TextChange) IType(org.eclipse.jdt.core.IType) JDTRefactoringDescriptorComment(org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment) IJavaProject(org.eclipse.jdt.core.IJavaProject)

Example 10 with TextChange

use of org.eclipse.ltk.core.refactoring.TextChange in project che by eclipse.

the class CUCorrectionProposal method createTextChange.

/**
	 * Creates the text change for this proposal.
	 * This method is only called once and only when no text change has been passed in
	 * {@link #CUCorrectionProposal(String, ICompilationUnit, TextChange, int, Image)}.
	 *
	 * @return the created text change
	 * @throws CoreException if the creation of the text change failed
	 */
protected TextChange createTextChange() throws CoreException {
    ICompilationUnit cu = getCompilationUnit();
    String name = getName();
    TextChange change;
    if (!cu.getResource().exists()) {
        String source;
        try {
            source = cu.getSource();
        } catch (JavaModelException e) {
            JavaPlugin.log(e);
            // empty
            source = new String();
        }
        Document document = new Document(source);
        document.setInitialLineDelimiter(StubUtility.getLineDelimiterUsed(cu));
        change = new DocumentChange(name, document);
    } else {
        CompilationUnitChange cuChange = new CompilationUnitChange(name, cu);
        cuChange.setSaveMode(TextFileChange.LEAVE_DIRTY);
        change = cuChange;
    }
    TextEdit rootEdit = new MultiTextEdit();
    change.setEdit(rootEdit);
    // initialize text change
    IDocument document = change.getCurrentDocument(new NullProgressMonitor());
    addEdits(document, rootEdit);
    return change;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) JavaModelException(org.eclipse.jdt.core.JavaModelException) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) TextChange(org.eclipse.ltk.core.refactoring.TextChange) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) DocumentChange(org.eclipse.ltk.core.refactoring.DocumentChange) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) IDocument(org.eclipse.jface.text.IDocument) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

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