Search in sources :

Example 1 with AccessorField

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);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) AccessorField(org.eclipse.jdt.ls.core.internal.codemanipulation.GenerateGetterSetterOperation.AccessorField) Test(org.junit.Test)

Example 2 with AccessorField

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();
    }
}
Also used : CodeActionProposal(org.eclipse.jdt.ls.core.internal.handlers.CodeActionProposal) IJavaElement(org.eclipse.jdt.core.IJavaElement) CoreException(org.eclipse.core.runtime.CoreException) Command(org.eclipse.lsp4j.Command) TextEdit(org.eclipse.text.edits.TextEdit) CodeAction(org.eclipse.lsp4j.CodeAction) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) AccessorField(org.eclipse.jdt.ls.core.internal.codemanipulation.GenerateGetterSetterOperation.AccessorField) GenerateGetterSetterOperation(org.eclipse.jdt.ls.core.internal.codemanipulation.GenerateGetterSetterOperation)

Example 3 with AccessorField

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);
}
Also used : TextEdit(org.eclipse.text.edits.TextEdit) AccessorField(org.eclipse.jdt.ls.core.internal.codemanipulation.GenerateGetterSetterOperation.AccessorField)

Example 4 with AccessorField

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);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) AccessorField(org.eclipse.jdt.ls.core.internal.codemanipulation.GenerateGetterSetterOperation.AccessorField) Test(org.junit.Test)

Aggregations

AccessorField (org.eclipse.jdt.ls.core.internal.codemanipulation.GenerateGetterSetterOperation.AccessorField)4 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 TextEdit (org.eclipse.text.edits.TextEdit)2 Test (org.junit.Test)2 CoreException (org.eclipse.core.runtime.CoreException)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 IJavaElement (org.eclipse.jdt.core.IJavaElement)1 GenerateGetterSetterOperation (org.eclipse.jdt.ls.core.internal.codemanipulation.GenerateGetterSetterOperation)1 CodeActionProposal (org.eclipse.jdt.ls.core.internal.handlers.CodeActionProposal)1 CodeAction (org.eclipse.lsp4j.CodeAction)1 Command (org.eclipse.lsp4j.Command)1