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

Wrong Method Being Invoked - Related To Static Imports

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.8.6, 2.4.0-beta-3
    • 2.5.1
    • Compiler

    Description

      When a static import brings in a method which conflicts with a method defined in a class' super class Java invokes the method from the super class but Groovy invokes the method from the static import.

      src/main/java/com/demo/Utils.java
      package com.demo;
      
      public class Utils {
          
          public static String getValue() {
              return "Utils.getValue";
          }
      }
      
      src/main/java/com/demo/MyBaseClass.java
      package com.demo;
      
      public class MyBaseClass {
          
          public static String getValue() {
              return "MyBaseClass.getValue";
          }
      }
      
      src/main/java/com/demo/JavaSubClass.java
      package com.demo;
      
      import static com.demo.Utils.*;
      
      public class JavaSubClass extends MyBaseClass {
          
          public String retrieveValue() {
              return getValue();
          }
      }
      
      src/main/groovy/com/demo/GroovySubClass.groovy
      package com.demo
      
      import static com.demo.Utils.*
      
      class GroovySubClass extends MyBaseClass {
          
          String retrieveValue() {
              getValue()
          }
      }
      
      src/test/groovy/com/demo/ImportTests.groovy
      package com.demo
      
      class ImportTests extends GroovyTestCase {
          
          void testJava() {
              def subClass = new JavaSubClass()
              def result = subClass.retrieveValue()
              assertEquals 'MyBaseClass.getValue', result
          }
          
          void testGroovy() {
              def subClass = new GroovySubClass()
              def result = subClass.retrieveValue()
              assertEquals 'MyBaseClass.getValue', result
          }
      }
      

      The Java test passes but the Groovy test fails.

      To run the code download the attached project (staticimport.zip) and run "./gradlew test".

      Attachments

        1. staticimport.zip
          41 kB
          Jeff Brown

        Activity

          People

            emilles Eric Milles
            brownj Jeff Brown
            Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: