You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Thodoris Sotiropoulos (Jira)" <ji...@apache.org> on 2023/03/09 15:53:00 UTC

[jira] [Updated] (GROOVY-10972) Issue in resolving overloaded method reference

     [ https://issues.apache.org/jira/browse/GROOVY-10972?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Thodoris Sotiropoulos updated GROOVY-10972:
-------------------------------------------
    Description: 
I have the following program

{code}
import java.util.function.Function;
import java.util.LinkedList;

class Foo {}


class Test {

    public static void main(String[] args) {
        LinkedList<Foo> x = new LinkedList();
        x.add(new Foo());

        Function<Integer, Foo> z = x::remove;
        Foo k = z.apply(0); // ClassCastException boolean cannot be cast to Foo.
    }

}
{code}

h3. Actual behavior

The compiler accepts the code, but the following exception is triggered at runtime.

{code}
Exception in thread "main" org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'false' with class 'java.lang.Boolean' to class 'Foo'
        at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnSAM(DefaultTypeTransformation.java:425)
        at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:336)
        at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:256)
        at org.codehaus.groovy.vmplugin.v8.IndyInterface.fromCache(IndyInterface.java:321)
        at Test.main(test.groovy:14)
{code}

There problem happens because there are two overloaded methods inside LinkedList:
  * E remove(int x)
  * boolean remove(Object d)

The compiler seems to resolve the second method (i.e., boolean remove (Object)) although its return type does not match with Function<Integer, Foo>.

Based only on the argument type, both methods are applicable. If the second "remove" method is more specific than the first one, then a compiler error should be raised at compile-time, because boolean does not match Foo. If this is not the case, then the program should call the first "remove" method.

  was:
I have the following program

{code}
import java.util.function.Function;
import java.util.LinkedList;

class Foo {}


class Test {

    public static void main(String[] args) {
        LinkedList<Foo> x = new LinkedList();
        x.add(new Foo());

        Function<Integer, Foo> z = x::remove;
        Foo k = z.apply(0); // ClassCastException boolean cannot be cast to Foo.
    }

}
{code}

h3. Actual behavior

The compiler accepts the code, but the following exception is triggered at runtime.

{code}
Exception in thread "main" org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'false' with class 'java.lang.Boolean' to class 'Foo'
        at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnSAM(DefaultTypeTransformation.java:425)
        at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:336)
        at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:256)
        at org.codehaus.groovy.vmplugin.v8.IndyInterface.fromCache(IndyInterface.java:321)
        at Test.main(test.groovy:14)
{code}

There problem happens because there are two overloaded methods inside LinkedList:
  * E remove(int x)
  * boolean remove(Object d)

The compiler seems to choose to resolve the second method although its return type does not match with Function<Integer, Foo>.

Based only on the argument type, both methods are applicable. If the second "remove" method is more specific than the first one, then a compiler error should be raised at compile-time. If it's not the case, then the program should call the first "remove" method.


> Issue in resolving overloaded method reference
> ----------------------------------------------
>
>                 Key: GROOVY-10972
>                 URL: https://issues.apache.org/jira/browse/GROOVY-10972
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static Type Checker
>            Reporter: Thodoris Sotiropoulos
>            Priority: Major
>
> I have the following program
> {code}
> import java.util.function.Function;
> import java.util.LinkedList;
> class Foo {}
> class Test {
>     public static void main(String[] args) {
>         LinkedList<Foo> x = new LinkedList();
>         x.add(new Foo());
>         Function<Integer, Foo> z = x::remove;
>         Foo k = z.apply(0); // ClassCastException boolean cannot be cast to Foo.
>     }
> }
> {code}
> h3. Actual behavior
> The compiler accepts the code, but the following exception is triggered at runtime.
> {code}
> Exception in thread "main" org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'false' with class 'java.lang.Boolean' to class 'Foo'
>         at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnSAM(DefaultTypeTransformation.java:425)
>         at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:336)
>         at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:256)
>         at org.codehaus.groovy.vmplugin.v8.IndyInterface.fromCache(IndyInterface.java:321)
>         at Test.main(test.groovy:14)
> {code}
> There problem happens because there are two overloaded methods inside LinkedList:
>   * E remove(int x)
>   * boolean remove(Object d)
> The compiler seems to resolve the second method (i.e., boolean remove (Object)) although its return type does not match with Function<Integer, Foo>.
> Based only on the argument type, both methods are applicable. If the second "remove" method is more specific than the first one, then a compiler error should be raised at compile-time, because boolean does not match Foo. If this is not the case, then the program should call the first "remove" method.



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