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