Search in sources :

Example 1 with CompilationUnitRewrite

use of org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite 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 2 with CompilationUnitRewrite

use of org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite in project che by eclipse.

the class RenameFieldProcessor method addDelegates.

private RefactoringStatus addDelegates() throws JavaModelException, CoreException {
    RefactoringStatus status = new RefactoringStatus();
    CompilationUnitRewrite rewrite = new CompilationUnitRewrite(fField.getCompilationUnit());
    rewrite.setResolveBindings(true);
    // add delegate for the field
    if (RefactoringAvailabilityTester.isDelegateCreationAvailable(fField)) {
        FieldDeclaration fieldDeclaration = ASTNodeSearchUtil.getFieldDeclarationNode(fField, rewrite.getRoot());
        if (fieldDeclaration.fragments().size() > 1) {
            status.addWarning(Messages.format(RefactoringCoreMessages.DelegateCreator_cannot_create_field_delegate_more_than_one_fragment, BasicElementLabels.getJavaElementName(fField.getElementName())), JavaStatusContext.create(fField));
        } else if (((VariableDeclarationFragment) fieldDeclaration.fragments().get(0)).getInitializer() == null) {
            status.addWarning(Messages.format(RefactoringCoreMessages.DelegateCreator_cannot_create_field_delegate_no_initializer, BasicElementLabels.getJavaElementName(fField.getElementName())), JavaStatusContext.create(fField));
        } else {
            DelegateFieldCreator creator = new DelegateFieldCreator();
            creator.setDeclareDeprecated(fDelegateDeprecation);
            creator.setDeclaration(fieldDeclaration);
            creator.setNewElementName(getNewElementName());
            creator.setSourceRewrite(rewrite);
            creator.prepareDelegate();
            creator.createEdit();
        }
    }
    // there may be getters even if the field is static final
    if (getGetter() != null && fRenameGetter)
        addMethodDelegate(getGetter(), getNewGetterName(), rewrite);
    if (getSetter() != null && fRenameSetter)
        addMethodDelegate(getSetter(), getNewSetterName(), rewrite);
    final CompilationUnitChange change = rewrite.createChange(true);
    if (change != null) {
        change.setKeepPreviewEdits(true);
        fChangeManager.manage(fField.getCompilationUnit(), change);
    }
    return status;
}
Also used : CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) DelegateFieldCreator(org.eclipse.jdt.internal.corext.refactoring.delegates.DelegateFieldCreator) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

Example 3 with CompilationUnitRewrite

use of org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite in project che by eclipse.

the class RenameNonVirtualMethodProcessor method addDeclarationUpdate.

private void addDeclarationUpdate(TextChangeManager manager) throws CoreException {
    if (getDelegateUpdating()) {
        // create the delegate
        CompilationUnitRewrite rewrite = new CompilationUnitRewrite(getDeclaringCU());
        rewrite.setResolveBindings(true);
        MethodDeclaration methodDeclaration = ASTNodeSearchUtil.getMethodDeclarationNode(getMethod(), rewrite.getRoot());
        DelegateMethodCreator creator = new DelegateMethodCreator();
        creator.setDeclaration(methodDeclaration);
        creator.setDeclareDeprecated(getDeprecateDelegates());
        creator.setSourceRewrite(rewrite);
        creator.setCopy(true);
        creator.setNewElementName(getNewElementName());
        creator.prepareDelegate();
        creator.createEdit();
        CompilationUnitChange cuChange = rewrite.createChange(true);
        if (cuChange != null) {
            cuChange.setKeepPreviewEdits(true);
            manager.manage(getDeclaringCU(), cuChange);
        }
    }
    String editName = RefactoringCoreMessages.RenameMethodRefactoring_update_declaration;
    ISourceRange nameRange = getMethod().getNameRange();
    ReplaceEdit replaceEdit = new ReplaceEdit(nameRange.getOffset(), nameRange.getLength(), getNewElementName());
    addTextEdit(manager.get(getDeclaringCU()), editName, replaceEdit);
}
Also used : CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite) DelegateMethodCreator(org.eclipse.jdt.internal.corext.refactoring.delegates.DelegateMethodCreator) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 4 with CompilationUnitRewrite

use of org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite in project che by eclipse.

the class InferTypeArgumentsRefactoring method rewriteDeclarations.

private void rewriteDeclarations(InferTypeArgumentsUpdate update, IProgressMonitor pm) throws CoreException {
    HashMap<ICompilationUnit, CuUpdate> updates = update.getUpdates();
    Set<Entry<ICompilationUnit, CuUpdate>> entrySet = updates.entrySet();
    //$NON-NLS-1$
    pm.beginTask("", entrySet.size());
    pm.setTaskName(RefactoringCoreMessages.InferTypeArgumentsRefactoring_creatingChanges);
    for (Iterator<Entry<ICompilationUnit, CuUpdate>> iter = entrySet.iterator(); iter.hasNext(); ) {
        if (pm.isCanceled())
            throw new OperationCanceledException();
        Entry<ICompilationUnit, CuUpdate> entry = iter.next();
        ICompilationUnit cu = entry.getKey();
        pm.worked(1);
        pm.subTask(BasicElementLabels.getFileName(cu));
        CompilationUnitRewrite rewrite = new CompilationUnitRewrite(cu);
        rewrite.setResolveBindings(false);
        CuUpdate cuUpdate = entry.getValue();
        for (Iterator<CollectionElementVariable2> cvIter = cuUpdate.getDeclarations().iterator(); cvIter.hasNext(); ) {
            ConstraintVariable2 cv = cvIter.next();
            rewriteConstraintVariable(cv, rewrite, fTCModel, fLeaveUnconstrainedRaw, null);
        }
        for (Iterator<CastVariable2> castsIter = cuUpdate.getCastsToRemove().iterator(); castsIter.hasNext(); ) {
            CastVariable2 castCv = castsIter.next();
            rewriteCastVariable(castCv, rewrite, fTCModel);
        }
        CompilationUnitChange change = rewrite.createChange(true);
        if (change != null) {
            fChangeManager.manage(cu, change);
        }
    }
}
Also used : CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CollectionElementVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.CollectionElementVariable2) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) Entry(java.util.Map.Entry) CastVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.CastVariable2) CuUpdate(org.eclipse.jdt.internal.corext.refactoring.generics.InferTypeArgumentsUpdate.CuUpdate) ConstraintVariable2(org.eclipse.jdt.internal.corext.refactoring.typeconstraints2.ConstraintVariable2) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

Example 5 with CompilationUnitRewrite

use of org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite in project che by eclipse.

the class CompilationUnitRewriteOperationsFix method createChange.

//	/**
//	 * {@inheritDoc}
//	 */
//	@Override
//	public LinkedProposalModel getLinkedPositions() {
//		if (!fLinkedProposalModel.hasLinkedPositions())
//			return null;
//
//		return fLinkedProposalModel;
//	}
/**
	 * {@inheritDoc}
	 */
public CompilationUnitChange createChange(IProgressMonitor progressMonitor) throws CoreException {
    CompilationUnitRewrite cuRewrite = new CompilationUnitRewrite((ICompilationUnit) fCompilationUnit.getJavaElement(), fCompilationUnit);
    fLinkedProposalModel.clear();
    for (int i = 0; i < fOperations.length; i++) {
        CompilationUnitRewriteOperation operation = fOperations[i];
        operation.rewriteAST(cuRewrite, fLinkedProposalModel);
    }
    CompilationUnitChange result = cuRewrite.createChange(getDisplayString(), true, null);
    if (result == null)
        throw new CoreException(new Status(IStatus.ERROR, JavaPlugin.ID_PLUGIN, Messages.format(FixMessages.CompilationUnitRewriteOperationsFix_nullChangeError, getDisplayString())));
    return result;
}
Also used : CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite) Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) CoreException(org.eclipse.core.runtime.CoreException) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

Aggregations

CompilationUnitRewrite (org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite)22 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)10 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)9 CompilationUnitChange (org.eclipse.jdt.core.refactoring.CompilationUnitChange)6 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)5 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)5 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)4 IMember (org.eclipse.jdt.core.IMember)4 ASTNode (org.eclipse.jdt.core.dom.ASTNode)4 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)4 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)4 IMethod (org.eclipse.jdt.core.IMethod)3 IType (org.eclipse.jdt.core.IType)3 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)3 SuperMethodInvocation (org.eclipse.jdt.core.dom.SuperMethodInvocation)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)2 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)2 SearchMatch (org.eclipse.jdt.core.search.SearchMatch)2