Search in sources :

Example 81 with RefactoringStatus

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

the class ExtractConstantRefactoring method initialize.

private RefactoringStatus initialize(JavaRefactoringArguments arguments) {
    final String selection = arguments.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION);
    if (selection != null) {
        int offset = -1;
        int length = -1;
        final StringTokenizer tokenizer = new StringTokenizer(selection);
        if (tokenizer.hasMoreTokens())
            offset = Integer.valueOf(tokenizer.nextToken()).intValue();
        if (tokenizer.hasMoreTokens())
            length = Integer.valueOf(tokenizer.nextToken()).intValue();
        if (offset >= 0 && length >= 0) {
            fSelectionStart = offset;
            fSelectionLength = length;
        } else
            return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_illegal_argument, new Object[] { selection, JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION }));
    } else
        return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION));
    final String handle = arguments.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT);
    if (handle != null) {
        final IJavaElement element = JavaRefactoringDescriptorUtil.handleToElement(arguments.getProject(), handle, false);
        if (element == null || !element.exists() || element.getElementType() != IJavaElement.COMPILATION_UNIT)
            return JavaRefactoringDescriptorUtil.createInputFatalStatus(element, getName(), IJavaRefactorings.EXTRACT_CONSTANT);
        else
            fCu = (ICompilationUnit) element;
    } else
        return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT));
    final String visibility = arguments.getAttribute(ATTRIBUTE_VISIBILITY);
    if (visibility != null && !"".equals(visibility)) {
        //$NON-NLS-1$
        int flag = 0;
        try {
            flag = Integer.parseInt(visibility);
        } catch (NumberFormatException exception) {
            return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_VISIBILITY));
        }
        fVisibility = JdtFlags.getVisibilityString(flag);
    }
    final String name = arguments.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME);
    if (//$NON-NLS-1$
    name != null && !"".equals(name))
        fConstantName = name;
    else
        return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME));
    final String replace = arguments.getAttribute(ATTRIBUTE_REPLACE);
    if (replace != null) {
        fReplaceAllOccurrences = Boolean.valueOf(replace).booleanValue();
    } else
        return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_REPLACE));
    final String declareFinal = arguments.getAttribute(ATTRIBUTE_QUALIFY);
    if (declareFinal != null) {
        fQualifyReferencesWithDeclaringClassName = Boolean.valueOf(declareFinal).booleanValue();
    } else
        return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_QUALIFY));
    return new RefactoringStatus();
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) StringTokenizer(java.util.StringTokenizer) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus)

Example 82 with RefactoringStatus

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

the class ExtractMethodAnalyzer method checkInitialConditions.

public RefactoringStatus checkInitialConditions(ImportRewrite rewriter) {
    RefactoringStatus result = getStatus();
    checkExpression(result);
    if (result.hasFatalError())
        return result;
    List<ASTNode> validDestinations = new ArrayList<ASTNode>();
    ASTNode destination = ASTResolving.findParentType(fEnclosingBodyDeclaration.getParent());
    while (destination != null) {
        if (isValidDestination(destination)) {
            validDestinations.add(destination);
        }
        destination = ASTResolving.findParentType(destination.getParent());
    }
    if (validDestinations.size() == 0) {
        result.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_no_valid_destination_type);
        return result;
    }
    fReturnKind = UNDEFINED;
    fMaxVariableId = LocalVariableIndex.perform(fEnclosingBodyDeclaration);
    if (analyzeSelection(result).hasFatalError())
        return result;
    int returns = fReturnKind == NO ? 0 : 1;
    if (fReturnValue != null) {
        fReturnKind = ACCESS_TO_LOCAL;
        returns++;
    }
    if (isExpressionSelected()) {
        fReturnKind = EXPRESSION;
        returns++;
    }
    if (returns > 1) {
        result.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_ambiguous_return_value, JavaStatusContext.create(fCUnit, getSelection()));
        fReturnKind = MULTIPLE;
        return result;
    }
    initReturnType(rewriter);
    return result;
}
Also used : ArrayList(java.util.ArrayList) ASTNode(org.eclipse.jdt.core.dom.ASTNode) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus)

Example 83 with RefactoringStatus

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

the class InlineConstantRefactoring method checkFinalConditions.

@Override
public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException {
    RefactoringStatus result = new RefactoringStatus();
    //$NON-NLS-1$
    pm.beginTask("", 3);
    try {
        fSelectionCuRewrite.clearASTAndImportRewrites();
        fDeclarationCuRewrite.clearASTAndImportRewrites();
        List<CompilationUnitChange> changes = new ArrayList<CompilationUnitChange>();
        HashSet<SimpleName> staticImportsInInitializer = new HashSet<SimpleName>();
        ImportReferencesCollector.collect(getInitializer(), fField.getJavaProject(), null, new ArrayList<SimpleName>(), staticImportsInInitializer);
        if (getReplaceAllReferences()) {
            SearchResultGroup[] searchResultGroups = findReferences(pm, result);
            for (int i = 0; i < searchResultGroups.length; i++) {
                if (pm.isCanceled())
                    throw new OperationCanceledException();
                SearchResultGroup group = searchResultGroups[i];
                ICompilationUnit cu = group.getCompilationUnit();
                CompilationUnitRewrite cuRewrite = getCuRewrite(cu);
                Name[] references = extractReferenceNodes(group.getSearchResults(), cuRewrite.getRoot());
                InlineTargetCompilationUnit targetCompilationUnit = new InlineTargetCompilationUnit(cuRewrite, references, this, staticImportsInInitializer);
                CompilationUnitChange change = targetCompilationUnit.getChange();
                if (change != null)
                    changes.add(change);
            }
        } else {
            Assert.isTrue(!isDeclarationSelected());
            InlineTargetCompilationUnit targetForOnlySelectedReference = new InlineTargetCompilationUnit(fSelectionCuRewrite, new Name[] { fSelectedConstantName }, this, staticImportsInInitializer);
            CompilationUnitChange change = targetForOnlySelectedReference.getChange();
            if (change != null)
                changes.add(change);
        }
        if (result.hasFatalError())
            return result;
        if (getRemoveDeclaration() && getReplaceAllReferences()) {
            boolean declarationRemoved = false;
            for (Iterator<CompilationUnitChange> iter = changes.iterator(); iter.hasNext(); ) {
                CompilationUnitChange change = iter.next();
                if (change.getCompilationUnit().equals(fDeclarationCuRewrite.getCu())) {
                    declarationRemoved = true;
                    break;
                }
            }
            if (!declarationRemoved) {
                InlineTargetCompilationUnit targetForDeclaration = new InlineTargetCompilationUnit(fDeclarationCuRewrite, new Name[0], this, staticImportsInInitializer);
                CompilationUnitChange change = targetForDeclaration.getChange();
                if (change != null)
                    changes.add(change);
            }
        }
        ICompilationUnit[] cus = new ICompilationUnit[changes.size()];
        for (int i = 0; i < changes.size(); i++) {
            CompilationUnitChange change = changes.get(i);
            cus[i] = change.getCompilationUnit();
        }
        result.merge(Checks.validateModifiesFiles(ResourceUtil.getFiles(cus), getValidationContext()));
        pm.worked(1);
        fChanges = changes.toArray(new CompilationUnitChange[changes.size()]);
        return result;
    } finally {
        pm.done();
    }
}
Also used : CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SimpleName(org.eclipse.jdt.core.dom.SimpleName) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ArrayList(java.util.ArrayList) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) SearchResultGroup(org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange) HashSet(java.util.HashSet)

Example 84 with RefactoringStatus

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

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

the class InlineMethodRefactoring method checkInitialConditions.

@Override
public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException {
    RefactoringStatus result = new RefactoringStatus();
    if (fSourceProvider == null && Invocations.isInvocation(fInitialNode)) {
        fSourceProvider = resolveSourceProvider(result, fInitialTypeRoot, fInitialNode);
        if (result.hasFatalError())
            return result;
    }
    result.merge(fSourceProvider.checkActivation());
    result.merge(fTargetProvider.checkActivation());
    return result;
}
Also used : RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus)

Aggregations

RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)251 IType (org.eclipse.jdt.core.IType)62 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)53 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)30 IMethod (org.eclipse.jdt.core.IMethod)29 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)28 IJavaElement (org.eclipse.jdt.core.IJavaElement)28 Test (org.junit.Test)26 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)24 ArrayList (java.util.ArrayList)23 BaseTest (org.eclipse.che.plugin.java.server.che.BaseTest)21 RenameRefactoring (org.eclipse.ltk.core.refactoring.participants.RenameRefactoring)19 RenameJavaElementDescriptor (org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor)18 IFile (org.eclipse.core.resources.IFile)16 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)16 ASTNode (org.eclipse.jdt.core.dom.ASTNode)16 CoreException (org.eclipse.core.runtime.CoreException)15 IField (org.eclipse.jdt.core.IField)15 IStatus (org.eclipse.core.runtime.IStatus)14 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)13