Search in sources :

Example 41 with RefactoringStatus

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

the class RenameLinkedModeRefactoringSession method doRename.

/**
     * Make rename operation.
     * @param newName the name which will be applied
     * @return result of the rename operation
     * @throws CoreException if an error occurs while creating the refactoring instance
     * @throws InvocationTargetException if an error occurred while executing the
     * operation.
     * @throws InterruptedException if the operation has been canceled by the
     * user.
     */
public RefactoringResult doRename(String newName) throws CoreException, InvocationTargetException, InterruptedException {
    if (fOriginalName.equals(newName)) {
        return DtoConverter.toRefactoringResultDto(new RefactoringStatus());
    }
    RenameSupport renameSupport = undoAndCreateRenameSupport(newName);
    if (renameSupport == null)
        return DtoConverter.toRefactoringResultDto(RefactoringStatus.createFatalErrorStatus("Can't create rename refactoring"));
    RefactoringResult refactoringResult = DtoConverter.toRefactoringResultDto(renameSupport.perform());
    PerformChangeOperation operation = renameSupport.getfPerformChangeOperation();
    if (operation == null) {
        return refactoringResult;
    }
    CompositeChange operationChange = (CompositeChange) operation.getUndoChange();
    Change[] changes = operationChange.getChildren();
    List<ChangeInfo> changesInfo = new ArrayList<>();
    prepareChangesInfo(changes, changesInfo);
    refactoringResult.setChanges(changesInfo);
    return refactoringResult;
}
Also used : PerformChangeOperation(org.eclipse.ltk.core.refactoring.PerformChangeOperation) ChangeInfo(org.eclipse.che.ide.ext.java.shared.dto.refactoring.ChangeInfo) RefactoringResult(org.eclipse.che.ide.ext.java.shared.dto.refactoring.RefactoringResult) ArrayList(java.util.ArrayList) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) Change(org.eclipse.ltk.core.refactoring.Change) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) RenameSupport(org.eclipse.jdt.ui.refactoring.RenameSupport)

Example 42 with RefactoringStatus

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

the class RefactoringTest method performRefactoring.

protected final RefactoringStatus performRefactoring(Refactoring ref, boolean providesUndo) throws Exception {
    performDummySearch();
    IUndoManager undoManager = getUndoManager();
    if (DESCRIPTOR_TEST) {
        final CreateChangeOperation create = new CreateChangeOperation(new CheckConditionsOperation(ref, CheckConditionsOperation.ALL_CONDITIONS), RefactoringStatus.FATAL);
        create.run(new NullProgressMonitor());
        RefactoringStatus checkingStatus = create.getConditionCheckingStatus();
        if (!checkingStatus.isOK())
            return checkingStatus;
        Change change = create.getChange();
        ChangeDescriptor descriptor = change.getDescriptor();
        if (descriptor instanceof RefactoringChangeDescriptor) {
            RefactoringChangeDescriptor rcd = (RefactoringChangeDescriptor) descriptor;
            RefactoringDescriptor refactoringDescriptor = rcd.getRefactoringDescriptor();
            if (refactoringDescriptor instanceof JavaRefactoringDescriptor) {
                JavaRefactoringDescriptor jrd = (JavaRefactoringDescriptor) refactoringDescriptor;
                RefactoringStatus validation = jrd.validateDescriptor();
                if (!validation.isOK())
                    return validation;
                RefactoringStatus refactoringStatus = new RefactoringStatus();
                Class expected = jrd.getClass();
                RefactoringContribution contribution = RefactoringCore.getRefactoringContribution(jrd.getID());
                jrd = (JavaRefactoringDescriptor) contribution.createDescriptor(jrd.getID(), jrd.getProject(), jrd.getDescription(), jrd.getComment(), contribution.retrieveArgumentMap(jrd), jrd.getFlags());
                assertEquals(expected, jrd.getClass());
                ref = jrd.createRefactoring(refactoringStatus);
                if (!refactoringStatus.isOK())
                    return refactoringStatus;
                TestRenameParticipantSingle.reset();
                TestCreateParticipantSingle.reset();
                TestMoveParticipantSingle.reset();
                TestDeleteParticipantSingle.reset();
            }
        }
    }
    final CreateChangeOperation create = new CreateChangeOperation(new CheckConditionsOperation(ref, CheckConditionsOperation.ALL_CONDITIONS), RefactoringStatus.FATAL);
    final PerformChangeOperation perform = new PerformChangeOperation(create);
    perform.setUndoManager(undoManager, ref.getName());
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    if (fIsPreDeltaTest) {
        IResourceChangeListener listener = new IResourceChangeListener() {

            public void resourceChanged(IResourceChangeEvent event) {
                if (create.getConditionCheckingStatus().isOK() && perform.changeExecuted()) {
                    TestModelProvider.assertTrue(event.getDelta());
                }
            }
        };
        try {
            TestModelProvider.clearDelta();
            workspace.checkpoint(false);
            workspace.addResourceChangeListener(listener);
            executePerformOperation(perform, workspace);
        } finally {
            workspace.removeResourceChangeListener(listener);
        }
    } else {
        executePerformOperation(perform, workspace);
    }
    RefactoringStatus status = create.getConditionCheckingStatus();
    if (!status.isOK())
        return status;
    assertTrue("Change wasn't executed", perform.changeExecuted());
    Change undo = perform.getUndoChange();
    if (providesUndo) {
        assertNotNull("Undo doesn't exist", undo);
        assertTrue("Undo manager is empty", undoManager.anythingToUndo());
    } else {
        assertNull("Undo manager contains undo but shouldn't", undo);
    }
    return null;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IUndoManager(org.eclipse.ltk.core.refactoring.IUndoManager) RefactoringContribution(org.eclipse.ltk.core.refactoring.RefactoringContribution) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) Change(org.eclipse.ltk.core.refactoring.Change) IResourceChangeListener(org.eclipse.core.resources.IResourceChangeListener) ChangeDescriptor(org.eclipse.ltk.core.refactoring.ChangeDescriptor) RefactoringChangeDescriptor(org.eclipse.ltk.core.refactoring.RefactoringChangeDescriptor) RefactoringChangeDescriptor(org.eclipse.ltk.core.refactoring.RefactoringChangeDescriptor) JavaRefactoringDescriptor(org.eclipse.jdt.core.refactoring.descriptors.JavaRefactoringDescriptor) RefactoringDescriptor(org.eclipse.ltk.core.refactoring.RefactoringDescriptor) CreateChangeOperation(org.eclipse.ltk.core.refactoring.CreateChangeOperation) PerformChangeOperation(org.eclipse.ltk.core.refactoring.PerformChangeOperation) IResourceChangeEvent(org.eclipse.core.resources.IResourceChangeEvent) IWorkspace(org.eclipse.core.resources.IWorkspace) CheckConditionsOperation(org.eclipse.ltk.core.refactoring.CheckConditionsOperation) JavaRefactoringDescriptor(org.eclipse.jdt.core.refactoring.descriptors.JavaRefactoringDescriptor)

Example 43 with RefactoringStatus

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

the class Checks method checkMethodInType.

//---- New method name checking -------------------------------------------------------------
/**
	 * Checks if the new method is already used in the given type.
	 * @param type
	 * @param methodName
	 * @param parameters
	 * @return the status
	 */
public static RefactoringStatus checkMethodInType(ITypeBinding type, String methodName, ITypeBinding[] parameters) {
    RefactoringStatus result = new RefactoringStatus();
    IMethodBinding method = org.eclipse.jdt.internal.corext.dom.Bindings.findMethodInType(type, methodName, parameters);
    if (method != null) {
        if (method.isConstructor()) {
            result.addWarning(Messages.format(RefactoringCoreMessages.Checks_methodName_constructor, new Object[] { BasicElementLabels.getJavaElementName(type.getName()) }));
        } else {
            result.addError(Messages.format(RefactoringCoreMessages.Checks_methodName_exists, new Object[] { BasicElementLabels.getJavaElementName(methodName), BasicElementLabels.getJavaElementName(type.getName()) }), JavaStatusContext.create(method));
        }
    }
    return result;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus)

Example 44 with RefactoringStatus

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

the class Checks method checkCompilationUnitNewName.

/**
	 * Returns OK status if the new name is OK, i.e. when no file with that name exists.
	 * The name of the given CU is not OK.
	 * 
	 * @param cu CU to rename
	 * @param newBareName the new name of the CU (without extension)
	 * @return the status: FATAL if the CU already exists, OK if OK
	 */
public static RefactoringStatus checkCompilationUnitNewName(ICompilationUnit cu, String newBareName) {
    String newCUName = JavaModelUtil.getRenamedCUName(cu, newBareName);
    IPath renamedResourcePath = cu.getParent().getPath().append(newCUName);
    if (resourceExists(renamedResourcePath))
        return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.Checks_cu_name_used, BasicElementLabels.getResourceName(newCUName)));
    else
        return new RefactoringStatus();
}
Also used : IPath(org.eclipse.core.runtime.IPath) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus)

Example 45 with RefactoringStatus

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

the class Checks method checkForMainAndNativeMethods.

public static RefactoringStatus checkForMainAndNativeMethods(IType type) throws JavaModelException {
    RefactoringStatus result = new RefactoringStatus();
    result.merge(checkForMainAndNativeMethods(type.getMethods()));
    result.merge(checkForMainAndNativeMethods(type.getTypes()));
    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