I explain with an example what I try to achieve.
I have the following method and I want to get just (without additional information) how it is called inside the method itself.
Example with the String "foo" harcoded.
public void foo(){
System.out.println("foo");
}
What I need is to obtain the same thing but using some internal method of the java libraries and that does not include additional information.
You can get the method name by calling
getMethodName()
from the ClassStackTraceElement
.You can even get the method names in descending order of call, as the call to
getAllMethods();
inside shows in the examplefoo()
:Code:
VER DEMO EN REXTESTER
Result:
Yes you can get the name of the method with the following statement:
Now, referring to this SO answer , every time you execute that function it will get you the entire class to, from that class, get the method you are using, and that can cause performance problems since it saves the entire class In Memory. If you are only going to use this method on a very specific occasion, it should not cause memory problems.
If you call this method inside another method you will get the name of the currently executing method:
Here is your example:
Will print: