[
https://issues.apache.org/jira/browse/GROOVY-11832?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18050897#comment-18050897
]
Eric Milles commented on GROOVY-11832:
--------------------------------------
The distance computation between argument and parameter does favor
{{StepBuilderHelper#listener(BaseListener)}}. However, the distance between
receiver and declaring class is then factored in and
{{SimpleStepBuilder#listener(Object)}} wins out.
chooseBestMethod has been reworked in Groovy 5+ for this very reason:
Groovy 4:
https://github.com/apache/groovy/blob/dbcb40562d7c4dce30172bc5f0bc8f64db10589a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java#L1043
Groovy 5:
https://github.com/apache/groovy/blob/37e99a60941c4f2b501a65c28a525c5057678ba8/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java#L1032
You can overcome this in Groovy 4 three ways:
# add a typecast to the receiver at the call site: {{((StepBuilderHelper) new
StepBuilder().chunk()).listener(new MyListener())}}
# add an override to close the distance:
{code:java}
public class SimpleStepBuilder<I, O> extends
AbstractTaskletStepBuilder<SimpleStepBuilder<I, O>> {
@Override
protected SimpleStepBuilder<I, O> self() {
return this;
}
@Override
public SimpleStepBuilder<I, O> listener(Object listener) {
return super.listener(listener);
}
@Override
public SimpleStepBuilder<I, O> listener(BaseListener listener) {
return super.listener(listener);
}
}
{code}
# add a type checking extension for onMethodSelection and change the target
method
> CompileStatic causes wrong method to be invoked
> -----------------------------------------------
>
> Key: GROOVY-11832
> URL: https://issues.apache.org/jira/browse/GROOVY-11832
> Project: Groovy
> Issue Type: Bug
> Components: Static compilation
> Affects Versions: 4.0.29
> Environment: Groovy 4.0.29, latest mac os, java 17
> Reporter: James Daugherty
> Assignee: Eric Milles
> Priority: Major
>
> I have been working on upgrading a Spring Boot Batch project that uses groovy
> and found an issue in Groovy where the wrong method is invoked if the code is
> annotated with CompileStatic.
>
> The class has 2 forms of a `listener` method - one that takes a specific type
> and one that takes Object. The specific type method is on the grand parent
> class while the object method is on the lowest class (overrides a similar
> method that is also on the grand parent class). When `@CompileStatic` is
> added, the wrong method is invoked (object is called instead of the specific
> type method).
>
> Casting doesn't seem to make a difference on the method invoked. I was not
> able to reproduce this with a simple example, it seems to require some
> combination of generics & inheritance to trigger this.
>
> Test project is here:
> [https://github.com/jdaugherty/groovy-issue-wrong-method]
>
> Run the test `WrongResolutionSpec` to verify this behavior.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)