rewrite-static-analysis icon indicating copy to clipboard operation
rewrite-static-analysis copied to clipboard

DeclarationSiteTypeVariance changes methods to be incompatible with super declaration

Open Bananeweizen opened this issue 1 year ago • 1 comments

What is the smallest, simplest way to reproduce the problem?

class A extends SomeExternalClassThatCannotBeChanged {
    @Override
    public void foo(final Supplier<String> supplier) {
       ...
    }
}

What did you expect to see?

No change.

What did you see instead?

The recipe makes the supplier use ? extends String. While that's the general goal of the recipe, it leads to failures if the super implementation is not contained in the source set modified by OpenRewrite. The subclass implementation becomes incompatible with the super declaration.

Are you interested in contributing a fix to OpenRewrite?

Maybe. The question is: What would be a good criterion to not apply the recipe? Can we check if the super declaration is part of the sources that we currently work on? Or should we generally avoid touching methods annotated with Override?

Bananeweizen avatar Mar 08 '24 12:03 Bananeweizen

As a quick first step it's perhaps best to be safe indeed by not changing methods that override another method. We have a small utility that could help there. And thanks for the offer to help!

timtebeek avatar Mar 08 '24 12:03 timtebeek