Search in sources :

Example 1 with MoveInstanceMethodProcessor

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

the class MoveHandler method getInstanceMethodDestinations.

private static MoveDestinationsResponse getInstanceMethodDestinations(CodeActionParams params) {
    final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(params.getTextDocument().getUri());
    if (unit == null) {
        return new MoveDestinationsResponse("Cannot find the compilation unit associated with " + params.getTextDocument().getUri());
    }
    MethodDeclaration methodDeclaration = getSelectedMethodDeclaration(unit, params);
    if (methodDeclaration == null) {
        return new MoveDestinationsResponse("The selected element is not a method.");
    }
    IMethodBinding methodBinding = methodDeclaration.resolveBinding();
    if (methodBinding == null || !(methodBinding.getJavaElement() instanceof IMethod)) {
        return new MoveDestinationsResponse("The selected element is not a method.");
    }
    IMethod method = (IMethod) methodBinding.getJavaElement();
    MoveInstanceMethodProcessor processor = new MoveInstanceMethodProcessor(method, PreferenceManager.getCodeGenerationSettings(unit));
    Refactoring refactoring = new MoveRefactoring(processor);
    CheckConditionsOperation check = new CheckConditionsOperation(refactoring, CheckConditionsOperation.INITIAL_CONDITONS);
    try {
        check.run(new NullProgressMonitor());
        if (check.getStatus().hasFatalError()) {
            return new MoveDestinationsResponse(check.getStatus().getMessageMatchingSeverity(RefactoringStatus.FATAL));
        }
        IVariableBinding[] possibleTargets = processor.getPossibleTargets();
        LspVariableBinding[] targets = Stream.of(possibleTargets).map(target -> new LspVariableBinding(target)).toArray(LspVariableBinding[]::new);
        return new MoveDestinationsResponse(targets);
    } catch (CoreException e) {
        JavaLanguageServerPlugin.log(e);
    }
    return new MoveDestinationsResponse("Cannot find any target to move the method to.");
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) RefactoringAvailabilityTesterCore(org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTesterCore) Arrays(java.util.Arrays) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) CreateChangeOperation(org.eclipse.ltk.core.refactoring.CreateChangeOperation) ProjectUtils(org.eclipse.jdt.ls.core.internal.ProjectUtils) CoreException(org.eclipse.core.runtime.CoreException) StringUtils(org.apache.commons.lang3.StringUtils) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) Refactoring(org.eclipse.ltk.core.refactoring.Refactoring) JavaMoveProcessor(org.eclipse.jdt.ls.core.internal.corext.refactoring.reorg.JavaMoveProcessor) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) DiagnosticsHelper(org.eclipse.jdt.ls.core.internal.corrections.DiagnosticsHelper) IPath(org.eclipse.core.runtime.IPath) Gson(com.google.gson.Gson) Change(org.eclipse.ltk.core.refactoring.Change) URI(java.net.URI) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) ASTNode(org.eclipse.jdt.core.dom.ASTNode) JDTUtils(org.eclipse.jdt.ls.core.internal.JDTUtils) IJavaProject(org.eclipse.jdt.core.IJavaProject) Collection(java.util.Collection) Set(java.util.Set) IReorgDestination(org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgDestination) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) Collectors(java.util.stream.Collectors) IConfirmQuery(org.eclipse.jdt.internal.corext.refactoring.reorg.IConfirmQuery) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Objects(java.util.Objects) IType(org.eclipse.jdt.core.IType) List(java.util.List) Stream(java.util.stream.Stream) ReorgPolicyFactory(org.eclipse.jdt.ls.core.internal.corext.refactoring.reorg.ReorgPolicyFactory) InnovationContext(org.eclipse.jdt.ls.core.internal.corrections.InnovationContext) Optional(java.util.Optional) ChangeUtil(org.eclipse.jdt.ls.core.internal.ChangeUtil) MoveInstanceMethodProcessor(org.eclipse.jdt.internal.corext.refactoring.structure.MoveInstanceMethodProcessor) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ResourcesPlugin(org.eclipse.core.resources.ResourcesPlugin) RefactorWorkspaceEdit(org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit) JavaModelException(org.eclipse.jdt.core.JavaModelException) SubMonitor(org.eclipse.core.runtime.SubMonitor) CodeGenerationSettings(org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings) MoveStaticMembersProcessor(org.eclipse.jdt.internal.corext.refactoring.structure.MoveStaticMembersProcessor) IMember(org.eclipse.jdt.core.IMember) CheckConditionsOperation(org.eclipse.ltk.core.refactoring.CheckConditionsOperation) MoveRefactoring(org.eclipse.ltk.core.refactoring.participants.MoveRefactoring) ArrayList(java.util.ArrayList) SymbolInformation(org.eclipse.lsp4j.SymbolInformation) IContainer(org.eclipse.core.resources.IContainer) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ProjectsManager(org.eclipse.jdt.ls.core.internal.managers.ProjectsManager) IProject(org.eclipse.core.resources.IProject) JdtFlags(org.eclipse.jdt.internal.corext.util.JdtFlags) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) ReorgDestinationFactory(org.eclipse.jdt.ls.core.internal.corext.refactoring.reorg.ReorgDestinationFactory) LinkedHashSet(java.util.LinkedHashSet) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) ResourceUtils(org.eclipse.jdt.ls.core.internal.ResourceUtils) JSONUtility(org.eclipse.jdt.ls.core.internal.JSONUtility) JavaCore(org.eclipse.jdt.core.JavaCore) LspVariableBinding(org.eclipse.jdt.ls.core.internal.handlers.JdtDomModels.LspVariableBinding) JavaLanguageServerPlugin(org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin) PreferenceManager(org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager) ReorgUtils(org.eclipse.jdt.ls.core.internal.corext.refactoring.reorg.ReorgUtils) IJavaElement(org.eclipse.jdt.core.IJavaElement) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IReorgQueries(org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgQueries) IResource(org.eclipse.core.resources.IResource) IMethod(org.eclipse.jdt.core.IMethod) MoveInnerToTopRefactoring(org.eclipse.jdt.internal.corext.refactoring.structure.MoveInnerToTopRefactoring) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) Comparator(java.util.Comparator) IMovePolicy(org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgPolicy.IMovePolicy) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) LspVariableBinding(org.eclipse.jdt.ls.core.internal.handlers.JdtDomModels.LspVariableBinding) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) MoveRefactoring(org.eclipse.ltk.core.refactoring.participants.MoveRefactoring) CoreException(org.eclipse.core.runtime.CoreException) CheckConditionsOperation(org.eclipse.ltk.core.refactoring.CheckConditionsOperation) IMethod(org.eclipse.jdt.core.IMethod) Refactoring(org.eclipse.ltk.core.refactoring.Refactoring) MoveRefactoring(org.eclipse.ltk.core.refactoring.participants.MoveRefactoring) MoveInnerToTopRefactoring(org.eclipse.jdt.internal.corext.refactoring.structure.MoveInnerToTopRefactoring) MoveInstanceMethodProcessor(org.eclipse.jdt.internal.corext.refactoring.structure.MoveInstanceMethodProcessor)

Example 2 with MoveInstanceMethodProcessor

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

the class MoveHandler method moveInstanceMethod.

private static RefactorWorkspaceEdit moveInstanceMethod(CodeActionParams params, LspVariableBinding destination, IProgressMonitor monitor) {
    final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(params.getTextDocument().getUri());
    if (unit == null) {
        return new RefactorWorkspaceEdit("Failed to move instance method because cannot find the compilation unit associated with " + params.getTextDocument().getUri());
    }
    MethodDeclaration methodDeclaration = getSelectedMethodDeclaration(unit, params);
    if (methodDeclaration == null || destination == null) {
        return new RefactorWorkspaceEdit("Failed to move instance method because no method is selected or no destination is specified.");
    }
    IMethodBinding methodBinding = methodDeclaration.resolveBinding();
    if (methodBinding == null || !(methodBinding.getJavaElement() instanceof IMethod)) {
        return new RefactorWorkspaceEdit("Failed to move instance method because the selected element is not a method.");
    }
    SubMonitor subMonitor = SubMonitor.convert(monitor, "Moving instance method...", 100);
    IMethod method = (IMethod) methodBinding.getJavaElement();
    MoveInstanceMethodProcessor processor = new MoveInstanceMethodProcessor(method, PreferenceManager.getCodeGenerationSettings(unit));
    Refactoring refactoring = new MoveRefactoring(processor);
    CheckConditionsOperation check = new CheckConditionsOperation(refactoring, CheckConditionsOperation.INITIAL_CONDITONS);
    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());
            return new RefactorWorkspaceEdit("Failed to move instance method. Reason: " + check.getStatus().toString());
        }
        IVariableBinding[] possibleTargets = processor.getPossibleTargets();
        Optional<IVariableBinding> target = Stream.of(possibleTargets).filter(possibleTarget -> Objects.equals(possibleTarget.getKey(), destination.bindingKey)).findFirst();
        if (target.isPresent()) {
            processor.setTarget(target.get());
            processor.setDeprecateDelegates(false);
            processor.setInlineDelegator(true);
            processor.setRemoveDelegator(true);
            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 instance method. Reason: " + check.getStatus().toString());
            }
            Change change = processor.createChange(subMonitor.split(20));
            return new RefactorWorkspaceEdit(ChangeUtil.convertToWorkspaceEdit(change));
        } else {
            return new RefactorWorkspaceEdit("Failed to move instance method because cannot find the target " + destination.name);
        }
    } catch (CoreException e) {
        JavaLanguageServerPlugin.log(e);
        return new RefactorWorkspaceEdit("Failed to move instance method because of " + e.toString());
    } finally {
        subMonitor.done();
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) RefactoringAvailabilityTesterCore(org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTesterCore) Arrays(java.util.Arrays) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) CreateChangeOperation(org.eclipse.ltk.core.refactoring.CreateChangeOperation) ProjectUtils(org.eclipse.jdt.ls.core.internal.ProjectUtils) CoreException(org.eclipse.core.runtime.CoreException) StringUtils(org.apache.commons.lang3.StringUtils) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) Refactoring(org.eclipse.ltk.core.refactoring.Refactoring) JavaMoveProcessor(org.eclipse.jdt.ls.core.internal.corext.refactoring.reorg.JavaMoveProcessor) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) DiagnosticsHelper(org.eclipse.jdt.ls.core.internal.corrections.DiagnosticsHelper) IPath(org.eclipse.core.runtime.IPath) Gson(com.google.gson.Gson) Change(org.eclipse.ltk.core.refactoring.Change) URI(java.net.URI) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) ASTNode(org.eclipse.jdt.core.dom.ASTNode) JDTUtils(org.eclipse.jdt.ls.core.internal.JDTUtils) IJavaProject(org.eclipse.jdt.core.IJavaProject) Collection(java.util.Collection) Set(java.util.Set) IReorgDestination(org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgDestination) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) Collectors(java.util.stream.Collectors) IConfirmQuery(org.eclipse.jdt.internal.corext.refactoring.reorg.IConfirmQuery) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Objects(java.util.Objects) IType(org.eclipse.jdt.core.IType) List(java.util.List) Stream(java.util.stream.Stream) ReorgPolicyFactory(org.eclipse.jdt.ls.core.internal.corext.refactoring.reorg.ReorgPolicyFactory) InnovationContext(org.eclipse.jdt.ls.core.internal.corrections.InnovationContext) Optional(java.util.Optional) ChangeUtil(org.eclipse.jdt.ls.core.internal.ChangeUtil) MoveInstanceMethodProcessor(org.eclipse.jdt.internal.corext.refactoring.structure.MoveInstanceMethodProcessor) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ResourcesPlugin(org.eclipse.core.resources.ResourcesPlugin) RefactorWorkspaceEdit(org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit) JavaModelException(org.eclipse.jdt.core.JavaModelException) SubMonitor(org.eclipse.core.runtime.SubMonitor) CodeGenerationSettings(org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings) MoveStaticMembersProcessor(org.eclipse.jdt.internal.corext.refactoring.structure.MoveStaticMembersProcessor) IMember(org.eclipse.jdt.core.IMember) CheckConditionsOperation(org.eclipse.ltk.core.refactoring.CheckConditionsOperation) MoveRefactoring(org.eclipse.ltk.core.refactoring.participants.MoveRefactoring) ArrayList(java.util.ArrayList) SymbolInformation(org.eclipse.lsp4j.SymbolInformation) IContainer(org.eclipse.core.resources.IContainer) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) ProjectsManager(org.eclipse.jdt.ls.core.internal.managers.ProjectsManager) IProject(org.eclipse.core.resources.IProject) JdtFlags(org.eclipse.jdt.internal.corext.util.JdtFlags) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) ReorgDestinationFactory(org.eclipse.jdt.ls.core.internal.corext.refactoring.reorg.ReorgDestinationFactory) LinkedHashSet(java.util.LinkedHashSet) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) ResourceUtils(org.eclipse.jdt.ls.core.internal.ResourceUtils) JSONUtility(org.eclipse.jdt.ls.core.internal.JSONUtility) JavaCore(org.eclipse.jdt.core.JavaCore) LspVariableBinding(org.eclipse.jdt.ls.core.internal.handlers.JdtDomModels.LspVariableBinding) JavaLanguageServerPlugin(org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin) PreferenceManager(org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager) ReorgUtils(org.eclipse.jdt.ls.core.internal.corext.refactoring.reorg.ReorgUtils) IJavaElement(org.eclipse.jdt.core.IJavaElement) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IReorgQueries(org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgQueries) IResource(org.eclipse.core.resources.IResource) IMethod(org.eclipse.jdt.core.IMethod) MoveInnerToTopRefactoring(org.eclipse.jdt.internal.corext.refactoring.structure.MoveInnerToTopRefactoring) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) Comparator(java.util.Comparator) IMovePolicy(org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgPolicy.IMovePolicy) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SubMonitor(org.eclipse.core.runtime.SubMonitor) Change(org.eclipse.ltk.core.refactoring.Change) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) MoveRefactoring(org.eclipse.ltk.core.refactoring.participants.MoveRefactoring) CoreException(org.eclipse.core.runtime.CoreException) CheckConditionsOperation(org.eclipse.ltk.core.refactoring.CheckConditionsOperation) RefactorWorkspaceEdit(org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit) IMethod(org.eclipse.jdt.core.IMethod) Refactoring(org.eclipse.ltk.core.refactoring.Refactoring) MoveRefactoring(org.eclipse.ltk.core.refactoring.participants.MoveRefactoring) MoveInnerToTopRefactoring(org.eclipse.jdt.internal.corext.refactoring.structure.MoveInnerToTopRefactoring) MoveInstanceMethodProcessor(org.eclipse.jdt.internal.corext.refactoring.structure.MoveInstanceMethodProcessor)

Aggregations

Gson (com.google.gson.Gson)2 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 Comparator (java.util.Comparator)2 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2 Objects (java.util.Objects)2 Optional (java.util.Optional)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 Stream (java.util.stream.Stream)2 StringUtils (org.apache.commons.lang3.StringUtils)2 IContainer (org.eclipse.core.resources.IContainer)2 IProject (org.eclipse.core.resources.IProject)2 IResource (org.eclipse.core.resources.IResource)2 ResourcesPlugin (org.eclipse.core.resources.ResourcesPlugin)2 CoreException (org.eclipse.core.runtime.CoreException)2 IPath (org.eclipse.core.runtime.IPath)2