Example usage for java.lang Throwable getStackTrace

List of usage examples for java.lang Throwable getStackTrace

Introduction

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

Prototype

public StackTraceElement[] getStackTrace() 

Source Link

Document

Provides programmatic access to the stack trace information printed by #printStackTrace() .

Usage

From source file:Main.java

private static String getDebugInfo() {
    Throwable stack = new Throwable().fillInStackTrace();
    StackTraceElement[] trace = stack.getStackTrace();
    int n = 2;/* www .j  ava 2 s  .  c  o m*/
    return trace[n].getClassName() + " " + trace[n].getMethodName() + "()" + ":" + trace[n].getLineNumber()
            + " ";
}

From source file:org.eurekastreams.commons.logging.LogFactory.java

/**
 * Make a log for the calling class./*from   w  ww.  j  a v  a2s  .c  o  m*/
 *
 * @return a log for the calling class
 */
public static Log make() {
    Throwable t = new Throwable();
    StackTraceElement directCaller = t.getStackTrace()[1];
    return org.apache.commons.logging.LogFactory.getLog(directCaller.getClassName());
}

From source file:org.eurekastreams.commons.logging.LogFactory.java

/**
 * @return The class name of the calling class.
 *//*from   w w  w  . j a  v  a 2 s .  c o m*/
public static String getClassName() {
    Throwable t = new Throwable();
    StackTraceElement directCaller = t.getStackTrace()[1];
    return directCaller.getClassName();
}

From source file:Main.java

public static String getStackTraceAsString(Throwable e) {
    String stackTraceString = e.getClass().getName() + ": " + e.getMessage();
    for (StackTraceElement ste : e.getStackTrace()) {
        stackTraceString += "\n" + ste.toString();
    }/* w w  w.  j  a va  2  s  .  c  om*/
    return stackTraceString;
}

From source file:Main.java

/**
 * Like {@link #sprintFullStack(Throwable)}, renders a stacktrace as a String for logging,
 * but excludes stack trace elements common with the calling scope, which may
 * be considered irrelevant to application functionality testing
 * @param t an exception//from   w  ww .j a v a  2 s .c o  m
 * @return a string representation of a shorter stack trace
 */
public static String sprintShortStack(final Throwable t) {
    if (t == null) {
        return "";
    }

    StringWriter writer = new StringWriter();
    Throwable local = new Throwable();

    StackTraceElement[] localStack = local.getStackTrace();
    StackTraceElement[] tStack = t.getStackTrace();

    int m = tStack.length - 1, n = localStack.length - 1;
    while (m >= 0 && n >= 0 && tStack[m].equals(localStack[n])) {
        m--;
        n--;
    }

    int framesInCommon = tStack.length - 1 - (m + 1);

    t.setStackTrace(Arrays.copyOf(t.getStackTrace(), m + 1, StackTraceElement[].class));
    t.printStackTrace(new PrintWriter(writer));
    t.setStackTrace(tStack);
    StringBuilder sb = new StringBuilder(writer.toString());
    if (framesInCommon != 0) {
        sb.append("\t... " + framesInCommon + " more");
    }
    return sb.toString();
}

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

public static List<String> getStackTraces(Throwable throwable) {
    StackTraceElement[] stElements = throwable.getStackTrace();
    if (stElements == null)
        return null;
    List<String> stList = new ArrayList<String>(stElements.length);
    for (StackTraceElement stElement : stElements)
        stList.add(stElement.toString());
    return stList;
}

From source file:com.p000ison.dev.simpleclans2.exceptions.handling.ExceptionReport.java

private static Object buildThrowableJSON(Throwable thrown) {
    JSONArray stackTrace = new JSONArray();

    for (StackTraceElement element : thrown.getStackTrace()) {
        stackTrace.add(element.toString());
    }//from  w  w w  .  j a  v  a  2  s.  c  o m

    return stackTrace;
}

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.
 * /* w w w.  j a v a2  s .co  m*/
 * @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:Main.java

public static void logToFile(Throwable e) {
    try {/*ww  w.  j  a va2 s .  c  o m*/
        StringBuffer sb = new StringBuffer(e.toString() + "\n");
        StackTraceElement[] stElements = e.getStackTrace();
        String newLine = "";

        for (StackTraceElement stElement : stElements) {
            sb.append(newLine);
            sb.append("\tat ");
            sb.append(stElement.toString());
            newLine = "\n";
        }
    } catch (Exception ee) {
        e.printStackTrace();
    }
}

From source file:org.projectforge.common.ExceptionHelper.java

/**
 * Gets the stackTrace of the given message in the form "at
 * org.projectforge.access.AccessChecker.hasPermission(AccessChecker.java:79) at
 * org.projectforge.task.TaskDao.hasInsertAccess(TaskDao.java:176) at ..." without entries does not match the beginning of
 * only4Namespace. <br/> Also stack trace entries containing "CGLIB$$" will be ignored in the output.
 * @param ex/*w ww. j ava  2s.c o m*/
 * @param only4Namespace
 * @return
 */
public static String getFilteredStackTrace(Throwable ex, String only4Namespace) {
    StringBuffer buf = new StringBuffer();
    StackTraceElement[] sta = ex.getStackTrace();
    boolean ignored = false;
    if (sta != null && sta.length > 0) {
        for (StackTraceElement ste : sta) {
            if (ignored == true) {
                if (ignore(ste.getClassName(), only4Namespace) == true) {
                    continue;
                }
            } else {
                ignored = false;
            }
            if (ignore(ste.getClassName(), only4Namespace) == true) {
                buf.append(" at ...");
                ignored = true;
                continue;
            }
            buf.append(" at ");
            buf.append(ste.toString());
        }
    }
    return buf.toString();
}