home | career | drupal | java | mac | mysql | perl | scala | uml | unix

Groovy example source code file (OverloadInvokeMethodTest.groovy)

This example Groovy source code file (OverloadInvokeMethodTest.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

groovytestcase, groovytestcase, missingmethodexception, missingmethodexception, object, object, overloadinvokemethodtest, overloadinvokemethodtest

The Groovy OverloadInvokeMethodTest.groovy source code

package groovy

/**
 * @version $Revision: 1.4 $
 */
class OverloadInvokeMethodTest extends GroovyTestCase {
    
    void testBug() {
        def value = foo(123)
        assert value == 246
    }

    /**
     * Lets overload the invokeMethod() mechanism to provide an alias
     * to an existing method
     */
    def invokeMethod(String name, Object args) {
        try {
            return metaClass.invokeMethod(this, name, args)
        }
        catch (MissingMethodException e) {
            if (name == 'foo') {
                return metaClass.invokeMethod(this, 'bar', args)
            }
            else {
                throw e
            }
        }
    }
    
    def bar(param) {
        return param * 2
    }

}

Other Groovy examples (source code examples)

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

new blog posts

 

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