I know that stacktraces can be used to retrieve current executing method with something like this:
Thread.currentThread().getStackTrace()[what you want]
However not every JVM provides this stack trace so this way of getting a method's name is pretty much unreliable. Anyway...what about exceptions? My need is to log the caller method name and, in order to provide some code maintenance, I'd like to avoid to just write that name down like this:
public void myMethod(){
...
}catch(anException ex){
log.error("myMethod failed!");
}
But rather something like:
}catch(anException ex){
log.error("{} failed!", ex.getStackTrace()[0].getMethodName()); //0 should be my method, the caller
}
Is this reliable? I think that, at least for exceptions, JVM should be expected to provide said stack trace...