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

Compiler generates invalid inner class constructor

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 2.4.0
    • Fix Version/s: 2.4.7
    • Component/s: class generator, Compiler
    • Labels:
      None

      Description

      replacement description:

      Intf.groovy
      interface Intf {
        def foo = { "bar" }
      }
      

      Will create an inner class that lacks the static modifier for the inner class table. Compare with

      JavaInterface.java
      public interface JavaInterface {
        class NestedInInterface {}
      }
      

      for reference

      Original description:
      The JLS specifies that an implicitly-declared constructor of a non-private inner class "implicitly declares one formal parameter representing the immediately enclosing instance of the class" (Section 8.8.9, see also 8.8.1).

      Intf.groovy
      interface Intf {
        def foo = { "bar" }
      }
      

      The above code creates an inner class (Intf$1, not exactly sure what it's for) with a default constructor that has no parameters:

      $ javap -p -classpath classes 'Intf$1'
      Compiled from "Intf.groovy"
      class Intf$1 implements groovy.lang.GroovyObject {
        ...
        public Intf$1();
        ...
      }
      

      While not a major issue, this non-conformance can break interoperability with anything that expects to work with Java classes. This particular example came up while trying to use a similarly-declared Groovy class from a Scala class, where the Scala compiler was unable to parse the generated inner class.

      For reference, here is an example Java Inner class and it's compiled representation. Note the constructor parameter on the inner class.

      JavaObj.java
      public class JavaObj {
        public class Inner {}
      }
      
      $ javap -p -classpath classes 'JavaObj$Inner'
      public class JavaObj$Inner {
        final JavaObj this$0;
        public JavaObj$Inner(JavaObj);
      }
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                shils Shil Sinha
                Reporter:
                alewando Adam Lewandowski
              • Votes:
                1 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: