Friday, July 31, 2015

Get Current method name (Java)

There are classes that it is important to debug when a method starts and when a method ends.
Since there will be several methods you can get the mehtod name with java code so you just use the same line to log where your code is being executed.

in order to do this, we need to get the method and the class name for the current method

This is done by calling the currentThread Statck so we can get the name for the method as follows:


As you can see the currentThread().getStackTrace()[2] will be the thread that called getCurrentMethodLogMessage() that is why is on index [2]

In order to call this you can do as follows:

You can get a reference for this at http://stackoverflow.com/questions/442747/getting-the-name-of-the-current-executing-method
Post a Comment