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

Groovy classes can't see package-local properties from a super class

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.0-beta-1, 1.8.6
    • 2.4.8
    • Compiler
    • None

    Description

      If I have a standard gradle project setup (attached):

      build.gradle
      src/
        main/
          java/
            test/
              AbstractBase.java
              SamePackageJavaBase.java
          groovy/
            test/
              GroovyBase.groovy
              GroovyBaseWithSuper.groovy
      

      I have a single Abstract base class:

      AbstractBase.java
      package test ;
      
      public abstract class AbstractBase {
        int base ;
      
        public AbstractBase( int base ) {
          this.base = base ;
        }
      
        public abstract int mult( int n ) ;
      }
      

      And a class GroovyBase that extends this class:

      GroovyBase.groovy
      package test
      
      public class GroovyBase extends AbstractBase {
        public GroovyBase( int n ) {
          super( n )
        }
      
        public int mult( int n ) {
          n * base
        }
      
        static main( args ) {
          println new GroovyBase( 10 ).mult( 3 )
        }
      }
      

      When running this (using the Gradle script in the attachment), I get the exception:

      Exception in thread "main" groovy.lang.MissingPropertyException: No such property: base for class: test.GroovyBase
      Possible solutions: class
      

      Changing the line

          n * base
      

      to

          n * super.base
      

      Or making the base field public in the AbstractBase class makes it work.

      It's as if the classes are considered to be in different packages for some things, but not for others.

      Thinking about it, not sure if this is a Gradle bug or a Groovy cross compiler one.

      To run the tests, unpack the attachment, and run:

      # Test the above failing example
      gradle -Pmain=test.GroovyBase
      # Test the addition of super.base
      gradle -Pmain=test.GroovyBaseWithSuper
      # Test the Java extension of the Abstract class
      gradle -Pmain=test.SamePackageJavaBase
      

      You can change the groovy version from 1.8.6 by passing (for example) -Pgroovy=groovy=2.0.0-beta-2

      Attachments

        1. FailingAbstractSuper.tgz
          0.9 kB
          Tim Yates

        Issue Links

          Activity

            People

              paulk Paul King
              tim_yates Tim Yates
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: