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

@CompileStatic Reference to constructor is ambiguous

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.2.2
    • 2.3.1
    • Static compilation
    • None

    Description

      Groovy does not properly distinguish between overloaded methods when it is attempting to find the appropriate method to call when using @CompileStatic.

      Example:

      MethodOverload.java
      package test;
      
      public class MethodOverload {
          
          public MethodOverload(String s, short[] arr) {}
          public MethodOverload(String s, int... arr) {}
          public MethodOverload(short[] arr) {} /* Causes confusion within Groovy */
          
          private static final String clz = "@groovy.transform.CompileStatic\n" + 
                  "public class Clz {\n" + 
                  "public void run() {\n" + 
                  "println(new test.MethodOverload('d',1))\n" + 
                  "}\n" + 
                  "}";
      
          public static void main(String[] args) throws Exception {
              Class clazz = new groovy.lang.GroovyClassLoader().parseClass(clz);
              clazz.getMethod("run").invoke(clazz.newInstance());
          }
          
      }
      

      Running the above will throw the following:

      Exception in thread "main" org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
      script1394410025829744485975.groovy: 4: [Static type checking] - Reference to method is ambiguous. Cannot choose between [void test.MethodOverload#<init>(short[]), void test.MethodOverload#<init>(java.lang.String, int[])]
      @ line 4, column 9.
      println(new test.MethodOverload("d",1))

      Groovy is mistakenly choosing (short[]) as one of the method candidates.

      Attachments

        Activity

          People

            Unassigned Unassigned
            nmccloud Kenneth Gendron
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: