Example usage for java.lang StackTraceElement getClassName

List of usage examples for java.lang StackTraceElement getClassName

Introduction

In this page you can find the example usage for java.lang StackTraceElement getClassName.

Prototype

public String getClassName() 

Source Link

Document

Returns the fully qualified name of the class containing the execution point represented by this stack trace element.

Usage

From source file:me.j360.dubbo.modules.util.concurrent.ThreadUtil.java

/**
 * StackTrace??./*w  w  w  . java2s . c o m*/
 */
public static String getCallerClass() {
    StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace();
    if (stacktrace.length >= 4) {
        StackTraceElement element = stacktrace[3];
        return element.getClassName();
    } else {
        return StringUtils.EMPTY;
    }
}

From source file:me.j360.dubbo.modules.util.concurrent.ThreadUtil.java

/**
 * StackTrace"??.??()"/*w w  w . j a  v  a2 s  .c  o m*/
 */
public static String getCallerMethod() {
    StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace();
    if (stacktrace.length >= 4) {
        StackTraceElement element = stacktrace[3];
        return element.getClassName() + '.' + element.getMethodName() + "()";
    } else {
        return StringUtils.EMPTY;
    }
}

From source file:me.j360.dubbo.modules.util.concurrent.ThreadUtil.java

/**
 * StackTrace??.//from  w  w w .  j  a v  a  2  s .c o m
 */
public static String getCurrentClass() {
    StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace();
    if (stacktrace.length >= 3) {
        StackTraceElement element = stacktrace[2];
        return element.getClassName();
    } else {
        return StringUtils.EMPTY;
    }
}

From source file:me.j360.dubbo.modules.util.concurrent.ThreadUtil.java

/**
 * StackTrace?"??.??()"/*from w w  w .  j a v  a2 s.  c  om*/
 */
public static String getCurrentMethod() {
    StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace();
    if (stacktrace.length >= 3) {
        StackTraceElement element = stacktrace[2];
        return element.getClassName() + '.' + element.getMethodName() + "()";
    } else {
        return StringUtils.EMPTY;
    }
}

From source file:Main.java

private static String generateTag() {
    StackTraceElement caller = new Throwable().getStackTrace()[2];
    String tag = "%s.%s(L:%d)";
    String callerClazzName = caller.getClassName();
    callerClazzName = callerClazzName.substring(callerClazzName.lastIndexOf(".") + 1);
    tag = String.format(tag, callerClazzName, caller.getMethodName(), caller.getLineNumber());
    tag = TextUtils.isEmpty(customTagPrefix) ? tag : customTagPrefix + ":" + tag;
    return tag;/*  w w  w.ja v a 2 s. c  o m*/
}

From source file:Main.java

/**
 * Formats a stack trace into a single line that provides relevant information for debugging
 * @param element the element to format//from  w w w.ja  v  a 2 s  .c  o  m
 * @return a well-formatted stack-trace line containing the class name, method name, and line number
 * that, when clicked in the logcat, will display the line or source from where the message originated.
 */
public static String formatStackTrace(StackTraceElement element) {
    StringBuilder b = new StringBuilder();

    b.append(" at ");
    String clazz = element.getClassName();
    b.append(clazz).append(".");
    b.append(element.getMethodName()).append("(");
    b.append(clazz.substring(clazz.lastIndexOf(".") + 1)).append(".java:");
    b.append(element.getLineNumber()).append(")").append(" , ##");
    return b.toString();
}

From source file:Main.java

private static String getFunctionName() {
    StackTraceElement[] sts = Thread.currentThread().getStackTrace();
    if (sts == null) {
        return null;
    }//  ww  w  . jav a 2 s  .c  o  m
    for (StackTraceElement st : sts) {
        if (st.isNativeMethod()) {
            continue;
        }
        if (st.getClassName().equals(Thread.class.getName())) {
            continue;
        }
        if (st.getFileName().equals("LogUtils.java")) {
            continue;
        }
        return "[" + Thread.currentThread().getName() + "(" + Thread.currentThread().getId() + "): "
                + st.getFileName() + ":" + st.getLineNumber() + "]";
    }
    return null;
}

From source file:com.qwazr.utils.ExceptionUtils.java

public final static String getLocation(StackTraceElement[] stackTrace, String prefix) {
    for (StackTraceElement element : stackTrace)
        if (element.getClassName().startsWith(prefix))
            return element.toString();
    return null;// www  .ja  v  a2 s. co  m
}

From source file:ClassUtil.java

/**
 * Infer the calling frame, skipping the given classes if so provided.
 * Code was derived from a private method found in the JDK Logger class
 *
 * @param skippedClasses the classes to skip
 * @return the frame found, just before the skipped classes (or just before
 * the caller of this method)/*w  ww  .  ja  v  a2s  .  c  om*/
 */
public static StackTraceElement getCallingFrame(Class... skippedClasses) {
    // Get the current stack trace.
    StackTraceElement[] stack = (new Throwable()).getStackTrace();

    // Simple case, no classes to skip, just return the caller of the caller
    if (skippedClasses.length == 0) {
        return stack[2];
    }

    // More complex case, return the caller, just before the skipped classes
    // First, search back to a method in the skipped classes, if any
    int ix;
    searchingForSkipped: for (ix = 0; ix < stack.length; ix++) {
        StackTraceElement frame = stack[ix];
        String cname = frame.getClassName();

        for (Class skipped : skippedClasses) {
            if (cname.equals(skipped.getName())) {
                break searchingForSkipped;
            }
        }
    }

    // Now search for the first frame before the skipped classes
    searchingForNonSkipped: for (; ix < stack.length; ix++) {
        StackTraceElement frame = stack[ix];
        String cname = frame.getClassName();

        for (Class skipped : skippedClasses) {
            if (cname.equals(skipped.getName())) {
                continue searchingForNonSkipped;
            }
        }

        // We've found the relevant frame.
        return frame;
    }

    // We haven't found a suitable frame
    return null;
}

From source file:com.ikon.util.StackTraceUtils.java

/**
 * Return the method who make the call./* w  w  w . j  av a2  s .  c om*/
 */
public static void logTrace(Logger log) {
    // The constructor for Throwable has a native function that fills the stack trace.
    StackTraceElement[] trace = (new Throwable()).getStackTrace();

    // Once you have the trace you can pick out information you need.
    if (trace.length >= 2) {
        for (int i = 2; i < trace.length; i++) {
            if (trace[i].getClassName().startsWith("com.ikon")) {
                StackTraceElement sse = trace[i];
                log.warn("{} -> {} ({}:{})", new Object[] { sse.getClassName(), sse.getMethodName(),
                        sse.getFileName(), sse.getLineNumber() });
            }
        }
    }
}