Java: How to get the caller function name

You could try

StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace();
StackTraceElement e = stacktrace[2];//maybe this number needs to be corrected
String methodName = e.getMethodName();

Here is code that is more modern (available in Java 9+) and better performing.

private static String getCallerMethodName()
{
   return StackWalker.
      getInstance().
      walk(stream -> stream.skip(1).findFirst().get()).
      getMethodName();
}

Change skip(1) to a larger number as needed to go higher on the stack.

This performs better than Thread.currentThread().getStackTrace() because it does not walk the entire stack and allocate all of the stack frames. It only walks two frames on the stack.

This method can be adapted to return StackWalker.StackFrame that has a lot of information about the method.