Example usage for java.lang Throwable Throwable

List of usage examples for java.lang Throwable Throwable

Introduction

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

Prototype

public Throwable() 

Source Link

Document

Constructs a new throwable with null as its detail message.

Usage

From source file:StackTraceTest.java

/**
 * Computes the factorial of a number/*  w  w  w  .j a v a 2 s  .  c  o m*/
 * @param n a nonnegative integer
 * @return n! = 1 * 2 * . . . * n
 */
public static int factorial(int n) {
    System.out.println("factorial(" + n + "):");
    Throwable t = new Throwable();
    StackTraceElement[] frames = t.getStackTrace();
    for (StackTraceElement f : frames)
        System.out.println(f);
    int r;
    if (n <= 1)
        r = 1;
    else
        r = n * factorial(n - 1);
    System.out.println("return " + r);
    return r;
}

From source file:Main.java

public static String getCallLocation(String fqn) {
    if (!java13) {
        try {/* ww  w.  ja  v  a  2s  .  c o  m*/
            StackTraceElement[] ste = new Throwable().getStackTrace();
            for (int i = 0; i < ste.length - 1; i++) {
                if (fqn.equals(ste[i].getClassName())) {
                    StackTraceElement callLocation = ste[i + 1];
                    String nextClassName = callLocation.getClassName();
                    if (nextClassName.equals(fqn)) {
                        continue;
                    }
                    return callLocation.toString();
                }
            }
        } catch (Throwable e) {
            java13 = true;
        }
    }
    return null;
}

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

/**
 * Return the method who make the call.//from  ww  w.  j a  v  a  2  s .  co  m
 */
public static StackTraceElement whoCalledMe() {
    // 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) {
        return trace[2];
    }

    return null;
}

From source file:Main.java

private static String getCaller(String tag) {
    String caller = "<unknown>";
    StackTraceElement[] trace = new Throwable().fillInStackTrace().getStackTrace();
    if (trace.length < 6) {
        return caller;
    }/*from   w  ww. j ava 2 s . c o  m*/
    // Walk up the stack looking for the first caller outside of LogUtils.
    // It will be at least 5 frames up, so start there.
    for (int i = 5; i < trace.length; i++) {
        // Class<?> clazz = trace[i].getClass();
        String clazzName = trace[i].getClassName();
        if (!clazzName.contains("Log")) {
            String callingClass = clazzName;
            callingClass = callingClass.substring(callingClass.lastIndexOf('.') + 1);
            callingClass = callingClass.substring(callingClass.lastIndexOf('$') + 1);
            caller = callingClass + "." + trace[i].getMethodName();
            break;
        }
    }
    return String.format(Locale.US, "[%d/%s] %s %s: %s", Thread.currentThread().getId(),
            Thread.currentThread().getName(), LOG_PREFIX, tag, caller);
}

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)//from   w  ww  .  ja v  a2  s.com
 */
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:org.b3log.latke.util.Callstacks.java

/**
 * Checks the current method is whether invoked by a caller specified by the given class name and method name.
 * /*from  w w  w . j a  v  a  2s.c om*/
 * @param className the given class name
 * @param methodName the given method name, "*" for matching all methods
 * @return {@code true} if it is invoked by the specified caller, returns {@code false} otherwise
 */
public static boolean isCaller(final String className, final String methodName) {
    final Throwable throwable = new Throwable();
    final StackTraceElement[] stackElements = throwable.getStackTrace();

    if (null == stackElements) {
        LOGGER.log(Level.WARNING, "Empty call stack");

        return false;
    }

    final boolean matchAllMethod = "*".equals(methodName);

    for (int i = 1; i < stackElements.length; i++) {
        if (stackElements[i].getClassName().equals(className)) {
            return matchAllMethod ? true : stackElements[i].getMethodName().equals(methodName);
        }
    }

    return false;
}

From source file:com.example.ryan.weixindemo.util.DebugLog.java

public static void e(String message) {
    if (!isDebuggable())
        return;/*from www .ja  v a 2s . com*/

    // Throwable instance must be created before any methods  
    getMethodNames(new Throwable().getStackTrace());
    Log.e(className, createLog(message));
}

From source file:com.example.ryan.weixindemo.util.DebugLog.java

public static void i(String message) {
    if (!isDebuggable())
        return;// w  w w  . j a v  a 2 s.  com

    getMethodNames(new Throwable().getStackTrace());
    Log.i(className, createLog(message));
}

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

/**
 * Return the method who make the call./*from   w  ww.  j  a  v  a 2s . c  o m*/
 */
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() });
            }
        }
    }
}

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

/**
 * Return the method who make the call.//from   w w w  . j  a v a  2s .  com
 */
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.openkm")) {
                StackTraceElement sse = trace[i];
                log.warn("{} -> {} ({}:{})", new Object[] { sse.getClassName(), sse.getMethodName(),
                        sse.getFileName(), sse.getLineNumber() });
            }
        }
    }
}