Here is a small snippet that can be used to find the name of the currently executing method in Java.
Throwable t = new Throwable(); StackTraceElement[] elements = t.getStackTrace(); System.out.println(elements[0].getMethodName());
Create a Throwable object in your method. During the creation of a Throwable instance, the JVM stores information of the currently executing method on the stack trace. Extract the stack trace and print the name of the zeroth element to get your method name.
Here is another small snippet to find the name of the class at runtime.
Thread.currentThread().getStackTrace()[1].getClassName()
Tiny little things, aren't they!
Happy Programming :)
Signing Off
Ryan
1 comment:
Great post thanks
Post a Comment