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

Groovy example source code file (GroovyInterceptableTest.groovy)

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

gi, gi, gi2, gi2, groovyinterceptable, groovyinterceptable, groovyinterceptabletest, groovytestcase, missingmethodexception, object, object, string

The Groovy GroovyInterceptableTest.groovy source code

package groovy

import org.codehaus.groovy.runtime.ReflectionMethodInvoker

class GroovyInterceptableTest extends GroovyTestCase {

    void testMethodInterception() {
        def g = new GI()
        assert g.someInt() == 2806
        assert g.someUnexistingMethod() == 1
        assert g.toString() == "invokeMethodToString"
    }

    void testProperties() {
        def g = new GI()
        assert g.foo == 89
        g.foo = 90
        assert g.foo == 90
        // should this be 1 or 90?
        assert g.getFoo() == 1
    }
    
    void testCallMissingMethod() {
        def obj = new GI2()
        shouldFail { obj.notAMethod() }
        assert 'missing' == obj.result 
    }
 
    void testCallMissingMethodFromInstance() {
        def obj = new GI2()
        shouldFail { obj.method() }
        assert 'missing' == obj.result
   }
}

class GI implements GroovyInterceptable {

    def foo = 89

    int someInt() { 2806 }
    String toString() { "originalToString" }

    Object invokeMethod(String name, Object args) {
        if ("toString" == name)
            return "invokeMethodToString"
        else if ("someInt" == name)
            return ReflectionMethodInvoker.invoke(this, name, args)
        else
            return 1
    }
}


class GI2 implements GroovyInterceptable {
  def result = ""
  def invokeMethod(String name, args) {
    def metaMethod = Foo.metaClass.getMetaMethod(name, args)
    if (metaMethod != null) return metaMethod.invoke(this, args)
    result += "missing"
    throw new MissingMethodException(name, Foo.class, args)
  }
  
  def method() {
      notAMethod()
  }
}

Other Groovy examples (source code examples)

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



my book on functional programming

 

new blog posts

 

Copyright 1998-2019 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.