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

A Java compile time error results in a runtime exception in Groovy

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 2.3.4, 2.4.0-rc-1
    • 2.5.0-alpha-1
    • Static compilation
    • None
    • Window 7

    Description

      A Groovy class with CompileStatic annotation exhibit a different behavior than Java. The following code results in a compile time error in Java, but throws a runtime exception in Groovy.

      @CompileStatic
      class Base
      {
        String getData() { return "ABCD" }
        Base() { this(getData()) } // Calling an instance method before the class is constructed
      
        Base(String arg) {}
      }
      
      @CompileStatic
      public class GMain
      {
        public static void main(String[] args)
        {
          Base b = new Base();
        }
      }
      

      Java produces:

      Java Compiler error: Error:(11, 11) java: cannot reference this before supertype constructor has been called
      

      Groovy produces:

      Groovy Runtime Error details:
      Exception in thread "main" java.lang.ClassCastException: java.lang.Class cannot be cast to main.groovy.Base
      	at main.groovy.Base.<init>(Base.groovy:9)
      	at main.groovy.Derived.<init>(Derived.groovy)
      	at main.groovy.GMain.main(Base.groovy:18)
      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              ......
      

      Attachments

        Issue Links

          Activity

            People

              paulk Paul King
              harminder Harminder Singh
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: