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

Misleading MissingMethodException thrown when this() is called inside a constructor

    XMLWordPrintableJSON

Details

    Description

      class Foo {
          Foo(String s) {}
      
          Foo(Date d) {
              println "call to this() must be the first statement of a ctor"
              this(d.toString())
          }
      }
      
      new Foo(new Date())
      

      In the snippet above, I'm calling this() after a println call.
      But in Java, it is mandatory that calls to this() or super() inside a constructor are the first statements of the block.
      Here I get the following stacktrace:

      groovy.lang.MissingMethodException: No signature of method: Foo.call() is applicable for argument types: (java.lang.String) values: {Fri May 16 23:48:20 CEST 2008}
      	at Foo.invokeMethod(Script35)
      	at Foo.<init>(Script35:6)
      	at Script35.run(Script35:10)
      

      The compiler should check that calls to this() or super() are done as the first statement, and not afterwards.

      Attachments

        1. Set2_2836Patches.zip
          3 kB
          Roshan Dawrani

        Activity

          People

            roshandawrani Roshan Dawrani
            guillaume Guillaume Sauthier
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: