Search in sources :

Example 1 with MoveStaticMembersProcessor

use of org.eclipse.jdt.internal.corext.refactoring.structure.MoveStaticMembersProcessor in project eclipse.jdt.ls by eclipse.

the class MoveHandler method moveStaticMember.

private static RefactorWorkspaceEdit moveStaticMember(IMember[] members, String destinationTypeName, IProgressMonitor monitor) {
    if (members.length == 0 || destinationTypeName == null) {
        return new RefactorWorkspaceEdit("Failed to move static member because no members are selected or no destination is specified.");
    }
    CodeGenerationSettings settings = members[0].getTypeRoot() instanceof ICompilationUnit ? PreferenceManager.getCodeGenerationSettings((ICompilationUnit) members[0].getTypeRoot()) : PreferenceManager.getCodeGenerationSettings(members[0].getJavaProject().getProject());
    MoveStaticMembersProcessor processor = new MoveStaticMembersProcessor(members, settings);
    Refactoring refactoring = new MoveRefactoring(processor);
    CheckConditionsOperation check = new CheckConditionsOperation(refactoring, CheckConditionsOperation.INITIAL_CONDITONS);
    SubMonitor subMonitor = SubMonitor.convert(monitor, "Moving static members...", 100);
    try {
        check.run(subMonitor.split(20));
        if (check.getStatus().getSeverity() >= RefactoringStatus.FATAL) {
            JavaLanguageServerPlugin.logError("Failed to execute the 'move' refactoring.");
            JavaLanguageServerPlugin.logError(check.getStatus().toString());
        }
        processor.setDestinationTypeFullyQualifiedName(destinationTypeName);
        processor.setDeprecateDelegates(false);
        check = new CheckConditionsOperation(refactoring, CheckConditionsOperation.FINAL_CONDITIONS);
        check.run(subMonitor.split(60));
        if (check.getStatus().getSeverity() >= RefactoringStatus.FATAL) {
            JavaLanguageServerPlugin.logError("Failed to execute the 'move' refactoring.");
            JavaLanguageServerPlugin.logError(check.getStatus().toString());
            return new RefactorWorkspaceEdit("Failed to move static member. Reason: " + check.getStatus().toString());
        }
        Change change = processor.createChange(subMonitor.split(20));
        return new RefactorWorkspaceEdit(ChangeUtil.convertToWorkspaceEdit(change));
    } catch (CoreException e) {
        JavaLanguageServerPlugin.log(e);
        return new RefactorWorkspaceEdit("Failed to move static member because of " + e.toString());
    } finally {
        subMonitor.done();
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CoreException(org.eclipse.core.runtime.CoreException) CodeGenerationSettings(org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings) CheckConditionsOperation(org.eclipse.ltk.core.refactoring.CheckConditionsOperation) SubMonitor(org.eclipse.core.runtime.SubMonitor) RefactorWorkspaceEdit(org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit) Change(org.eclipse.ltk.core.refactoring.Change) MoveStaticMembersProcessor(org.eclipse.jdt.internal.corext.refactoring.structure.MoveStaticMembersProcessor) Refactoring(org.eclipse.ltk.core.refactoring.Refactoring) MoveRefactoring(org.eclipse.ltk.core.refactoring.participants.MoveRefactoring) MoveInnerToTopRefactoring(org.eclipse.jdt.internal.corext.refactoring.structure.MoveInnerToTopRefactoring) MoveRefactoring(org.eclipse.ltk.core.refactoring.participants.MoveRefactoring)

Aggregations

CoreException (org.eclipse.core.runtime.CoreException)1 SubMonitor (org.eclipse.core.runtime.SubMonitor)1 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 CodeGenerationSettings (org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings)1 MoveInnerToTopRefactoring (org.eclipse.jdt.internal.corext.refactoring.structure.MoveInnerToTopRefactoring)1 MoveStaticMembersProcessor (org.eclipse.jdt.internal.corext.refactoring.structure.MoveStaticMembersProcessor)1 RefactorWorkspaceEdit (org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit)1 Change (org.eclipse.ltk.core.refactoring.Change)1 CheckConditionsOperation (org.eclipse.ltk.core.refactoring.CheckConditionsOperation)1 Refactoring (org.eclipse.ltk.core.refactoring.Refactoring)1 MoveRefactoring (org.eclipse.ltk.core.refactoring.participants.MoveRefactoring)1