who Called Me via StackTraceElement - Java java.lang

Java examples for java.lang:Throwable

Description

who Called Me via StackTraceElement

Demo Code


//package com.java2s;

public class Main {
    public static String whoCalledMe() {
        StackTraceElement[] stackTraceElements = Thread.currentThread()
                .getStackTrace();/*from   w ww.  j  a va 2  s .  c  o  m*/
        StackTraceElement caller = stackTraceElements[4];
        String classname = caller.getClassName();
        int idx = classname.lastIndexOf(".");
        if (idx > 0)
            classname = classname.substring(++idx);
        String methodName = caller.getMethodName();
        // int lineNumber = caller.getLineNumber();
        return classname + "." + methodName;// + ":" + lineNumber;
    }
}

Related Tutorials