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:com.openkm.util.StackTraceUtils.java

/**
 * Return the method who make the call./*from www.  ja  v  a2s  .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() });
            }
        }
    }
}

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 w  w .j  av a2s . co  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();
}

From source file:Main.java

public static String getLocation(int back) {
    try {//from  w  ww  .  j a va 2 s  . co m
        // Use Throwable to obtain stack trace, using Thread.currentThread().getStackTrace is expensive because of thread-safety */
        StackTraceElement e = (new Throwable()).getStackTrace()[back];
        if (e.isNativeMethod()) {
            return " [<native>]";
        }
        return " [" + e.getClassName().split("\\$")[0].replace('.', '/') + ".java:" + e.getLineNumber() + "]";
    } catch (ArrayIndexOutOfBoundsException e) {
    } catch (SecurityException e) {
    }
    return " [<unknown>]";
}

From source file:ro.bmocanu.zendo.ZendoCore.java

private static String discoverTestClassInStackTrace(StackTraceElement[] stackTrace) {
    // the 4th element in this stack trace should be the testing class
    // (0=dumpThreads, 1=getStackTrace, 2=testThis())
    // for most cases it is better to assume that the last element in the stack trace is our
    // class//  ww w  .j av  a  2s.  c  o  m
    // StackTraceElement testStackElement = stackTrace[stackTrace.length - 1];
    // String testClassName = testStackElement.getClassName();
    for (StackTraceElement element : stackTrace) {
        String className = element.getClassName();
        if (className.startsWith(ZendoConstants.ZENDO_TEST_PACKAGE_PREFIX)) {
            return className;
        }
    }

    throw new InvalidTestException(
            "Cannot determine the test class for the given test. Please use the ZendoCore.testThis(test) method.");
}

From source file:com.clustercontrol.selfcheck.monitor.ThreadActivityMonitor.java

private static String getStackTrace(Thread t) {
    StackTraceElement[] eList = t.getStackTrace();
    StringBuilder trace = new StringBuilder();
    for (StackTraceElement e : eList) {
        trace.append("\n\tat ");
        trace.append(e.getClassName() + "." + e.getMethodName() + "(" + e.getFileName() + ":"
                + e.getLineNumber() + ")");
    }//  www.j av a 2s . c  om
    return trace.toString();
}

From source file:org.polymap.core.model.Messages.java

public static String getForClass(String keySuffix) {

    Exception e = new Exception();
    e.fillInStackTrace();//from   w ww  .  j  a v  a  2 s .  com
    StackTraceElement[] trace = e.getStackTrace();
    StackTraceElement elm = trace[trace.length - 1];

    StringBuffer key = new StringBuffer(64);
    key.append(StringUtils.substringAfterLast(elm.getClassName(), ".")).append("_").append(key);

    ClassLoader cl = Messages.class.getClassLoader();
    // getBundle() caches the bundles
    ResourceBundle bundle = ResourceBundle.getBundle(BUNDLE_NAME, RWT.getLocale(), cl);
    return bundle.getString(key.toString());
}

From source file:Main.java

public static String getThreadStack(Thread t) {
    StackTraceElement[] stacks = t.getStackTrace();
    StringBuffer buffer = new StringBuffer();
    for (StackTraceElement stack : stacks) {
        String filename = stack.getFileName();
        if (filename == null) {
            filename = "NULL";
        }/*from   w ww . j  a  v a  2  s  .  c  o m*/
        String className = stack.getClassName();
        String methodName = stack.getMethodName();
        int line = stack.getLineNumber();
        buffer.append(String.format("%s.%s(%s:%d)\r", className, methodName, filename, line));
    }
    return buffer.toString();
}

From source file:org.polymap.core.Messages.java

public static String getForClass(String keySuffix) {
    Exception e = new Exception();
    e.fillInStackTrace();//from  www  . j  a  va2  s  .co m
    StackTraceElement[] trace = e.getStackTrace();
    StackTraceElement elm = trace[trace.length - 1];

    StringBuffer key = new StringBuffer(64);
    key.append(StringUtils.substringAfterLast(elm.getClassName(), ".")).append("_").append(key);

    ClassLoader cl = Messages.class.getClassLoader();
    // getBundle() caches the bundles
    ResourceBundle bundle = ResourceBundle.getBundle(BUNDLE_NAME, RWT.getLocale(), cl);
    return bundle.getString(key.toString());
}

From source file:Main.java

public static String getCompressedStackTrace(Throwable t, int startAt, int limit) {
    try {/*from   www .  j a v a2s.c  o  m*/
        StackTraceElement[] stackTrace = t.getStackTrace();
        if (stackTrace.length < startAt) {
            return "";
        }
        StringBuilder sb = new StringBuilder("");
        for (int i = startAt; i < stackTrace.length && i < startAt + limit; i++) {
            StackTraceElement element = stackTrace[i];
            String classname = element.getClassName();
            String cnShort;
            boolean showLineNumber = true;
            boolean breakAfter = false;
            if (classname.startsWith("com.vuze.android.remote.")) {
                cnShort = classname.substring(24, classname.length());
            } else if (classname.equals("android.os.Handler")) {
                showLineNumber = false;
                cnShort = "Handler";
            } else if (classname.equals("android.os.Looper")) {
                showLineNumber = false;
                cnShort = "Looper";
                breakAfter = true;
            } else if (classname.length() < 9) { // include full if something like aa.ab.ac
                cnShort = classname;
            } else {
                int len = classname.length();
                int start = len > 14 ? len - 14 : 0;

                int pos = classname.indexOf('.', start);
                if (pos >= 0) {
                    start = pos + 1;
                }
                cnShort = classname.substring(start, len);
            }
            if (i != startAt) {
                sb.append(", ");
            }
            sb.append(cnShort);
            sb.append('.');
            sb.append(element.getMethodName());
            if (showLineNumber) {
                sb.append(':');
                sb.append(element.getLineNumber());
            }
            if (breakAfter) {
                break;
            }
        }
        Throwable cause = t.getCause();
        if (cause != null) {
            sb.append("\n|Cause ");
            sb.append(cause.getClass().getSimpleName());
            if (cause instanceof Resources.NotFoundException || cause instanceof RuntimeException) {
                sb.append(' ');
                sb.append(cause.getMessage());
            }
            sb.append(' ');
            sb.append(getCompressedStackTrace(cause, 0, 9));
        }
        return sb.toString();
    } catch (Throwable derp) {
        return "derp " + derp.getClass().getSimpleName();
    }
}

From source file:de.tudarmstadt.ukp.clarin.webanno.api.dao.DebugUtils.java

public static void smallStack(int aLimit) {
    StringBuilder sb = new StringBuilder();
    Exception e = new RuntimeException();
    boolean reqNewLine = true;
    boolean firstSkipped = false;
    int count = 0;
    for (StackTraceElement f : e.getStackTrace()) {
        if (!firstSkipped) {
            firstSkipped = true;//from ww  w  .j a v a 2  s .  c o  m
            continue;
        }
        if (f.getClassName().startsWith("de.tudarmstadt")) {
            if (reqNewLine) {
                sb.append("\n");
            }
            sb.append(f);
            count++;
        } else {
            sb.append(".");
            reqNewLine = true;
        }
        if (aLimit > 0 && count >= aLimit) {
            break;
        }
    }
    LOG.debug(sb);
}