Search in sources :

Example 21 with SubProgressMonitor

use of org.eclipse.core.runtime.SubProgressMonitor in project che by eclipse.

the class IntroduceIndirectionRefactoring method adjustVisibility.

private RefactoringStatus adjustVisibility(IMember whoToAdjust, ModifierKeyword neededVisibility, boolean alsoIncreaseEnclosing, IProgressMonitor monitor) throws CoreException {
    Map<IMember, IncomingMemberVisibilityAdjustment> adjustments;
    if (isRewriteKept(whoToAdjust.getCompilationUnit()))
        adjustments = fIntermediaryAdjustments;
    else
        adjustments = new HashMap<IMember, IncomingMemberVisibilityAdjustment>();
    int existingAdjustments = adjustments.size();
    addAdjustment(whoToAdjust, neededVisibility, adjustments);
    if (alsoIncreaseEnclosing)
        while (whoToAdjust.getDeclaringType() != null) {
            whoToAdjust = whoToAdjust.getDeclaringType();
            addAdjustment(whoToAdjust, neededVisibility, adjustments);
        }
    boolean hasNewAdjustments = (adjustments.size() - existingAdjustments) > 0;
    if (hasNewAdjustments && ((whoToAdjust.isReadOnly() || whoToAdjust.isBinary())))
        return RefactoringStatus.createErrorStatus(Messages.format(RefactoringCoreMessages.IntroduceIndirectionRefactoring_cannot_update_binary_target_visibility, new String[] { JavaElementLabels.getElementLabel(whoToAdjust, JavaElementLabels.ALL_DEFAULT) }), JavaStatusContext.create(whoToAdjust));
    RefactoringStatus status = new RefactoringStatus();
    // Don't create a rewrite if it is not necessary
    if (!hasNewAdjustments)
        return status;
    try {
        monitor.beginTask(RefactoringCoreMessages.MemberVisibilityAdjustor_adjusting, 2);
        Map<ICompilationUnit, CompilationUnitRewrite> rewrites;
        if (!isRewriteKept(whoToAdjust.getCompilationUnit())) {
            CompilationUnitRewrite rewrite = new CompilationUnitRewrite(whoToAdjust.getCompilationUnit());
            rewrite.setResolveBindings(false);
            rewrites = new HashMap<ICompilationUnit, CompilationUnitRewrite>();
            rewrites.put(whoToAdjust.getCompilationUnit(), rewrite);
            status.merge(rewriteVisibility(adjustments, rewrites, new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL)));
            rewrite.attachChange((CompilationUnitChange) fTextChangeManager.get(whoToAdjust.getCompilationUnit()), true, new SubProgressMonitor(monitor, 1, SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
        }
    } finally {
        monitor.done();
    }
    return status;
}
Also used : CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite) IncomingMemberVisibilityAdjustment(org.eclipse.jdt.internal.corext.refactoring.structure.MemberVisibilityAdjustor.IncomingMemberVisibilityAdjustment) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) HashMap(java.util.HashMap) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IMember(org.eclipse.jdt.core.IMember) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor)

Example 22 with SubProgressMonitor

use of org.eclipse.core.runtime.SubProgressMonitor in project che by eclipse.

the class ExtractTempRefactoring method checkInitialConditions.

@Override
public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException {
    try {
        //$NON-NLS-1$
        pm.beginTask("", 6);
        RefactoringStatus result = Checks.validateModifiesFiles(ResourceUtil.getFiles(new ICompilationUnit[] { fCu }), getValidationContext());
        if (result.hasFatalError())
            return result;
        if (fCompilationUnitNode == null) {
            fCompilationUnitNode = RefactoringASTParser.parseWithASTProvider(fCu, true, new SubProgressMonitor(pm, 3));
        } else {
            pm.worked(3);
        }
        result.merge(checkSelection(new SubProgressMonitor(pm, 3)));
        if (!result.hasFatalError() && isLiteralNodeSelected())
            fReplaceAllOccurrences = false;
        return result;
    } finally {
        pm.done();
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor)

Example 23 with SubProgressMonitor

use of org.eclipse.core.runtime.SubProgressMonitor in project che by eclipse.

the class InlineConstantRefactoring method findReferences.

private SearchResultGroup[] findReferences(IProgressMonitor pm, RefactoringStatus status) throws JavaModelException {
    final RefactoringSearchEngine2 engine = new RefactoringSearchEngine2(SearchPattern.createPattern(fField, IJavaSearchConstants.REFERENCES));
    engine.setFiltering(true, true);
    engine.setScope(RefactoringScopeFactory.create(fField));
    engine.setStatus(status);
    engine.setRequestor(new IRefactoringSearchRequestor() {

        public SearchMatch acceptSearchMatch(SearchMatch match) {
            return match.isInsideDocComment() ? null : match;
        }
    });
    engine.searchPattern(new SubProgressMonitor(pm, 1));
    return (SearchResultGroup[]) engine.getResults();
}
Also used : IRefactoringSearchRequestor(org.eclipse.jdt.internal.corext.refactoring.IRefactoringSearchRequestor) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) RefactoringSearchEngine2(org.eclipse.jdt.internal.corext.refactoring.RefactoringSearchEngine2) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor)

Example 24 with SubProgressMonitor

use of org.eclipse.core.runtime.SubProgressMonitor in project che by eclipse.

the class InlineMethodRefactoring method checkFinalConditions.

@Override
public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException {
    //$NON-NLS-1$
    pm.beginTask("", 20);
    fChangeManager = new TextChangeManager();
    RefactoringStatus result = new RefactoringStatus();
    fSourceProvider.initialize();
    fTargetProvider.initialize();
    pm.setTaskName(RefactoringCoreMessages.InlineMethodRefactoring_searching);
    RefactoringStatus searchStatus = new RefactoringStatus();
    String binaryRefsDescription = Messages.format(RefactoringCoreMessages.ReferencesInBinaryContext_ref_in_binaries_description, BasicElementLabels.getJavaElementName(fSourceProvider.getMethodName()));
    ReferencesInBinaryContext binaryRefs = new ReferencesInBinaryContext(binaryRefsDescription);
    ICompilationUnit[] units = fTargetProvider.getAffectedCompilationUnits(searchStatus, binaryRefs, new SubProgressMonitor(pm, 1));
    binaryRefs.addErrorIfNecessary(searchStatus);
    if (searchStatus.hasFatalError()) {
        result.merge(searchStatus);
        return result;
    }
    IFile[] filesToBeModified = getFilesToBeModified(units);
    result.merge(Checks.validateModifiesFiles(filesToBeModified, getValidationContext()));
    if (result.hasFatalError())
        return result;
    result.merge(ResourceChangeChecker.checkFilesToBeChanged(filesToBeModified, new SubProgressMonitor(pm, 1)));
    checkOverridden(result, new SubProgressMonitor(pm, 4));
    IProgressMonitor sub = new SubProgressMonitor(pm, 15);
    //$NON-NLS-1$
    sub.beginTask("", units.length * 3);
    for (int c = 0; c < units.length; c++) {
        ICompilationUnit unit = units[c];
        sub.subTask(Messages.format(RefactoringCoreMessages.InlineMethodRefactoring_processing, BasicElementLabels.getFileName(unit)));
        CallInliner inliner = null;
        try {
            boolean added = false;
            MultiTextEdit root = new MultiTextEdit();
            CompilationUnitChange change = (CompilationUnitChange) fChangeManager.get(unit);
            change.setEdit(root);
            BodyDeclaration[] bodies = fTargetProvider.getAffectedBodyDeclarations(unit, new SubProgressMonitor(pm, 1));
            if (bodies.length == 0)
                continue;
            inliner = new CallInliner(unit, (CompilationUnit) bodies[0].getRoot(), fSourceProvider);
            for (int b = 0; b < bodies.length; b++) {
                BodyDeclaration body = bodies[b];
                inliner.initialize(body);
                RefactoringStatus nestedInvocations = new RefactoringStatus();
                ASTNode[] invocations = removeNestedCalls(nestedInvocations, unit, fTargetProvider.getInvocations(body, new SubProgressMonitor(sub, 2)));
                for (int i = 0; i < invocations.length; i++) {
                    ASTNode invocation = invocations[i];
                    result.merge(inliner.initialize(invocation, fTargetProvider.getStatusSeverity()));
                    if (result.hasFatalError())
                        break;
                    if (result.getSeverity() < fTargetProvider.getStatusSeverity()) {
                        added = true;
                        TextEditGroup group = new TextEditGroup(RefactoringCoreMessages.InlineMethodRefactoring_edit_inline);
                        change.addTextEditGroup(group);
                        result.merge(inliner.perform(group));
                    } else {
                        fDeleteSource = false;
                    }
                }
                // to generate the modifications.
                if (!nestedInvocations.isOK()) {
                    result.merge(nestedInvocations);
                    fDeleteSource = false;
                }
            }
            if (!added) {
                fChangeManager.remove(unit);
            } else {
                root.addChild(inliner.getModifications());
                ImportRewrite rewrite = inliner.getImportEdit();
                if (rewrite.hasRecordedChanges()) {
                    TextEdit edit = rewrite.rewriteImports(null);
                    if (edit instanceof MultiTextEdit ? ((MultiTextEdit) edit).getChildrenSize() > 0 : true) {
                        root.addChild(edit);
                        change.addTextEditGroup(new TextEditGroup(RefactoringCoreMessages.InlineMethodRefactoring_edit_import, new TextEdit[] { edit }));
                    }
                }
            }
        } finally {
            if (inliner != null)
                inliner.dispose();
        }
        sub.worked(1);
        if (sub.isCanceled())
            throw new OperationCanceledException();
    }
    result.merge(searchStatus);
    sub.done();
    pm.done();
    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) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) ReferencesInBinaryContext(org.eclipse.jdt.internal.corext.refactoring.base.ReferencesInBinaryContext) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) ASTNode(org.eclipse.jdt.core.dom.ASTNode) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) TextEditGroup(org.eclipse.text.edits.TextEditGroup) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextChangeManager(org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange)

Example 25 with SubProgressMonitor

use of org.eclipse.core.runtime.SubProgressMonitor in project che by eclipse.

the class InlineMethodRefactoring method checkOverridden.

private void checkOverridden(RefactoringStatus status, IProgressMonitor pm) throws JavaModelException {
    //$NON-NLS-1$
    pm.beginTask("", 9);
    pm.setTaskName(RefactoringCoreMessages.InlineMethodRefactoring_checking_overridden);
    MethodDeclaration decl = fSourceProvider.getDeclaration();
    IMethod method = (IMethod) decl.resolveBinding().getJavaElement();
    if (method == null || Flags.isPrivate(method.getFlags())) {
        pm.worked(8);
        return;
    }
    IType type = method.getDeclaringType();
    ITypeHierarchy hierarchy = type.newTypeHierarchy(new SubProgressMonitor(pm, 6));
    checkSubTypes(status, method, hierarchy.getAllSubtypes(type), new SubProgressMonitor(pm, 1));
    checkSuperClasses(status, method, hierarchy.getAllSuperclasses(type), new SubProgressMonitor(pm, 1));
    checkSuperInterfaces(status, method, hierarchy.getAllSuperInterfaces(type), new SubProgressMonitor(pm, 1));
    //$NON-NLS-1$
    pm.setTaskName("");
}
Also used : ITypeHierarchy(org.eclipse.jdt.core.ITypeHierarchy) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) IMethod(org.eclipse.jdt.core.IMethod) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) IType(org.eclipse.jdt.core.IType)

Aggregations

SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)213 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)53 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)53 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)39 CoreException (org.eclipse.core.runtime.CoreException)38 ArrayList (java.util.ArrayList)36 IFile (org.eclipse.core.resources.IFile)33 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)31 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)29 IOException (java.io.IOException)23 IType (org.eclipse.jdt.core.IType)19 HashSet (java.util.HashSet)17 IProject (org.eclipse.core.resources.IProject)14 IResource (org.eclipse.core.resources.IResource)14 File (java.io.File)13 List (java.util.List)13 IPath (org.eclipse.core.runtime.IPath)13 IMethod (org.eclipse.jdt.core.IMethod)13 HashMap (java.util.HashMap)12 InvocationTargetException (java.lang.reflect.InvocationTargetException)11