[
https://issues.apache.org/jira/browse/GROOVY-12232?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18105210#comment-18105210
]
ASF GitHub Bot commented on GROOVY-12232:
-----------------------------------------
paulk-asert opened a new pull request, #2801:
URL: https://github.com/apache/groovy/pull/2801
…ctor delegates to super dynamically
Since 5.0.0-alpha-4 (GROOVY-6285/GROOVY-9244), writeAICCall wraps every
non-primitive cast argument in a PojoWrapper so the anonymous inner class's
generated constructor can select among ambiguous super constructors by the
declared cast type at runtime — the dynamic
selectConstructorAndTransformArguments path unwraps it. But when the super
constructor is arity-unique, the generated constructor binds it statically and
consumes its Object parameter with a plain cast, which the wrapper reaches
intact: new Test(['a'] as Set) {} failed with GroovyCastException 'Cannot cast
PojoWrapper to Set' (reduced from Gradle's test suite).
writeAICCall now evaluates the same predicate the constructor's own
compilation uses for its super call (arity-unique candidate, no spread
arguments — mirroring makeDirectConstructorCall) against the generated
constructor's super call, found by scanning its body (the this$0 assignment may
precede it), and wraps only when delegation will be dynamic. Both consumers now
see what they expect: the dynamic selector receives wrappers and keeps choosing
by cast type; a statically bound super call receives the plain value.
> `GroovyCastException: Cannot cast object PojoWrapper` when an as coercion is
> passed directly as an argument to an anonymous inner class constructor
> ---------------------------------------------------------------------------------------------------------------------------------------------------
>
> Key: GROOVY-12232
> URL: https://issues.apache.org/jira/browse/GROOVY-12232
> Project: Groovy
> Issue Type: Bug
> Affects Versions: 5.0.8
> Reporter: Octavia Togami
> Assignee: Paul King
> Priority: Minor
>
> This used to work on Groovy 4.0.33, but fails now on 5.0.8:
> {code:groovy}
> class Test {
> Test(Set<String> values) {}
> }
> new Test(['a'] as Set) {}
> {code}
> The error is:
> {code:sh}
> GroovyCastException: Cannot cast object
> 'org.codehaus.groovy.runtime.wrappers.PojoWrapper@47fbc56'
> with class 'org.codehaus.groovy.runtime.wrappers.PojoWrapper' to class
> 'java.util.Set'
> {code}
> Without the anon class, coercion (e.g. using {{Set.of(...)}}), if it's stored
> in a local var first, or if the class constructor is not statically
> resolvable; it works fine.
> This was reduced from real code in Gradle:
> https://github.com/gradle/gradle/blob/be7b1d956fd466144fbb3a1e268bfd6704205250/platforms/jvm/java-compiler-worker/src/test/groovy/org/gradle/api/internal/tasks/compile/AnnotationProcessingCompileTaskTest.groovy#L58
--
This message was sent by Atlassian Jira
(v8.20.10#820010)