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.");
}
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();
}
}
Aggregations