podam icon indicating copy to clipboard operation
podam copied to clipboard

Add support for setters that return an ancestor type

Open chrisbouchard opened this issue 7 years ago • 2 comments

It would be nice to accept a method as a setter even if it is declared to return an ancestor type. E.g.,

public interface MyInterface {
    public int getValue();
    public MyInterface setValue(int value);
}

public class MyImplementation implements MyInterface {
    private int value;
    @Override public int getValue() { return value; }
    @Override public MyInterface setValue(int value) {
        this.value = value; return this;
    }
}

(Putting aside for a moment that in this case we could declare the overridden method with a more restrictive return type. In my particular case the implementation is generated code.)

Podam would fail to consider MyImplementation#setValue(int) to be a setter, because it returns MyInterface rather than MyImplementation. Is there any reason not to allow this case?

As far as I can tell, my only work-around at the moment would be to re-implement all of AbstractClassInfoStrategy#findPojoAttributes in a custom class info strategy, which seems like a lot of unnecessary work. Essentially, I'd like to make the following change in that method:

} else if (method.getParameterTypes().length > 0
        && (method.getReturnType().equals(void.class)
                || method.getReturnType().equals(workClass))) {

to

} else if (method.getParameterTypes().length > 0
        && (method.getReturnType().equals(void.class)
                || method.getReturnType().isAssignableFrom(workClass))) {

chrisbouchard avatar Mar 11 '19 17:03 chrisbouchard

Hi,

Thanks for a suggestion. Maybe you can do a pull request?

Thanks, Daniil

daivanov avatar Mar 13 '19 05:03 daivanov

Added to Podam 7.2.3

daivanov avatar Mar 22 '19 09:03 daivanov