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(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();
}
}
use of org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit in project eclipse.jdt.ls by eclipse.
the class MoveHandler method moveTypeToNewFile.
private static RefactorWorkspaceEdit moveTypeToNewFile(CodeActionParams params, IProgressMonitor monitor) {
final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(params.getTextDocument().getUri());
if (unit == null) {
return new RefactorWorkspaceEdit("Failed to move type to new file because cannot find the compilation unit associated with " + params.getTextDocument().getUri());
}
IType type = getSelectedType(unit, params);
if (type == null) {
return new RefactorWorkspaceEdit("Failed to move type to new file because no type is selected.");
}
SubMonitor subMonitor = SubMonitor.convert(monitor, "Moving type to new file...", 100);
try {
MoveInnerToTopRefactoring refactoring = new MoveInnerToTopRefactoring(type, PreferenceManager.getCodeGenerationSettings(unit));
CheckConditionsOperation check = new CheckConditionsOperation(refactoring, CheckConditionsOperation.ALL_CONDITIONS);
check.run(subMonitor.split(50));
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 type to new file. Reason: " + check.getStatus().toString());
}
Change change = refactoring.createChange(subMonitor.split(50));
return new RefactorWorkspaceEdit(ChangeUtil.convertToWorkspaceEdit(change));
} catch (CoreException e) {
JavaLanguageServerPlugin.log(e);
return new RefactorWorkspaceEdit("Failed to move type to new file because of " + e.toString());
} catch (OperationCanceledException e) {
return null;
}
}
use of org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit in project eclipse.jdt.ls by eclipse.
the class MoveHandler method moveTypeToClass.
private static RefactorWorkspaceEdit moveTypeToClass(CodeActionParams params, String destinationTypeName, IProgressMonitor monitor) {
final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(params.getTextDocument().getUri());
if (unit == null) {
return new RefactorWorkspaceEdit("Failed to move type to another class because cannot find the compilation unit associated with " + params.getTextDocument().getUri());
}
IType type = getSelectedType(unit, params);
if (type == null) {
return new RefactorWorkspaceEdit("Failed to move type to another class because no type is selected.");
}
try {
if (RefactoringAvailabilityTesterCore.isMoveStaticAvailable(type)) {
return moveStaticMember(new IMember[] { type }, destinationTypeName, monitor);
}
return new RefactorWorkspaceEdit("Moving non-static type to another class is not supported.");
} catch (JavaModelException e) {
return new RefactorWorkspaceEdit("Failed to move type to another class. Reason: " + e.toString());
}
}
use of org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit in project eclipse.jdt.ls by eclipse.
the class MoveHandler method move.
public static RefactorWorkspaceEdit move(MoveParams moveParams, IProgressMonitor monitor) {
if (moveParams == null) {
return new RefactorWorkspaceEdit("moveParams should not be empty.");
}
try {
if ("moveResource".equalsIgnoreCase(moveParams.moveKind)) {
String targetUri = null;
if (moveParams.destination instanceof String) {
targetUri = (String) moveParams.destination;
} else {
String json = (moveParams.destination == null ? null : new Gson().toJson(moveParams.destination));
PackageNode packageNode = JSONUtility.toLsp4jModel(json, PackageNode.class);
if (packageNode == null) {
return new RefactorWorkspaceEdit("Invalid destination object: " + moveParams.destination);
}
targetUri = packageNode.uri;
}
return moveCU(moveParams.sourceUris, targetUri, moveParams.updateReferences, monitor);
} else if ("moveInstanceMethod".equalsIgnoreCase(moveParams.moveKind)) {
String json = (moveParams.destination == null ? null : new Gson().toJson(moveParams.destination));
LspVariableBinding variableBinding = JSONUtility.toLsp4jModel(json, LspVariableBinding.class);
if (variableBinding == null) {
return new RefactorWorkspaceEdit("Invalid destination object: " + moveParams.destination);
}
return moveInstanceMethod(moveParams.params, variableBinding, monitor);
} else if ("moveStaticMember".equalsIgnoreCase(moveParams.moveKind)) {
String typeName = resolveTargetTypeName(moveParams.destination);
return moveStaticMember(moveParams.params, typeName, monitor);
} else if ("moveTypeToNewFile".equalsIgnoreCase(moveParams.moveKind)) {
return moveTypeToNewFile(moveParams.params, monitor);
} else if ("moveTypeToClass".equalsIgnoreCase(moveParams.moveKind)) {
String typeName = resolveTargetTypeName(moveParams.destination);
return moveTypeToClass(moveParams.params, typeName, monitor);
}
} catch (IllegalArgumentException e) {
return new RefactorWorkspaceEdit(e.getMessage());
}
return new RefactorWorkspaceEdit("Unsupported move operation.");
}
use of org.eclipse.jdt.ls.core.internal.handlers.GetRefactorEditHandler.RefactorWorkspaceEdit in project eclipse.jdt.ls by eclipse.
the class MoveHandlerTest method testMoveInstanceMethod.
@Test
public void testMoveInstanceMethod() throws Exception {
IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
// @formatter:off
ICompilationUnit cuSecond = pack1.createCompilationUnit("Second.java", "package test1;\n" + "\n" + "public class Second {\n" + " public void foo() {\n" + " }\n" + "}", false, null);
// @formatter:on
// @formatter:off
pack1.createCompilationUnit("Third.java", "package test1;\n" + "\n" + "public class Third {\n" + " public void bar() {\n" + " }\n" + "}", false, null);
// @formatter:on
// @formatter:off
ICompilationUnit cu = pack1.createCompilationUnit("E.java", "package test1;\n" + "\n" + "public class E {\n" + " Second s;\n" + " String name;\n" + " int id;\n" + " public void print(Third t) {\n" + " System.out.println(name);\n" + " s.foo();\n" + " t.bar();\n" + " }\n" + "\n" + " public void log(Third t) {\n" + " print(t);\n" + " }\n" + "}", false, null);
// @formatter:on
CodeActionParams params = CodeActionUtil.constructCodeActionParams(cu, "s.foo");
MoveParams moveParams = new MoveParams("moveInstanceMethod", new String[] { JDTUtils.toURI(cu) }, params);
MoveDestinationsResponse response = MoveHandler.getMoveDestinations(moveParams);
assertNotNull(response);
assertNull(response.errorMessage);
assertNotNull(response.destinations);
assertEquals(2, response.destinations.length);
RefactorWorkspaceEdit refactorEdit = MoveHandler.move(new MoveParams("moveInstanceMethod", params, response.destinations[1], true), new NullProgressMonitor());
assertNotNull(refactorEdit);
assertNotNull(refactorEdit.edit);
List<Either<TextDocumentEdit, ResourceOperation>> changes = refactorEdit.edit.getDocumentChanges();
assertEquals(2, changes.size());
// @formatter:off
String expected = "package test1;\n" + "\n" + "public class E {\n" + " Second s;\n" + " String name;\n" + " int id;\n" + " public void log(Third t) {\n" + " s.print(this, t);\n" + " }\n" + "}";
// @formatter:on
TextDocumentEdit textEdit = changes.get(0).getLeft();
assertNotNull(textEdit);
assertEquals(expected, TextEditUtil.apply(cu.getSource(), textEdit.getEdits()));
// @formatter:off
expected = "package test1;\n" + "\n" + "public class Second {\n" + " public void foo() {\n" + " }\n" + "\n" + " public void print(E e, Third t) {\n" + " System.out.println(e.name);\n" + " foo();\n" + " t.bar();\n" + " }\n" + "}";
// @formatter:on
textEdit = changes.get(1).getLeft();
assertNotNull(textEdit);
assertEquals(expected, TextEditUtil.apply(cuSecond.getSource(), textEdit.getEdits()));
}
Aggregations