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

Trait Instance Init Block causes Runtime exception for Trait fields with default values

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Won't Fix
    • Affects Version/s: 2.5.5
    • Fix Version/s: None
    • Component/s: Compiler, groovy-runtime
    • Labels:
      None

      Description

      With ref. to GROOVY-8880, when instance or static field has default value in Trait, when same Trait has Instance Init Block, Runtime exception happens.

      Example code:

      trait Trait {
          Integer fieldWithDefaultValue = 0
          {
              System.out.println("Only instance init block gives this problem")
          }
      }
      class ClassWithTrait implements Trait {
      }
      new ClassWithTrait().fieldWithDefaultValue
      

      Runtime exception:

      groovy.lang.MissingMethodException: No signature of method: java.lang.Integer.call() is applicable for argument types: (Trait$Trait$Helper$__init__closure1) values: [Trait$Trait$Helper$__init__closure1@5833d058]
      Possible solutions: wait(), abs(), any(), wait(long), each(groovy.lang.Closure), any(groovy.lang.Closure)
      
      	at Trait$Trait$Helper.$init$(ConsoleScript80:3)
      
      	at ClassWithTrait.<init>(ConsoleScript80)
      
      	at ConsoleScript80.run(ConsoleScript80:9)
      

      Note: It does not matter if field is static of instance.
      Note 2: Static Init Block in Trait does not cause same issue and works Ok.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                anton.pryamostanov Anton Pryamostanov
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: