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

reference inner java classes

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 1.0-JSR-4
    • None
    • None

    Description

      Working with inner java classes, there are two different issues which I feel belong together.

      Consider this java class which I will reference from a groovy script:

      package foogroovy;
      public class Outer
      {
      public Outer() {}
      public static class Inner
      {
      public Inner() {}
      }
      }

      Problem #1: making the script compile

      The following script does not compile due to a MissingClassException.
      It ONLY works when the inner class is referenced and imported as Outer$Inner.

      //--------------------------------------------------------------------
      import foogroovy.Outer;
      // import foogroovy.Outer$Inner;
      // this is the only way to make at least case 2 compile

      class UseInner1
      {
      static void main( String[] args )

      { // case 1: Outer.Inner inner = new Outer.Inner(); // case 2: Outer$Inner inner = new Outer$Inner(); // both deliver: groovy.lang.MissingClassException: No such class: Outer in cast for class: UseInner2. At [-1:-1] C:\src\foogroovy\UseInner2.groovy }

      }

      Problem #2: getting the correct class

      Problem #1 can be solved by importing foogroovy.*;. This leads to another issue. Outer.Inner.class seems to be a java.lang.class!!

      //--------------------------------------------------------------------
      import foogroovy.*;

      class UseInner2
      {
      static void main( String[] args )

      { Outer.Inner inner = new Outer.Inner(); println Outer.Inner.class // gives(!): 'class java.lang.class' println Outer$Inner.class // gives: 'class foogroovy.Outer$Inner' println Outer.class // gives: 'class foogroovy.Outer' }

      }

      Attachments

        Activity

          People

            blackdrag Jochen Theodorou
            herkules Joerg Plewe
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: