Skip to content
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

Apply var to method invocations #236

Open
MBoegers opened this issue Jun 12, 2023 · 4 comments
Open

Apply var to method invocations #236

MBoegers opened this issue Jun 12, 2023 · 4 comments
Labels
recipe Recipe requested

Comments

@MBoegers
Copy link
Contributor

MBoegers commented Jun 12, 2023

What problem are you trying to solve?

While developing basic var usage with #217 java reveals that local variable type inference is a big field, so we decided to split.
One open area is applying var to variable declarations initialized by method invocations.

What precondition(s) should be checked before applying this recipe?

varis not applicable if

  1. the method has return type ´short´ or byte
  2. the method's return type does not match the type definition in the left-hand side

Describe the situation before applying the recipe

class A {
    String getHello() {
        return "Hello Rewrite";
    }
    void simple(String bar) {
        String msg = getHello(); // (1)
        System.out.println(msg);
    }
    void generic(String bar) {
        List<String> msgs = List.of("Hello", "Rewrite"); // (2)
        System.out.println(msgs);
    }
}

Describe the situation after applying the recipe

class A {
    String getHello() {
        return "Hello Rewrite";
    }
    void simple(String bar) {
        var msg = getHello(); // (1)
        System.out.println(msg);
    }
    void generic(String bar) {
        var msgs = List.of("Hello", "Rewrite"); // (2)
        System.out.println(msgs);
    }
}

Have you considered any alternatives or workarounds?

An alternative would be to no support var in combination with methods

Any additional context

It may be a good idea to implement this as an independent recipe, this would increase separation of concern and make configuration easier.

Are you interested in contributing this recipe to OpenRewrite?

Yes, but need additional support understanding generics.

@knutwannheden knutwannheden added the recipe Recipe requested label Jun 12, 2023
@knutwannheden
Copy link
Contributor

@MBoegers Thanks for offering to work on this! Just reach out to us with questions right here in the issue comments or on Slack, if you prefer.

@timtebeek timtebeek moved this to Backlog in OpenRewrite Jun 12, 2023
@timtebeek timtebeek moved this from Backlog to Recipes Wanted in OpenRewrite Jun 12, 2023
@MBoegers
Copy link
Contributor Author

@knutwannheden thanks for the offer, but first let me close #217 ;)

@MBoegers
Copy link
Contributor Author

I'll start with this one.

@MBoegers MBoegers mentioned this issue Jun 29, 2023
4 tasks
MBoegers added a commit to MBoegers/rewrite-migrate-java that referenced this issue Jun 29, 2023
@MBoegers
Copy link
Contributor Author

MBoegers commented Jul 7, 2023

While implementing the handling of varfor generic constructor invocations, I noticed that it may be hard to handle normal method invocation within the same recipe. That's why I decided to add a new one for handling method invocation separately.

joanvr pushed a commit that referenced this issue Jul 10, 2023
* fix missing imports in VarBaseTest

* add Tests to prove issue #236 us already covered (except generics)

* add new Recipe to cover var for generics add test harness

* add missing licences

* Implement var for generic constructor invocations and move general method invocation to own recipe

* Update UseVarKeywordTest to be compliant with child test UseVarForGenericsConstructorsTest

* Modify Generic Method Invocations
* Update name of UseVarForGenericMethodInvocations to reflect action
* Fix yml recipe java-lang-var.yml
* update tests to reflect shortcomings of open rewrite regarding generic method invocations

* Apply suggestions from code review

Co-authored-by: Joan Viladrosa <[email protected]>

---------

Co-authored-by: Joan Viladrosa <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
recipe Recipe requested
Projects
Status: Recipes Wanted
Development

No branches or pull requests

2 participants