alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (SuperMethod2Bug.groovy)

This example Groovy source code file (SuperMethod2Bug.groovy) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

cheese, cheese, derived, groovytestcase, string, superbase, superbase, superderived, superderived, testbase, testbase, testbase2, testderived, testderived3testbase3

The Groovy SuperMethod2Bug.groovy source code

/*
 * Copyright 2003-2010 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package groovy.bugs

/**
 * @version $Revision: 20569 $
 */
 
class SuperMethod2Bug extends GroovyTestCase {
     
    void testBug() {
        def base = new SuperBase()
        def value = base.doSomething()
        assert value == "TestBase"
        
        
        base = new SuperDerived()
        value = base.doSomething()
        assert value == "TestDerivedTestBase"
    }

    void testBug2() {
        def base = new SuperBase()
        def value = base.foo(2)
        assert value == "TestBase2"
        
        
        base = new SuperDerived()
        value = base.foo(3)
        assert value == "TestDerived3TestBase3"
    }

    void testBug3() {
        def base = new SuperBase()
        def value = base.foo(2,3)
        assert value == "foo(x,y)Base2,3"
        
        
        base = new SuperDerived()
        value = base.foo(3,4)
        assert value == "foo(x,y)Derived3,4foo(x,y)Base3,4"
    }

    void testBug4() {
        def base = new SuperBase("Cheese")
        def value = base.name
        assert value == "Cheese"
        
        
        base = new SuperDerived("Cheese")
        value = base.name
        assert value == "CheeseDerived"
    }
    
    void testCallsToSuperMethodsReturningPrimitives(){
       def base = new SuperBase("super cheese")
       assert base.longMethod() == 1
       assert base.intMethod() == 1
       assert base.boolMethod() == true
       
       base = new SuperDerived("derived super cheese")
       assert base.longMethod() == 1
       assert base.intMethod() == 1
       assert base.boolMethod() == true       
    }
}

class SuperBase {
    String name

    SuperBase() {
    }
    
    SuperBase(String name) {
        this.name = name
    }
    
    def doSomething() {
        "TestBase"
    }

    def foo(param) {
        "TestBase" + param
    }
    
    def foo(x, y) {
        "foo(x,y)Base" + x + "," + y
    }
    
    boolean boolMethod(){true}
    long longMethod(){1l}
    int intMethod(){1i}
}

class SuperDerived extends SuperBase {
    
    def calls = 0
    
    SuperDerived() {
    }
    
    SuperDerived(String name) {
        super(name + "Derived")
    }
    
    def doSomething() {
        /** @todo ++calls causes bug */
        //calls++
        /*
        calls = calls + 1
        assert calls < 3
        */
        
        "TestDerived" + super.doSomething()
    }
    
    def foo(param) {
        "TestDerived" + param + super.foo(param)
    }
    
    def foo(x, y) {
        "foo(x,y)Derived" + x + "," + y + super.foo(x, y)
    }
    
    // we want to ensure that a call with super, which is directly added into 
    // bytecode without calling MetaClass does correct boxing
    boolean booMethod(){super.boolMethod()}
    int intMethod(){super.intMethod()}
    long longMethod(){super.longMethod()}
}

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy SuperMethod2Bug.groovy source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.