Paul King created GROOVY-12826:
----------------------------------

             Summary:  SC: an instance extension method called implicitly in a 
lambda receives the Class instead of the enclosing instance
                 Key: GROOVY-12826
                 URL: https://issues.apache.org/jira/browse/GROOVY-12826
             Project: Groovy
          Issue Type: Bug
            Reporter: Paul King


Under @CompileStatic, a native lambda that implicitly calls an instance 
extension method on the enclosing instance passes a Class object as the 
receiver, not "this".

{code:groovy}
import groovy.transform.CompileStatic
import java.util.function.Supplier

@CompileStatic
class C {
    String toString() \{ 'C!' }
    def m() {
        Supplier<String> s = () -> inspect()
        s.get()
    }
}

assert new C().m() == 'C!' // fails: actual value is "class java.lang.Object"
{code}

Other examples of the same problem:
* dump() in a lambda dumps a java.lang.Class rather than the instance.
* with \{ it.getClass().simpleName } in a lambda gives "Class".
* sum() in a lambda, in a subclass of ArrayList<Integer>, throws 
GroovyCastException.
* The same applies to the property form: () -> properties reads the properties 
of the Class.

The equivalent closure, \{ -> inspect() }, gives the expected result.

Cause: an ExtensionMethodNode keeps the static modifier of the method 
implementing it, so isStatic() is true even for an instance extension method. 
StaticTypesLambdaAnalyzer therefore treats the call as a static member 
reference, and classes the lambda as non-capturing.

Proposed fix: treat an extension method as static only when isStaticExtension() 
is true. A true static extension method, such as sleep(1), stays non-capturing.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to