use of org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit in project eclipse.jdt.ls by eclipse.
the class MoveHandler method moveStaticMember.
private static RefactorWorkspaceEdit moveStaticMember(CodeActionParams params, String destinationTypeName, IProgressMonitor monitor) {
final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(params.getTextDocument().getUri());
if (unit == null) {
return new RefactorWorkspaceEdit("Failed to move static member because cannot find the compilation unit associated with " + params.getTextDocument().getUri());
}
BodyDeclaration bodyDeclaration = getSelectedMemberDeclaration(unit, params);
List<IJavaElement> elements = new ArrayList<>();
if (bodyDeclaration instanceof MethodDeclaration) {
elements.add(((MethodDeclaration) bodyDeclaration).resolveBinding().getJavaElement());
} else if (bodyDeclaration instanceof FieldDeclaration) {
for (Object fragment : ((FieldDeclaration) bodyDeclaration).fragments()) {
elements.add(((VariableDeclarationFragment) fragment).resolveBinding().getJavaElement());
}
} else if (bodyDeclaration instanceof AbstractTypeDeclaration) {
elements.add(((AbstractTypeDeclaration) bodyDeclaration).resolveBinding().getJavaElement());
}
IMember[] members = elements.stream().filter(element -> element instanceof IMember).map(element -> (IMember) element).toArray(IMember[]::new);
return moveStaticMember(members, destinationTypeName, monitor);
}
use of org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit in project eclipse.jdt.ls by eclipse.
the class GetRefactorEditHandlerTest method testExtractVariable.
@Test
public void testExtractVariable() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("class A{\n");
buf.append(" void m(int i){\n");
buf.append(" int x= /*]*/0/*[*/;\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
Range selection = getRange(cu, null);
CodeActionParams params = CodeActionUtil.constructCodeActionParams(cu, selection);
GetRefactorEditParams editParams = new GetRefactorEditParams(RefactorProposalUtility.EXTRACT_VARIABLE_COMMAND, params);
RefactorWorkspaceEdit refactorEdit = GetRefactorEditHandler.getEditsForRefactor(editParams);
Assert.assertNotNull(refactorEdit);
Assert.assertNotNull(refactorEdit.edit);
String actual = AbstractQuickFixTest.evaluateWorkspaceEdit(refactorEdit.edit);
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("class A{\n");
buf.append(" void m(int i){\n");
buf.append(" int j = 0;\n");
buf.append(" int x= /*]*/j/*[*/;\n");
buf.append(" }\n");
buf.append("}\n");
AbstractSourceTestCase.compareSource(buf.toString(), actual);
Assert.assertNotNull(refactorEdit.command);
Assert.assertEquals(GetRefactorEditHandler.RENAME_COMMAND, refactorEdit.command.getCommand());
Assert.assertNotNull(refactorEdit.command.getArguments());
Assert.assertEquals(1, refactorEdit.command.getArguments().size());
}
use of org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit in project eclipse.jdt.ls by eclipse.
the class GetRefactorEditHandlerTest method testExtractMethod.
@Test
public void testExtractMethod() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("\n");
buf.append("public class E {\n");
buf.append(" public int foo(boolean b1, boolean b2) {\n");
buf.append(" int n = 0;\n");
buf.append(" int i = 0;\n");
buf.append(" /*[*/\n");
buf.append(" if (b1)\n");
buf.append(" i = 1;\n");
buf.append(" if (b2)\n");
buf.append(" n = n + i;\n");
buf.append(" /*]*/\n");
buf.append(" return n;\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
Range selection = getRange(cu, null);
CodeActionParams params = CodeActionUtil.constructCodeActionParams(cu, selection);
GetRefactorEditParams editParams = new GetRefactorEditParams(RefactorProposalUtility.EXTRACT_METHOD_COMMAND, params);
RefactorWorkspaceEdit refactorEdit = GetRefactorEditHandler.getEditsForRefactor(editParams);
Assert.assertNotNull(refactorEdit);
Assert.assertNotNull(refactorEdit.edit);
String actual = AbstractQuickFixTest.evaluateWorkspaceEdit(refactorEdit.edit);
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("\n");
buf.append("public class E {\n");
buf.append(" public int foo(boolean b1, boolean b2) {\n");
buf.append(" int n = 0;\n");
buf.append(" int i = 0;\n");
buf.append(" n = extracted(b1, b2, n, i);\n");
buf.append(" return n;\n");
buf.append(" }\n");
buf.append("\n");
buf.append(" private int extracted(boolean b1, boolean b2, int n, int i) {\n");
buf.append(" /*[*/\n");
buf.append(" if (b1)\n");
buf.append(" i = 1;\n");
buf.append(" if (b2)\n");
buf.append(" n = n + i;\n");
buf.append(" /*]*/\n");
buf.append(" return n;\n");
buf.append(" }\n");
buf.append("}\n");
AbstractSourceTestCase.compareSource(buf.toString(), actual);
Assert.assertNotNull(refactorEdit.command);
Assert.assertEquals(GetRefactorEditHandler.RENAME_COMMAND, refactorEdit.command.getCommand());
Assert.assertNotNull(refactorEdit.command.getArguments());
Assert.assertEquals(1, refactorEdit.command.getArguments().size());
}
use of org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit in project eclipse.jdt.ls by eclipse.
the class GetRefactorEditHandlerTest method testExtractConstant.
@Test
public void testExtractConstant() throws Exception {
IPackageFragment pack1 = fSourceFolder.createPackageFragment("test1", false, null);
StringBuilder buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("class A{\n");
buf.append(" void m(int i){\n");
buf.append(" int x= /*]*/0/*[*/;\n");
buf.append(" }\n");
buf.append("}\n");
ICompilationUnit cu = pack1.createCompilationUnit("E.java", buf.toString(), false, null);
Range selection = getRange(cu, null);
CodeActionParams params = CodeActionUtil.constructCodeActionParams(cu, selection);
GetRefactorEditParams editParams = new GetRefactorEditParams(RefactorProposalUtility.EXTRACT_CONSTANT_COMMAND, params);
RefactorWorkspaceEdit refactorEdit = GetRefactorEditHandler.getEditsForRefactor(editParams);
Assert.assertNotNull(refactorEdit);
Assert.assertNotNull(refactorEdit.edit);
String actual = AbstractQuickFixTest.evaluateWorkspaceEdit(refactorEdit.edit);
buf = new StringBuilder();
buf.append("package test1;\n");
buf.append("class A{\n");
buf.append(" private static final int _0 = /*]*/0/*[*/;\n");
buf.append("\n");
buf.append(" void m(int i){\n");
buf.append(" int x= _0;\n");
buf.append(" }\n");
buf.append("}\n");
AbstractSourceTestCase.compareSource(buf.toString(), actual);
Assert.assertNotNull(refactorEdit.command);
Assert.assertEquals(GetRefactorEditHandler.RENAME_COMMAND, refactorEdit.command.getCommand());
Assert.assertNotNull(refactorEdit.command.getArguments());
Assert.assertEquals(1, refactorEdit.command.getArguments().size());
}
use of org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit 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