Possible Duplicate:
Getting the name of the current executing method
Is there a way in Java
for a given method to know its own name? If so, how can it be referenced from inside the method?
Possible Duplicate:
Getting the name of the current executing method
Is there a way in Java
for a given method to know its own name? If so, how can it be referenced from inside the method?
You can determine it by analyzing stack trace. But it may carry quite significant performance penalty. In AOP universe you can also have aspect that will determine method name and store it in some kind of context.
In stack trace method you would do something like
Exception e = new Exception();
e.fillInStackTrace();
e.getStackTrace()[0].getMethodName();
or (as suggested in comment)
Thread.currentThread().getStackTrace()[0].getMethodName();
The first element of the stack trace (Thread.currentThread().getStackTrace()
, see this question) should tell you in which method you currently are at runtime.