Groovy web console

subscribe to the feed Subscribe
to this
site

MOP example

Published 1 month ago by osoco with tags mop
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
Integer.metaClass.invokeMethod = { String name, args ->
    System.out.println("Call to $name intercepted on $delegate... ")
    def validMethod = Integer.metaClass.getMetaMethod(name, args)
    if (validMethod) {
        if (delegate < 5) {
            def result = validMethod.invoke(delegate, args) // delegating
            result
        } else {
            throw new java.lang.reflect.InvocationTargetException();
        }
    } else {
        Integer.metaClass.invokeMissingMethod(delegate, name, args)
    }
}

println 2.floatValue()
println 1 << 3

try {
    println 6.intValue()
} catch (java.lang.reflect.InvocationTargetException ex) {
    println ex
}

try {
    println 5.unknown()
} catch (Exception ex) {
    println ex
}