[ 
https://issues.apache.org/jira/browse/GROOVY-11800?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18037471#comment-18037471
 ] 

Eric Milles commented on GROOVY-11800:
--------------------------------------

Groovy 5 tries the normal path for resolving property "log" against the 
closure.  In both cases, before checking the owner, if the delegate is a 
collection or array "getAt" is tried.  "['']*.log" produces an error since 
string does not have "log" property.  "[]*.log" produces an empty list.  This 
new empty list is the value of "log" and then "((Logger) [])" fails on 
constructor invocation.

> Unqualified member access in empty array tap resolves incorrectly in Groovy 5
> -----------------------------------------------------------------------------
>
>                 Key: GROOVY-11800
>                 URL: https://issues.apache.org/jira/browse/GROOVY-11800
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 5.0.2
>            Reporter: Stephen Albrecht
>            Assignee: Eric Milles
>            Priority: Major
>
> In migrating to Groovy 5 I had to prepend certain slf4j `log` calls with 
> `this` or else they would throw a RuntimeException. Below is a reproducible 
> example.
> build.gradle dependencies
> {code:java}
> dependencies {
>     implementation 'org.apache.groovy:groovy:5.0.2'
>     implementation 'ch.qos.logback:logback-core:1.5.20'
>     implementation 'ch.qos.logback:logback-classic:1.5.20'
> } {code}
> Main.groovy
> {code:java}
> package org.example
> import groovy.util.logging.Slf4j
> @Slf4j
> class Foo {
>     def bar() {
>         [''].tap {
>             log.info "Logs in Groovy 4 and 5"
>         }
>         [].tap {
>             log.info "Logs in Groovy 4, fails in Groovy 5"
>         }
>     }
> }
> new Foo().bar(){code}
> Output
> {code:java}
> 15:12:45.605 [main] INFO org.example.Foo -- Logs in Groovy 4 and 5
> Caught: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot 
> cast object '[]' with class 'java.util.ArrayList' to class 'org.slf4j.Logger' 
> due to: groovy.lang.GroovyRuntimeException: Could not find matching 
> constructor for: org.slf4j.Logger()
> org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast 
> object '[]' with class 'java.util.ArrayList' to class 'org.slf4j.Logger' due 
> to: groovy.lang.GroovyRuntimeException: Could not find matching constructor 
> for: org.slf4j.Logger()
>  at org.example.Foo$_bar_closure2.doCall(Main.groovy:12)
>  at org.example.Foo.bar(Main.groovy:11)
>  at org.example.Main.run(Main.groovy:17)
>  Suppressed: groovy.lang.GroovyRuntimeException: Could not find matching 
> constructor for: org.slf4j.Logger(ArrayList)
>  at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1797)
>  at groovy.lang.MetaClassImpl.invokeConstructor(MetaClassImpl.java:1572)
>  at 
> org.codehaus.groovy.runtime.InvokerHelper.invokeConstructorOf(InvokerHelper.java:666)
>  at 
> org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnSAM(DefaultTypeTransformation.java:400)
>  at 
> org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:339)
>  at 
> org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:258)
>  at 
> org.codehaus.groovy.vmplugin.v8.IndyInterface.fromCache(IndyInterface.java:344)
>  at org.example.Foo$_bar_closure2.doCall(Main.groovy:12)
>  at 
> java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
>  at java.base/java.lang.reflect.Method.invoke(Method.java:565)
>  at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:338)
>  at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:274)
>  at 
> org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:270)
>  at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1008)
>  at groovy.lang.Closure.call(Closure.java:471)
>  at groovy.lang.Closure.call(Closure.java:460)
>  at 
> org.codehaus.groovy.runtime.DefaultGroovyMethods.callWithDelegateAndParameter(DefaultGroovyMethods.java:276)
>  at 
> org.codehaus.groovy.runtime.DefaultGroovyMethods.tap(DefaultGroovyMethods.java:14853)
>  at org.codehaus.groovy.runtime.dgm$1164.doMethodInvoke(Unknown Source)
>  at 
> org.codehaus.groovy.vmplugin.v8.IndyInterface.fromCache(IndyInterface.java:344)
>  at org.example.Foo.bar(Main.groovy:11)
>  at 
> org.codehaus.groovy.vmplugin.v8.IndyInterface.fromCache(IndyInterface.java:344)
>  at org.example.Main.run(Main.groovy:17)
>  at 
> groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:290)
>  at groovy.lang.GroovyShell.run(GroovyShell.java:422)
>  at groovy.lang.GroovyShell.run(GroovyShell.java:411)
>  at groovy.ui.GroovyMain.processOnce(GroovyMain.java:649)
>  at groovy.ui.GroovyMain.run(GroovyMain.java:389)
>  at groovy.ui.GroovyMain$GroovyCommand.process(GroovyMain.java:313)
>  at groovy.ui.GroovyMain.processArgs(GroovyMain.java:141)
>  at groovy.ui.GroovyMain.main(GroovyMain.java:114)
>  at 
> java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
>  at java.base/java.lang.reflect.Method.invoke(Method.java:565)
>  at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:115)
>  at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:37){code}
> I would expect `log` to resolve correctly in both cases, and IntelliJ flags 
> the `this` workaround as an unnecessary qualified reference. 



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to