Skip to content

Do necessary down casts when generating method calls #2576

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 15, 2023

Conversation

IlyaMuravjov
Copy link
Collaborator

@IlyaMuravjov IlyaMuravjov commented Sep 1, 2023

Description

Fixes #2598

Since code generation uses erased types, it may need to perform downcasts when calling methods, this PR adds such down casts.

How to test

Manual tests

Generate tests for the following classes, all tests should compile and pass, there should be no redundant casts.

public class Box<T> {
    private final T value;

    public Box(T value) {
        this.value = value;
    }

    public T getValue() {
        return value;
    }
}
public class StringHolder {
    private final String string;

    public StringHolder(String string) {
        this.string = string;
    }

    public String getString() {
        return string;
    }

    public static Box<StringHolder> createBoxed() {
        return new Box<>(new StringHolder("s"));
    }

    public static StringHolder create() {
        return new StringHolder("s");
    }
}
public class StringHolderChild extends StringHolder {
    public StringHolderChild(String string) {
        super(string);
    }

    public static Box<StringHolder> createBoxed() {
        return new Box<>(new StringHolderChild("s"));
    }

    public static StringHolder create() {
        return new StringHolderChild("s");
    }
}

Expected tests for SpringHolderChild (summaries are omitted):

public final class StringHolderChildTest {
    @Test
    @DisplayName("createBoxed: -> return new Box<>(new StringHolderChild(\"s\"))")
    public void testCreateBoxed_Return() {
        Box actual = StringHolderChild.createBoxed();

        String string = "s";
        StringHolderChild stringHolderChild = new StringHolderChild(string);
        Box expected = new Box(stringHolderChild);

        Object expectedValue = expected.getValue();
        Object actualValue = actual.getValue();
        String expectedValueString = (((StringHolder) expectedValue)).getString();
        String actualValueString = (((StringHolder) actualValue)).getString();
        assertEquals(expectedValueString, actualValueString);

    }
    
    @Test
    @DisplayName("create: -> return new StringHolderChild(\"s\")")
    public void testCreate_Return() {
        StringHolderChild actual = ((StringHolderChild) StringHolderChild.create());

        String string = "s";
        StringHolderChild expected = new StringHolderChild(string);

        String expectedString = expected.getString();
        String actualString = actual.getString();
        assertEquals(expectedString, actualString);

    }
}

Self-check list

  • I've set the proper labels for my PR (at least, for category and component).
  • PR title and description are clear and intelligible.
  • I've added enough comments to my code, particularly in hard-to-understand areas.
  • The functionality I've repaired, changed or added is covered with automated tests.
  • Manual tests have been provided optionally.
  • The documentation for the functionality I've been working on is up-to-date.

@IlyaMuravjov IlyaMuravjov added comp-codegen Issue is related to code generator ctg-bug-fix PR is fixing a bug labels Sep 1, 2023
@IlyaMuravjov IlyaMuravjov merged commit 2a74b8c into main Sep 15, 2023
@IlyaMuravjov IlyaMuravjov deleted the ilya_m/downcast-receiver branch September 15, 2023 13:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp-codegen Issue is related to code generator ctg-bug-fix PR is fixing a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generic type MessageResponse of Http response is not applied is assertion part
2 participants