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

AddMissingMethodImplementation should check for superclass implementations of the relevant method #466

Open
nmck257 opened this issue Apr 25, 2024 · 3 comments
Labels
bug Something isn't working enhancement New feature or request

Comments

@nmck257
Copy link
Contributor

nmck257 commented Apr 25, 2024

What problem are you trying to solve?

Currently, the AddMissingMethodImplementation checks primarily for existing method declarations on the visited class declaration. It does not check whether the visited class has a superclass which already provides an implementation of the relevant method.

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

Existing recipe, no suggested precondition changes

Describe the situation before applying the recipe

// surrounding code
interface MyInterface {
    void foo();
    void bar();
}
class MyBaseClass implements MyInterface {
    public void bar() {}
}
// code for the recipe to actually visit
class MyClass extends MyBaseClass {

}

Describe the situation after applying the recipe

If we run a recipe like this:

  - org.openrewrite.java.migrate.AddMissingMethodImplementation:
      fullyQualifiedClassName: MyInterface
      methodPattern: "*..* foo()"
      methodTemplateString: "public void foo() { /* added */ }"

...then we would get this:

class MyClass extends MyBaseClass {
    public void foo() { /* added */ }
}

But if we run this

  - org.openrewrite.java.migrate.AddMissingMethodImplementation:
      fullyQualifiedClassName: MyInterface
      methodPattern: "*..* bar()"
      methodTemplateString: "public void bar() { /* added */ }"

...then we would get no change, because MyClass already has an implementation of bar() from MyBaseClass.

Have you considered any alternatives or workarounds?

This behavior could also be disabled with a new option (default enabled)

Any additional context

Are you interested in contributing this recipe to OpenRewrite?

Maybe eventually; could also be a good first issue

@timtebeek timtebeek added the enhancement New feature or request label Apr 26, 2024
@timtebeek timtebeek moved this to Backlog in OpenRewrite Apr 26, 2024
@timtebeek
Copy link
Contributor

@cjobinabo Since AddMissingMethodImplementation is heavily used in some of the recipes your team contributed I figured
let you know about this issue, as it might affect your users too. No expectation of course that you'll fix this; we're already glad with all the work that you've done, but thought it good to coordinate any changes here.

displayName: Adds missing JDBC interface methods.
description: Add method implementations stubs to classes that implement JDBC interfaces.
recipeList:
- org.openrewrite.java.migrate.AddMissingMethodImplementation:
fullyQualifiedClassName: java.sql.Connection
methodPattern: "*..* abort(java.util.concurrent.Executor)"
methodTemplateString: "public void abort(java.util.concurrent.Executor executor) { \n\t// TODO Auto-generated method stub\n }"
- org.openrewrite.java.migrate.AddMissingMethodImplementation:
fullyQualifiedClassName: java.sql.Connection
methodPattern: "*..* getNetworkTimeout()"
methodTemplateString: "public int getNetworkTimeout() { \n\t// TODO Auto-generated method stub\n return 0; }"

@timtebeek timtebeek added the bug Something isn't working label Apr 26, 2024
@rgustafson-ie
Copy link

I saw this in practice today, on subclasses of Spring's AbstractDataSource.

@cjobinabo
Copy link
Contributor

cjobinabo commented Jun 27, 2024

@cjobinabo Since AddMissingMethodImplementation is heavily used in some of the recipes your team contributed I figured let you know about this issue, as it might affect your users too. No expectation of course that you'll fix this; we're already glad with all the work that you've done, but thought it good to coordinate any changes here.

displayName: Adds missing JDBC interface methods.
description: Add method implementations stubs to classes that implement JDBC interfaces.
recipeList:
- org.openrewrite.java.migrate.AddMissingMethodImplementation:
fullyQualifiedClassName: java.sql.Connection
methodPattern: "*..* abort(java.util.concurrent.Executor)"
methodTemplateString: "public void abort(java.util.concurrent.Executor executor) { \n\t// TODO Auto-generated method stub\n }"
- org.openrewrite.java.migrate.AddMissingMethodImplementation:
fullyQualifiedClassName: java.sql.Connection
methodPattern: "*..* getNetworkTimeout()"
methodTemplateString: "public int getNetworkTimeout() { \n\t// TODO Auto-generated method stub\n return 0; }"

Thanks for the heads up, Tim (I know I'm super late to your message 😓). I'll try to keep an eye out for updates addressing this GitHub issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
Status: Backlog
Development

No branches or pull requests

4 participants