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

Problem With Static Fields Defined In A Trait

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.3.8
    • 2.3.9, 2.4.0-rc-1
    • None
    • None

    Description

      A problem occurs when a static method defined in a trait attempts to retrieve the value of a static field without using "this". Note that assigning a value to the static field appears to work but retrieving the value does not.

      The attached app contains the following code.

      src/demo/SomeTrait.groovy
      package demo
      
      trait SomeTrait {
          private static String someString
      
          static String getStringValue() {
              if(someString == null) {
                  someString = 'Default Value'
              }
              someString
          }
      
          static String getStringValueUsingThis() {
              if(this.someString == null) {
                  someString = 'Default Value'
              }
              this.someString
          }
      }
      
      src/demo/SomeClass.groovy
      package demo
      
      class SomeClass implements SomeTrait {}
      
      test/demo/SomeClassTest.groovy
      package demo
      
      class SomeClassTest extends GroovyTestCase {
      
          void testStaticPropertyAccess() {
              // this fails
              assert 'Default Value' == SomeClass.stringValue
          }
      
          void testStaticPropertyAccessUsingThis() {
              // this passes
              assert 'Default Value' == SomeClass.stringValueUsingThis
          }
      }
      

      The test fails with the following:

      groovy.lang.MissingMethodException: No signature of method: java.lang.Class.demo_SomeTrait__someString$get() is applicable for argument types: () values: []
      	at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:56)
      	at org.codehaus.groovy.runtime.callsite.StaticMetaClassSite.call(StaticMetaClassSite.java:52)
      	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
      	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
      	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)
      	at demo.SomeTrait$Trait$Helper.getStringValue(SomeTrait.groovy:7)
      	at demo.SomeTrait$Trait$Helper$getStringValue$0.call(Unknown Source)
      	at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
      	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
      	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
      	at demo.SomeClass.getStringValue(SomeClass.groovy)
      

      Attachments

        1. statictraitproperty.zip
          50 kB
          Jeff Brown

        Activity

          People

            melix Cédric Champeau
            brownj Jeff Brown
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: