use of org.eclipse.jdt.ls.core.internal.codemanipulation.GenerateGetterSetterOperation.AccessorField in project eclipse.jdt.ls by eclipse.
the class GenerateAccessorsHandlerTest method testResolveUnimplementedAccessors_methodsExist.
@Test
public void testResolveUnimplementedAccessors_methodsExist() throws JavaModelException {
// @formatter:off
ICompilationUnit unit = fPackageP.createCompilationUnit("B.java", "package p;\r\n" + "\r\n" + "public class B {\r\n" + " String name;\r\n" + " int id;\r\n" + " public String getName() {\r\n" + " return name;\r\n" + " }\r\n" + " public void setName(String name) {\r\n" + " this.name = name;\r\n" + " }\r\n" + " public int getId() {\r\n" + " return id;\r\n" + " }\r\n" + "}", true, null);
// @formatter:on
AccessorField[] accessors = GenerateAccessorsHandler.getUnimplementedAccessors(unit.findPrimaryType());
assertNotNull(accessors);
assertEquals(1, accessors.length);
assertEquals("id", accessors[0].fieldName);
assertFalse(accessors[0].generateGetter);
assertTrue(accessors[0].generateSetter);
}
use of org.eclipse.jdt.ls.core.internal.codemanipulation.GenerateGetterSetterOperation.AccessorField in project eclipse.jdt.ls by eclipse.
the class SourceAssistProcessor method getGetterSetterAction.
private Optional<Either<Command, CodeAction>> getGetterSetterAction(CodeActionParams params, IInvocationContext context, IType type, String kind, boolean isInTypeDeclaration) {
try {
AccessorField[] accessors = GenerateGetterSetterOperation.getUnimplementedAccessors(type);
if (accessors == null || accessors.length == 0) {
return Optional.empty();
} else if (accessors.length == 1 || !preferenceManager.getClientPreferences().isAdvancedGenerateAccessorsSupported()) {
CodeActionProposal getAccessorsProposal = (pm) -> {
// If cursor position is not specified, then insert to the last by default.
IJavaElement insertBefore = isInTypeDeclaration ? CodeGenerationUtils.findInsertElement(type, null) : CodeGenerationUtils.findInsertElement(type, params.getRange());
GenerateGetterSetterOperation operation = new GenerateGetterSetterOperation(type, context.getASTRoot(), preferenceManager.getPreferences().isCodeGenerationTemplateGenerateComments(), insertBefore);
TextEdit edit = operation.createTextEdit(pm, accessors);
return convertToWorkspaceEdit(context.getCompilationUnit(), edit);
};
return getCodeActionFromProposal(params.getContext(), context.getCompilationUnit(), ActionMessages.GenerateGetterSetterAction_label, kind, getAccessorsProposal);
} else {
Command command = new Command(ActionMessages.GenerateGetterSetterAction_ellipsisLabel, COMMAND_ID_ACTION_GENERATEACCESSORSPROMPT, Collections.singletonList(params));
if (preferenceManager.getClientPreferences().isSupportedCodeActionKind(JavaCodeActionKind.SOURCE_GENERATE_ACCESSORS)) {
CodeAction codeAction = new CodeAction(ActionMessages.GenerateGetterSetterAction_ellipsisLabel);
codeAction.setKind(kind);
codeAction.setCommand(command);
codeAction.setDiagnostics(Collections.emptyList());
return Optional.of(Either.forRight(codeAction));
} else {
return Optional.of(Either.forLeft(command));
}
}
} catch (OperationCanceledException | CoreException e) {
JavaLanguageServerPlugin.logException("Failed to generate Getter and Setter source action", e);
return Optional.empty();
}
}
use of org.eclipse.jdt.ls.core.internal.codemanipulation.GenerateGetterSetterOperation.AccessorField in project eclipse.jdt.ls by eclipse.
the class GenerateAccessorsHandlerTest method generateAccessors.
private void generateAccessors(IType type, Range cursor) throws ValidateEditException, CoreException {
AccessorField[] accessors = GenerateAccessorsHandler.getUnimplementedAccessors(type);
TextEdit edit = GenerateAccessorsHandler.generateAccessors(type, accessors, true, cursor);
assertNotNull(edit);
JavaModelUtil.applyEdit(type.getCompilationUnit(), edit, true, null);
}
use of org.eclipse.jdt.ls.core.internal.codemanipulation.GenerateGetterSetterOperation.AccessorField in project eclipse.jdt.ls by eclipse.
the class GenerateAccessorsHandlerTest method testResolveUnimplementedAccessors.
@Test
public void testResolveUnimplementedAccessors() throws JavaModelException {
// @formatter:off
ICompilationUnit unit = fPackageP.createCompilationUnit("B.java", "package p;\r\n" + "\r\n" + "public class B {\r\n" + " private static String staticField = \"23434343\";\r\n" + " private final String finalField;\r\n" + " String name;\r\n" + "}", true, null);
// @formatter:on
AccessorField[] accessors = GenerateAccessorsHandler.getUnimplementedAccessors(unit.findPrimaryType());
assertNotNull(accessors);
assertEquals(3, accessors.length);
assertEquals("staticField", accessors[0].fieldName);
assertTrue(accessors[0].isStatic);
assertTrue(accessors[0].generateGetter && accessors[0].generateSetter);
assertEquals("finalField", accessors[1].fieldName);
assertFalse(accessors[1].isStatic);
assertTrue(accessors[1].generateGetter);
assertFalse(accessors[1].generateSetter);
assertEquals("name", accessors[2].fieldName);
assertFalse(accessors[2].isStatic);
assertTrue(accessors[2].generateGetter);
assertTrue(accessors[2].generateSetter);
}
Aggregations