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

Static type checking can't find default method overload on interface

    XMLWordPrintableJSON

Details

    Description

      The static type checker cannot find default methods on an interface if the method overloads an abstract method. For example, consider the following Java classes:

      package net.robinfriedli.botify.scripting;
      
      public interface TestInt {
      
          void foo(String s);
      
          default void foo(long l) {}
      }
      
      public class TestImpl implements TestInt {
          @Override
          public void foo(String s) {
          }
      }

      When compiling the script

      new net.robinfriedli.botify.scripting.TestImpl().foo(1l)

       the following exception is thrown:

       

      MultipleCompilationErrorsException:
      startup failed:
      Script1.groovy: 1: [Static type checking] - Cannot find matching method net.robinfriedli.botify.scripting.TestImpl#foo(long). Please check if the declared type is correct and if the method exists.
       @ line 1, column 1.
         new net.robinfriedli.botify.scripting.TestImpl().foo(1l)
         ^
      
      1 error
      

      The script runs fine without static compilation.

       

      Calling the implemented abstract method compiles sucessfully:

      new net.robinfriedli.botify.scripting.TestImpl().foo('str')
      

      However, when changing the interface to

       

      package net.robinfriedli.botify.scripting;
      
      public interface TestInt {
      
          void foo(String s);
      
          default void bar(long l) {}
      }
      

      The following script compiles:

       

       

      new net.robinfriedli.botify.scripting.TestImpl().bar(1l)
      

       

      The scripts are executed in Java by GroovyShell#evaluate using the following compiler configuration:

      CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
      ASTTransformationCustomizer compileStaticCustomizer = new ASTTransformationCustomizer(
          singletonMap("extensions", singletonList("net.robinfriedli.botify.scripting.TypeCheckingExtension")),
          CompileStatic.class
      );
      
      compilerConfiguration.addCompilationCustomizers(compileStaticCustomizer);
      return compilerConfiguration;
      

       

      Attachments

        Issue Links

          Activity

            People

              emilles Eric Milles
              robinfriedli Robin Friedli
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 0.5h
                  0.5h