Search in sources :

Example 36 with RefactoringStatus

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

the class PromoteTempToFieldRefactoring method initialize.

private RefactoringStatus initialize(JavaRefactoringArguments arguments) {
    fSelfInitializing = true;
    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.CONVERT_LOCAL_VARIABLE);
        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 = flag;
    }
    final String initialize = arguments.getAttribute(ATTRIBUTE_INITIALIZE);
    if (initialize != null && !"".equals(initialize)) {
        //$NON-NLS-1$
        int value = 0;
        try {
            value = Integer.parseInt(initialize);
        } catch (NumberFormatException exception) {
            return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_INITIALIZE));
        }
        fInitializeIn = value;
    }
    final String name = arguments.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME);
    if (//$NON-NLS-1$
    name != null && !"".equals(name))
        fFieldName = name;
    else
        return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME));
    final String declareStatic = arguments.getAttribute(ATTRIBUTE_STATIC);
    if (declareStatic != null) {
        fDeclareStatic = Boolean.valueOf(declareStatic).booleanValue();
    } else
        return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_STATIC));
    final String declareFinal = arguments.getAttribute(ATTRIBUTE_FINAL);
    if (declareFinal != null) {
        fDeclareFinal = Boolean.valueOf(declareFinal).booleanValue();
    } else
        return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_FINAL));
    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 37 with RefactoringStatus

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

the class ReplaceInvocationsRefactoring method checkFinalConditions.

@Override
public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException {
    //$NON-NLS-1$
    pm.beginTask("", 20);
    fChangeManager = new TextChangeManager();
    RefactoringStatus result = new RefactoringStatus();
    fSourceProvider = resolveSourceProvider(fMethodBinding, result);
    if (result.hasFatalError())
        return result;
    result.merge(fSourceProvider.checkActivation());
    if (result.hasFatalError())
        return result;
    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));
                    }
                }
                // do this after we have inlined the method calls. We still want
                // to generate the modifications.
                result.merge(nestedInvocations);
            }
            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 38 with RefactoringStatus

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

the class ReplaceInvocationsRefactoring method checkInitialConditions.

@Override
public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException {
    // TargetProvider must get an untampered AST with original invocation node
    // SourceProvider must get a tweaked AST with method body / parameter names replaced
    RefactoringStatus result = new RefactoringStatus();
    if (fMethod == null) {
        if (!(fSelectionTypeRoot instanceof ICompilationUnit))
            return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReplaceInvocationsRefactoring_cannot_replace_in_binary);
        ICompilationUnit cu = (ICompilationUnit) fSelectionTypeRoot;
        CompilationUnit root = new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL).parse(cu, true);
        fSelectionNode = getTargetNode(cu, root, fSelectionStart, fSelectionLength);
        if (fSelectionNode == null)
            return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ReplaceInvocationsRefactoring_select_method_to_apply);
        if (fSelectionNode.getNodeType() == ASTNode.METHOD_DECLARATION) {
            MethodDeclaration methodDeclaration = (MethodDeclaration) fSelectionNode;
            fTargetProvider = TargetProvider.create(methodDeclaration);
            fMethodBinding = methodDeclaration.resolveBinding();
        } else {
            MethodInvocation methodInvocation = (MethodInvocation) fSelectionNode;
            fTargetProvider = TargetProvider.create(cu, methodInvocation);
            fMethodBinding = methodInvocation.resolveMethodBinding();
        }
        if (fMethodBinding == null)
            return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InlineMethodRefactoring_error_noMethodDeclaration);
        fMethod = (IMethod) fMethodBinding.getJavaElement();
    } else {
        ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
        parser.setProject(fMethod.getJavaProject());
        IBinding[] bindings = parser.createBindings(new IJavaElement[] { fMethod }, null);
        fMethodBinding = (IMethodBinding) bindings[0];
        if (fMethodBinding == null)
            return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InlineMethodRefactoring_error_noMethodDeclaration);
        fTargetProvider = TargetProvider.create(fMethodBinding);
    }
    result.merge(fTargetProvider.checkActivation());
    return result;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) RefactoringASTParser(org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) IBinding(org.eclipse.jdt.core.dom.IBinding) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) RefactoringASTParser(org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser) ASTParser(org.eclipse.jdt.core.dom.ASTParser)

Example 39 with RefactoringStatus

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

the class CreateFileChange method isValid.

@Override
public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException {
    RefactoringStatus result = new RefactoringStatus();
    IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(fPath);
    URI location = file.getLocationURI();
    if (location == null) {
        result.addFatalError(Messages.format(NLSChangesMessages.CreateFileChange_error_unknownLocation, BasicElementLabels.getPathLabel(file.getFullPath(), false)));
        return result;
    }
    //		}
    return result;
}
Also used : IFile(org.eclipse.core.resources.IFile) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) URI(java.net.URI)

Example 40 with RefactoringStatus

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

the class RefactoringSession method createChange.

public ChangeCreationResult createChange() throws RefactoringException {
    Change change = createChange(new CreateChangeOperation(new CheckConditionsOperation(refactoring, CheckConditionsOperation.FINAL_CONDITIONS), RefactoringStatus.FATAL), true);
    // Status has been updated since we have passed true
    RefactoringStatus status = conditionCheckingStatus;
    // Creating the change has been canceled
    if (change == null && status == null) {
        internalSetChange(change);
        throw new RefactoringException("Creating the change has been canceled");
    }
    // Set change if we don't have fatal errors.
    if (!status.hasFatalError()) {
        internalSetChange(change);
    }
    ChangeCreationResult result = DtoFactory.newDto(ChangeCreationResult.class);
    result.setStatus(DtoConverter.toRefactoringStatusDto(status));
    result.setCanShowPreviewPage(status.isOK());
    return result;
}
Also used : CreateChangeOperation(org.eclipse.ltk.core.refactoring.CreateChangeOperation) CheckConditionsOperation(org.eclipse.ltk.core.refactoring.CheckConditionsOperation) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) ChangeCreationResult(org.eclipse.che.ide.ext.java.shared.dto.refactoring.ChangeCreationResult) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) MoveCompilationUnitChange(org.eclipse.jdt.internal.corext.refactoring.changes.MoveCompilationUnitChange) UndoTextFileChange(org.eclipse.ltk.core.refactoring.UndoTextFileChange) Change(org.eclipse.ltk.core.refactoring.Change) DynamicValidationStateChange(org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationStateChange) RenameCompilationUnitChange(org.eclipse.jdt.internal.corext.refactoring.changes.RenameCompilationUnitChange) RenamePackageChange(org.eclipse.jdt.internal.corext.refactoring.changes.RenamePackageChange) RefactoringException(org.eclipse.che.plugin.java.server.refactoring.RefactoringException)

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