use of org.eclipse.jdt.ls.core.internal.handlers.GenerateConstructorsHandler.CheckConstructorsResponse in project eclipse.jdt.ls by eclipse.
the class SourceAssistProcessor method getGenerateConstructorsAction.
private Optional<Either<Command, CodeAction>> getGenerateConstructorsAction(CodeActionParams params, IInvocationContext context, IType type, String kind, IProgressMonitor monitor) {
try {
if (type == null || type.isAnnotation() || type.isInterface() || type.isAnonymous() || type.getCompilationUnit() == null) {
return Optional.empty();
}
} catch (JavaModelException e) {
return Optional.empty();
}
if (preferenceManager.getClientPreferences().isGenerateConstructorsPromptSupported()) {
CheckConstructorsResponse status = GenerateConstructorsHandler.checkConstructorStatus(type, monitor);
if (status.constructors.length == 0) {
return Optional.empty();
}
if (status.constructors.length == 1 && status.fields.length == 0) {
CodeActionProposal generateConstructorsProposal = (pm) -> {
TextEdit edit = GenerateConstructorsHandler.generateConstructors(type, status.constructors, status.fields, params.getRange(), pm);
return convertToWorkspaceEdit(type.getCompilationUnit(), edit);
};
return getCodeActionFromProposal(params.getContext(), type.getCompilationUnit(), ActionMessages.GenerateConstructorsAction_label, kind, generateConstructorsProposal);
}
Command command = new Command(ActionMessages.GenerateConstructorsAction_ellipsisLabel, COMMAND_ID_ACTION_GENERATECONSTRUCTORSPROMPT, Collections.singletonList(params));
if (preferenceManager.getClientPreferences().isSupportedCodeActionKind(JavaCodeActionKind.SOURCE_GENERATE_CONSTRUCTORS)) {
CodeAction codeAction = new CodeAction(ActionMessages.GenerateConstructorsAction_ellipsisLabel);
codeAction.setKind(kind);
codeAction.setCommand(command);
codeAction.setDiagnostics(Collections.emptyList());
return Optional.of(Either.forRight(codeAction));
} else {
return Optional.of(Either.forLeft(command));
}
}
return Optional.empty();
}
use of org.eclipse.jdt.ls.core.internal.handlers.GenerateConstructorsHandler.CheckConstructorsResponse in project eclipse.jdt.ls by eclipse.
the class GenerateConstructorsHandlerTest method testCheckConstructorStatus.
@Test
public void testCheckConstructorStatus() throws JavaModelException {
// @formatter:off
fPackageP.createCompilationUnit("B.java", "package p;\r\n" + "\r\n" + "public class B {\r\n" + " public B(String name) {\r\n" + " }\r\n" + " public B(String name, int id) {\r\n" + " }\r\n" + " private B() {\r\n" + " }\r\n" + "}", true, null);
ICompilationUnit unit = fPackageP.createCompilationUnit("C.java", "package p;\r\n" + "\r\n" + "public class C extends B {\r\n" + " private static String logger;\r\n" + " private final String uuid = \"123\";\r\n" + " private final String instance;\r\n" + " private String address;\r\n" + "}", true, null);
// @formatter:on
CodeActionParams params = CodeActionUtil.constructCodeActionParams(unit, "String address");
CheckConstructorsResponse response = GenerateConstructorsHandler.checkConstructorsStatus(params);
assertNotNull(response.constructors);
assertEquals(2, response.constructors.length);
assertEquals("B", response.constructors[0].name);
assertTrue(Arrays.equals(new String[] { "String" }, response.constructors[0].parameters));
assertEquals("B", response.constructors[1].name);
assertTrue(Arrays.equals(new String[] { "String", "int" }, response.constructors[1].parameters));
assertNotNull(response.fields);
assertEquals(2, response.fields.length);
assertEquals("instance", response.fields[0].name);
assertEquals("String", response.fields[0].type);
assertEquals("address", response.fields[1].name);
assertEquals("String", response.fields[1].type);
}
use of org.eclipse.jdt.ls.core.internal.handlers.GenerateConstructorsHandler.CheckConstructorsResponse in project eclipse.jdt.ls by eclipse.
the class GenerateConstructorsHandlerTest method testCheckConstructorStatus_enum.
@Test
public void testCheckConstructorStatus_enum() throws JavaModelException {
// @formatter:off
ICompilationUnit unit = fPackageP.createCompilationUnit("B.java", "package p;\r\n" + "\r\n" + "public enum B {\r\n" + "}", true, null);
// @formatter:on
CodeActionParams params = CodeActionUtil.constructCodeActionParams(unit, "enum B");
CheckConstructorsResponse response = GenerateConstructorsHandler.checkConstructorsStatus(params);
assertNotNull(response.constructors);
assertEquals(1, response.constructors.length);
assertEquals("Object", response.constructors[0].name);
assertNotNull(response.constructors[0].parameters);
assertEquals(0, response.constructors[0].parameters.length);
assertNotNull(response.fields);
assertEquals(0, response.fields.length);
}
use of org.eclipse.jdt.ls.core.internal.handlers.GenerateConstructorsHandler.CheckConstructorsResponse in project eclipse.jdt.ls by eclipse.
the class GenerateConstructorsHandlerTest method testGenerateConstructors.
@Test
public void testGenerateConstructors() throws ValidateEditException, CoreException, IOException {
// @formatter:off
fPackageP.createCompilationUnit("B.java", "package p;\r\n" + "\r\n" + "public class B {\r\n" + " public B(String name) {\r\n" + " }\r\n" + " public B(String name, int id) {\r\n" + " }\r\n" + " private B() {\r\n" + " }\r\n" + "}", true, null);
ICompilationUnit unit = fPackageP.createCompilationUnit("C.java", "package p;\r\n" + "\r\n" + "public class C extends B {\r\n" + " private static String logger;\r\n" + " private final String uuid = \"123\";\r\n" + " private final String instance;\r\n" + " private String address;\r\n" + "}", true, null);
// @formatter:on
CodeActionParams params = CodeActionUtil.constructCodeActionParams(unit, "String address");
CheckConstructorsResponse response = GenerateConstructorsHandler.checkConstructorsStatus(params);
assertNotNull(response.constructors);
assertEquals(2, response.constructors.length);
assertNotNull(response.fields);
assertEquals(2, response.fields.length);
CodeGenerationSettings settings = new CodeGenerationSettings();
settings.createComments = false;
TextEdit edit = GenerateConstructorsHandler.generateConstructors(unit.findPrimaryType(), response.constructors, response.fields, settings, null, new NullProgressMonitor());
assertNotNull(edit);
JavaModelUtil.applyEdit(unit, edit, true, null);
/* @formatter:off */
String expected = "package p;\r\n" + "\r\n" + "public class C extends B {\r\n" + " private static String logger;\r\n" + " private final String uuid = \"123\";\r\n" + " private final String instance;\r\n" + " private String address;\r\n" + " public C(String name, String instance, String address) {\r\n" + " super(name);\r\n" + " this.instance = instance;\r\n" + " this.address = address;\r\n" + " }\r\n" + " public C(String name, int id, String instance, String address) {\r\n" + " super(name, id);\r\n" + " this.instance = instance;\r\n" + " this.address = address;\r\n" + " }\r\n" + "}";
/* @formatter:on */
compareSource(expected, unit.getSource());
}
use of org.eclipse.jdt.ls.core.internal.handlers.GenerateConstructorsHandler.CheckConstructorsResponse in project eclipse.jdt.ls by eclipse.
the class GenerateConstructorsHandlerTest method testGenerateConstructorsBeforeCursorPosition.
@Test
public void testGenerateConstructorsBeforeCursorPosition() throws ValidateEditException, CoreException, IOException {
String oldValue = preferences.getCodeGenerationInsertionLocation();
try {
preferences.setCodeGenerationInsertionLocation(CodeGenerationUtils.INSERT_BEFORE_CURSOR);
// @formatter:off
fPackageP.createCompilationUnit("B.java", "package p;\r\n" + "\r\n" + "public class B {\r\n" + " public B(String name) {\r\n" + " }\r\n" + " public B(String name, int id) {\r\n" + " }\r\n" + " private B() {\r\n" + " }\r\n" + "}", true, null);
ICompilationUnit unit = fPackageP.createCompilationUnit("C.java", "package p;\r\n" + "\r\n" + "public class C extends B {\r\n" + " private static String logger;\r\n" + " private final String uuid = \"123\";\r\n" + " private final String instance;/*|*/\r\n" + " private String address;\r\n" + "}", true, null);
// @formatter:on
CodeActionParams params = CodeActionUtil.constructCodeActionParams(unit, "String address");
CheckConstructorsResponse response = GenerateConstructorsHandler.checkConstructorsStatus(params);
assertNotNull(response.constructors);
assertEquals(2, response.constructors.length);
assertNotNull(response.fields);
assertEquals(2, response.fields.length);
CodeGenerationSettings settings = new CodeGenerationSettings();
settings.createComments = false;
Range cursor = CodeActionUtil.getRange(unit, "/*|*/");
TextEdit edit = GenerateConstructorsHandler.generateConstructors(unit.findPrimaryType(), response.constructors, response.fields, settings, cursor, new NullProgressMonitor());
assertNotNull(edit);
JavaModelUtil.applyEdit(unit, edit, true, null);
/* @formatter:off */
String expected = "package p;\r\n" + "\r\n" + "public class C extends B {\r\n" + " private static String logger;\r\n" + " private final String uuid = \"123\";\r\n" + " public C(String name, String instance, String address) {\r\n" + " super(name);\r\n" + " this.instance = instance;\r\n" + " this.address = address;\r\n" + " }\r\n" + " public C(String name, int id, String instance, String address) {\r\n" + " super(name, id);\r\n" + " this.instance = instance;\r\n" + " this.address = address;\r\n" + " }\r\n" + " private final String instance;/*|*/\r\n" + " private String address;\r\n" + "}";
/* @formatter:on */
compareSource(expected, unit.getSource());
} finally {
preferences.setCodeGenerationInsertionLocation(oldValue);
}
}
Aggregations