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

invalid reference to an array constructor leads to type unsafe results

    XMLWordPrintableJSON

Details

    Description

      I have the following program

      import java.util.function.*;
      
      class A{
      }
      class Test {
        public static void main(String[] args) {
          Function<Object, A[]> x = A[]::new;
          A[] y = x.apply(new Object());
        }
      } 

      Actual behavior

      The code compiles, and I get a ClassCastException:

      Exception in thread "main" org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'java.lang.Object@63d4e2ba' with class 'java.lang.Object' to class 'int'
          at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToNumber(DefaultTypeTransformation.java:177)
          at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.intUnbox(DefaultTypeTransformation.java:85)
          at Test.ctorRef$main$0(test.groovy)
          at Test.main(test.groovy:13) 

      Actual behavior

      The code should have been rejected.

      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: