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

Late and superfluous setting of default values on primitives in subclasses

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.0, 1.1-beta-3, 1.1-rc-1, 1.1-rc-2, 1.1-rc-3
    • 1.6-rc-1, 1.5.8, 1.7-beta-1
    • class generator
    • None
    • Tested Linux jdk1.5.0_13, 1.6.0_03, 1.6.0_10

    Description

         b=new B(42)
         println "finally, intA=$b.intA, strA=$b.strA"
         println "         intB=$b.intB, strB=$b.strB"
      
         class A {
            int intA
            String strA
      
            A(int a) {
               intA=a
               strA=intA as String
               println "in A.<init> intA=$intA strA=$strA"
               config()
            }
      
            void config() {
               println "in A.config intA=$intA strA=$strA"
            }
         }
      
         class B extends A{
            int intB
            String strB
      
            B(int a) {
               super(a)
               println "in B.<init> intB=$intB strB=$strB"
            }
      
            void config() {
               super.config()
               intB=intA-1
               strB=intB as String
               println "in B.config intB=$intB strB=$strB"
            }
      
         }
      

      The output is as follows:

      in A.<init> intA=42 strA=42
      in A.config intA=42 strA=42
      in B.config intB=41 strB=41
      in B.<init> intB=0 strB=41
      finally, intA=42, strA=42
      intB=0, strB=41

      As rtayek@ca.rr.com pointed out already:

      > calling a virtual config from a ctor seems a bit strange, but it seems to work in java. it may be a boxing issue. decompiling B.class, the ctor has:
      >
      > Integer integer = new Integer(0);
      > integer;
      > intB = DefaultTypeTransformation.intUnbox(integer);
      >
      > in it after the call to super.

      So I would state this as a bug. Rather major in my opinion, either.

      Attachments

        Activity

          People

            blackdrag Jochen Theodorou
            patric Patric Lichtsteiner
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: