[ https://issues.apache.org/jira/browse/GROOVY-6764?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15451432#comment-15451432 ]
ASF GitHub Bot commented on GROOVY-6764: ---------------------------------------- GitHub user paulk-asert opened a pull request: https://github.com/apache/groovy/pull/404 GROOVY-6764: Problem Referring To Statically Imported Constants You can merge this pull request into a Git repository by running: $ git pull https://github.com/paulk-asert/groovy groovy6764 Alternatively you can review and apply these changes as the patch at: https://github.com/apache/groovy/pull/404.patch To close this pull request, make a commit to your master/trunk branch with (at least) the following in the commit message: This closes #404 ---- commit fb1ce06d4ea4685734238875f2aad8d99549dbe6 Author: paulk <pa...@asert.com.au> Date: 2016-08-31T07:22:43Z GROOVY-6764: Problem Referring To Statically Imported Constants ---- > Problem Referring To Statically Imported Constants > -------------------------------------------------- > > Key: GROOVY-6764 > URL: https://issues.apache.org/jira/browse/GROOVY-6764 > Project: Groovy > Issue Type: Bug > Affects Versions: 2.1.9, 2.4.0-rc-1 > Reporter: Jeff Brown > Attachments: constantsquestion.zip > > > It appears that if a class implements an interface which declares constants, > Groovy code which statically imports the constant from the class (not the > interface) doesn't work. I think this is supposed to work. It does work > from Java. See the attached app in constantsquestion.zip which includes the > following code. Extract the .zip and run "./gradlew test" to see that the > Java test passes and the Groovy test fails. > {code:borderStyle=solid|title=src/main/java/com/demo/Constants.java} > package com.demo; > public interface Constants { > int ANSWER = 42; > } > {code} > {code:borderStyle=solid|title=src/main/java/com/demo/Helper.java} > package com.demo; > public class Helper implements Constants { > } > {code} > {code:borderStyle=solid|title=test/java/com/demo/JavaHelperTests.java} > package com.demo; > import org.junit.Test; > import org.junit.runner.RunWith; > import org.junit.runners.JUnit4; > import static org.junit.Assert.assertEquals; > import static com.demo.Helper.ANSWER; > @RunWith(JUnit4.class) > public class JavaHelperTests { > @Test > public void testAccessingConstant() { > // this test passes... > assertEquals(42, ANSWER); > } > } > {code} > {code:borderStyle=solid|title=test/groovy/com/demo/GroovyHelperSpec.groovy} > package com.demo > import spock.lang.Specification > import static com.demo.Helper.ANSWER > class GroovyHelperSpec extends Specification { > void 'test referring to statically imported constant'() { > // this test fails... > expect: > 42 == ANSWER > } > } > {code} > The error message from the Groovy spec is: > {noformat} > groovy.lang.MissingPropertyException: No such property: ANSWER for class: > com.demo.GroovyHelperSpec > at com.demo.GroovyHelperSpec.test referring to statically imported > constant(GroovyHelperSpec.groovy:10) > {noformat} > Note that everything works fine from Groovy if the constant is imported from > the interface instead of from the class which implements the interface. > Is this a bug? -- This message was sent by Atlassian JIRA (v6.3.4#6332)