02 December 2014

If you find yourself in a situation where you cannot debug your application (from an IDE or via attaching remotely to the executing JVM), you can use a little Groovy meta class magic to add a method to each class in order to print the current stack of any given line:

this.class.classLoader.class.classes.each {
    it.metaClass.printStackTrace = { ->
        Thread.currentThread().stackTrace.each { element ->
            println "\tat ${element.className}.${element.methodName}(${element.fileName}:${element.lineNumber})"

Once the above code has been executed (preferably from your main class), you can use it anywhere in your Groovy application to print the current stack trace:

class MyClass {

    def method() {


This is particularly useful when trying to track down which of a number of multiple code points might be invoking a given method.

comments powered by Disqus