Wouter Peers created GROOVY-11558:
-------------------------------------

             Summary: Method reference treated as closure in method dispatch
                 Key: GROOVY-11558
                 URL: https://issues.apache.org/jira/browse/GROOVY-11558
             Project: Groovy
          Issue Type: Bug
          Components: Compiler, Static Type Checker
    Affects Versions: 4.0.24
            Reporter: Wouter Peers


When passing a method reference to an overloaded method of which one of the 
signatures accepts a functional interface. Groovy seems to treat the method ref 
as a closure and dispatches to the wrong method signature.

Example:
{code:groovy}
@FunctionalInterface
interface PresenceAware {
    boolean isPresent()
}

class Presence {
    static boolean isPresent(Object o){
        o != null
    }
    static boolean isPresent(PresenceAware p){
        p.isPresent()
    }
}

class StringWrapper implements PresenceAware{

    private final String s

    StringWrapper(String s) {
        this.s = s
    }

    @Override
    boolean isPresent() {
        return s!= null && !s.empty
    }
}

assert !Presence.isPresent(null)
assert Presence.isPresent('')
assert !Presence.isPresent(new StringWrapper(''))
assert Presence.isPresent(Integer.&compare) 
// => MissingMethodException (in Groovy v4.0.24)
assert Presence.isPresent(Integer.&valueOf) 
// => MethodSelectionException (in Groovy v4.0.24)
{code}
This code runs fine in Groovy v2.4.7—as I would expect, the method references 
passed in the last two statements are treated as an Object when dispatching to 
Presence::valueOf mehtod.

In Groovy v4.0.24 however the method references seem to be (wrongfully) treated 
as closures and are dispatched to Presence::isPresent(PresenceAware) signature.



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

Reply via email to