Groovy: println vs System.out.println with GroovyInterceptable -
why need use system.out.println instead of println when use groovyinterceptable?
for example if coding in groovy file can print console typing:
println "printing console" but if want print here:
class test implements groovyinterceptable { def sum(integer x, integer y) { x + y } def invokemethod(string name, args) { system.out.println "invoke method $name args: $args" } } def test = new test() test?.sum(2,3) i have use system.out.println in method, or else stackoverflowerror. why?
update: @dan getz answer below know why happens groovyinterceptable class now. know if there other class implementations in groovy issue arise?
this because class test implements groovyinterceptable interface, according docs, is
used notify methods should intercepted through
invokemethodmechanism ofgroovyobject.
this isn't methods have been defined on class. try:
test?.total(2,3) you'll see returns
invoke method total args: [2, 3]
the call println inside invokemethod understood call this.println, call sum be. this.println calls invokemethod again, because implemented groovyinterceptable, , on.
this wouldn't happen if didn't implement groovyinterceptable. example, running following code
class test { def sum(integer x, integer y) { println "let's sum!" x + y } } def test = new test() test?.sum(2,3) will output
let's sum!
Comments
Post a Comment