Skip to content

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

@nmck257

Description

@nmck257

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

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingenhancementNew feature or request

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions