Search in sources :

Example 21 with RefactoringStatus

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

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

the class RenameLocalVariableProcessor method initialize.

private RefactoringStatus initialize(JavaRefactoringArguments extended) {
    final String handle = extended.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT);
    if (handle != null) {
        final IJavaElement element = JavaRefactoringDescriptorUtil.handleToElement(extended.getProject(), handle, false);
        if (element != null && element.exists()) {
            if (element.getElementType() == IJavaElement.COMPILATION_UNIT) {
                fCu = (ICompilationUnit) element;
            } else if (element.getElementType() == IJavaElement.LOCAL_VARIABLE) {
                fLocalVariable = (ILocalVariable) element;
                fCu = (ICompilationUnit) fLocalVariable.getAncestor(IJavaElement.COMPILATION_UNIT);
                if (fCu == null)
                    return JavaRefactoringDescriptorUtil.createInputFatalStatus(element, getProcessorName(), IJavaRefactorings.RENAME_LOCAL_VARIABLE);
            } else
                return JavaRefactoringDescriptorUtil.createInputFatalStatus(element, getProcessorName(), IJavaRefactorings.RENAME_LOCAL_VARIABLE);
        } else
            return JavaRefactoringDescriptorUtil.createInputFatalStatus(element, getProcessorName(), IJavaRefactorings.RENAME_LOCAL_VARIABLE);
    } else
        return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT));
    final String name = extended.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME);
    if (//$NON-NLS-1$
    name != null && !"".equals(name))
        setNewElementName(name);
    else
        return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JavaRefactoringDescriptorUtil.ATTRIBUTE_NAME));
    if (fCu != null && fLocalVariable == null) {
        final String selection = extended.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) {
                try {
                    final IJavaElement[] elements = fCu.codeSelect(offset, length);
                    if (elements != null) {
                        for (int index = 0; index < elements.length; index++) {
                            final IJavaElement element = elements[index];
                            if (element instanceof ILocalVariable)
                                fLocalVariable = (ILocalVariable) element;
                        }
                    }
                    if (fLocalVariable == null)
                        return JavaRefactoringDescriptorUtil.createInputFatalStatus(null, getProcessorName(), IJavaRefactorings.RENAME_LOCAL_VARIABLE);
                } catch (JavaModelException exception) {
                    JavaPlugin.log(exception);
                }
            } 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 references = extended.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_REFERENCES);
    if (references != null) {
        fUpdateReferences = Boolean.valueOf(references).booleanValue();
    } else
        return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JavaRefactoringDescriptorUtil.ATTRIBUTE_REFERENCES));
    return new RefactoringStatus();
}
Also used : ILocalVariable(org.eclipse.jdt.core.ILocalVariable) IJavaElement(org.eclipse.jdt.core.IJavaElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) StringTokenizer(java.util.StringTokenizer) JavaModelException(org.eclipse.jdt.core.JavaModelException) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus)

Example 23 with RefactoringStatus

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

the class RenamePackageProcessor method checkTypeNameConflicts.

private RefactoringStatus checkTypeNameConflicts(ICompilationUnit iCompilationUnit, Set<String> topLevelTypeNames) throws CoreException {
    RefactoringStatus result = new RefactoringStatus();
    IType[] types = iCompilationUnit.getTypes();
    for (int i = 0; i < types.length; i++) {
        String name = types[i].getElementName();
        if (topLevelTypeNames.contains(name)) {
            String[] keys = { getElementLabel(iCompilationUnit.getParent()), getElementLabel(types[i]) };
            String msg = Messages.format(RefactoringCoreMessages.RenamePackageRefactoring_contains_type, keys);
            RefactoringStatusContext context = JavaStatusContext.create(types[i]);
            result.addError(msg, context);
        }
    }
    return result;
}
Also used : RefactoringStatusContext(org.eclipse.ltk.core.refactoring.RefactoringStatusContext) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IType(org.eclipse.jdt.core.IType)

Example 24 with RefactoringStatus

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

the class RenameTypeParameterProcessor method checkNewElementName.

public RefactoringStatus checkNewElementName(String name) throws CoreException {
    Assert.isNotNull(name);
    RefactoringStatus result = Checks.checkTypeParameterName(name, fTypeParameter);
    if (Checks.startsWithLowerCase(name))
        result.addWarning(RefactoringCoreMessages.RenameTypeParameterRefactoring_should_start_lowercase);
    if (Checks.isAlreadyNamed(fTypeParameter, name))
        result.addFatalError(RefactoringCoreMessages.RenameTypeParameterRefactoring_another_name);
    IMember member = fTypeParameter.getDeclaringMember();
    if (member instanceof IType) {
        IType type = (IType) member;
        if (type.getTypeParameter(name).exists())
            result.addFatalError(RefactoringCoreMessages.RenameTypeParameterRefactoring_class_type_parameter_already_defined);
    } else if (member instanceof IMethod) {
        IMethod method = (IMethod) member;
        if (method.getTypeParameter(name).exists())
            result.addFatalError(RefactoringCoreMessages.RenameTypeParameterRefactoring_method_type_parameter_already_defined);
    } else {
        //$NON-NLS-1$
        JavaPlugin.logErrorMessage("Unexpected sub-type of IMember: " + member.getClass().getName());
        Assert.isTrue(false);
    }
    return result;
}
Also used : RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IMethod(org.eclipse.jdt.core.IMethod) IMember(org.eclipse.jdt.core.IMember) IType(org.eclipse.jdt.core.IType)

Example 25 with RefactoringStatus

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

the class JavaRenameProcessor method checkFinalConditions.

@Override
public final RefactoringStatus checkFinalConditions(IProgressMonitor pm, CheckConditionsContext context) throws CoreException, OperationCanceledException {
    ResourceChangeChecker checker = (ResourceChangeChecker) context.getChecker(ResourceChangeChecker.class);
    IResourceChangeDescriptionFactory deltaFactory = checker.getDeltaFactory();
    RefactoringStatus result = doCheckFinalConditions(pm, context);
    if (result.hasFatalError())
        return result;
    IFile[] changed = getChangedFiles();
    for (int i = 0; i < changed.length; i++) {
        deltaFactory.change(changed[i]);
    }
    fRenameModifications = computeRenameModifications();
    fRenameModifications.buildDelta(deltaFactory);
    fRenameModifications.buildValidateEdits((ValidateEditChecker) context.getChecker(ValidateEditChecker.class));
    return result;
}
Also used : IFile(org.eclipse.core.resources.IFile) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IResourceChangeDescriptionFactory(org.eclipse.core.resources.mapping.IResourceChangeDescriptionFactory) ResourceChangeChecker(org.eclipse.ltk.core.refactoring.participants.ResourceChangeChecker)

Aggregations

RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)235 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 IJavaElement (org.eclipse.jdt.core.IJavaElement)28 Test (org.junit.Test)26 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)24 ArrayList (java.util.ArrayList)22 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)22 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 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)16 ASTNode (org.eclipse.jdt.core.dom.ASTNode)16 IField (org.eclipse.jdt.core.IField)15 IStatus (org.eclipse.core.runtime.IStatus)14 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)13 Refactoring (org.eclipse.ltk.core.refactoring.Refactoring)13 IFile (org.eclipse.core.resources.IFile)12