Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-10972

Issue in resolving overloaded method reference

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • None
    • 3.0.18, 4.0.12
    • Static Type Checker
    • None

    Description

      I have the following program

      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.
          }
      
      }
      

      Actual behavior

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

      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)
      

      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.

      Attachments

        Activity

          People

            emilles Eric Milles
            theosot Thodoris Sotiropoulos
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: