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.qpark.eip.core.ToString.java

private static String getStackTrace(final Throwable t, final boolean isCause) {
    StringBuffer sb = new StringBuffer(1024);
    if (isCause) {
        sb.append("Caused by: ");
    }// w  ww. ja v a  2s . c o  m
    sb.append(t.getClass().getName()).append(": ").append(t.getMessage()).append("\n");
    StackTraceElement[] stack = t.getStackTrace();
    int classNameLines = 0;
    int classNameLinesMax = 3;
    boolean printDots = false;
    boolean firstLine = true;
    for (StackTraceElement elem : stack) {
        if (firstLine || elem.getClassName().startsWith(CLASSNAME) && classNameLines <= classNameLinesMax) {
            sb.append("\tat ").append(elem.toString()).append("\n");
            classNameLines++;
            firstLine = false;
            printDots = false;
        } else {
            if (!printDots) {
                sb.append("\tat ...\n");
                printDots = true;
            }
            classNameLines = 0;
        }
    }
    Throwable cause = t.getCause();
    if (cause != null) {
        sb.append(getStackTrace(cause, true));
    }
    return sb.toString();
}

From source file:com.ichi2.anki.AnkiDroidApp.java

private static String getExceptionHash(Throwable th) {
    final StringBuilder res = new StringBuilder();
    Throwable cause = th;//from   w  w w.j a v a2 s .c  o  m
    while (cause != null) {
        final StackTraceElement[] stackTraceElements = cause.getStackTrace();
        for (final StackTraceElement e : stackTraceElements) {
            res.append(e.getClassName());
            res.append(e.getMethodName());
        }
        cause = cause.getCause();
    }
    return Integer.toHexString(res.toString().hashCode());
}

From source file:org.jahia.services.SpringContextSingleton.java

private static boolean isApplicationContextInitializationInProgress() {
    StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
    for (StackTraceElement element : stackTrace) {
        for (String className : APPLICATION_CONTEXT_INITIALIZATION_IN_PROGRESS_INDICATORS) {
            if (element.getClassName().equals(className)) {
                return true;
            }//w w  w.  j  av a2s  . c  o m
        }
    }
    return false;
}

From source file:controllers.GWT2Controller.java

/**
 * Invoke a Service //from   ww w .  j  av  a2 s.  c  o m
 * 
 * This way GWT2Service is allowed to use GWT2Chain
 * 
 * @param mod
 * @return
 * @throws Exception 
 */
private static String invoke(GWT2Service service, RPCRequest rpcRequest) throws Exception {
    String responseload = null;

    try {
        // Get GWt2Invoker
        GWT2Invoker invoker = new GWT2Invoker(service, request, response, session, rpcRequest);

        // run 
        boolean run = true;
        Object result = null;
        Object chainResult = null;
        boolean firstcall = true;
        GWT2ChainRuntime chain = null;
        while (run) {
            try {
                if (firstcall) {
                    firstcall = false;
                    // call service
                    result = invoker.invoke();
                    // get a result stop request
                    run = false;
                } else if (chain != null) {
                    // call callback
                    result = chain.getChain().chain(chainResult);
                    // get a result stop request
                    run = false;
                } else {
                    // no callback to call stop process
                    run = false;
                }
            } catch (GWT2ChainAsync casync) {
                // async chain call
                chain = casync;
                chainResult = chainAwait(chain);
            } catch (GWT2ChainSync csync) {
                chain = csync;
                chainResult = chainDo(chain);
            } catch (InvocationTargetException ite) {
                if (ite.getTargetException() instanceof GWT2ChainAsync) {
                    // async chain call
                    chain = (GWT2ChainAsync) ite.getTargetException();
                    chainResult = chainAwait(chain);
                } else if (ite.getTargetException() instanceof GWT2ChainSync) {
                    chain = (GWT2ChainSync) ite.getTargetException();
                    chainResult = chainDo(chain);
                } else
                    throw ite;
            }
        }

        // encode result
        responseload = RPC.encodeResponseForSuccess(rpcRequest.getMethod(), result,
                rpcRequest.getSerializationPolicy(), AbstractSerializationStream.DEFAULT_FLAGS);

        return responseload;

    } catch (Exception ex) {
        if (ex instanceof PlayException)
            // Rethrow the enclosed exception
            throw (PlayException) ex;

        // if not gwt rpc rethrow
        if (!isGWT())
            throw ex;

        try {
            // else encode error
            responseload = RPC.encodeResponseForFailure(rpcRequest.getMethod(), ex,
                    rpcRequest.getSerializationPolicy(), AbstractSerializationStream.DEFAULT_FLAGS);

        } catch (SerializationException e) {
            StackTraceElement element = PlayException.getInterestingStrackTraceElement(e);
            if (element != null) {
                throw new JavaExecutionException(Play.classes.getApplicationClass(element.getClassName()),
                        element.getLineNumber(), e);
            }
            throw new JavaExecutionException(e);
        }
    }

    return responseload;
}

From source file:mSearch.tool.Log.java

private static void fehlermeldung_(int fehlerNummer, Exception ex, String[] texte) {
    final Throwable t = new Throwable();
    final StackTraceElement methodCaller = t.getStackTrace()[2];
    final String klasse = methodCaller.getClassName() + "." + methodCaller.getMethodName();
    String kl;/*  w ww.j a  v a2  s  .c o m*/
    try {
        kl = klasse;
        while (kl.contains(".")) {
            if (Character.isUpperCase(kl.charAt(0))) {
                break;
            } else {
                kl = kl.substring(kl.indexOf(".") + 1);
            }
        }
    } catch (Exception ignored) {
        kl = klasse;
    }
    addFehlerNummer(fehlerNummer, kl, ex != null);
    if (ex != null || Config.debug) {
        // Exceptions immer ausgeben
        resetProgress();
        String x, z;
        if (ex != null) {
            x = "!";
        } else {
            x = "=";
        }
        z = "*";
        logList.add(x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x
                + x + x + x + x + x + x + x + x + x + x + x + x + x + x + x);

        try {
            // Stacktrace
            try (StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw)) {
                if (ex != null) {
                    ex.printStackTrace(pw);
                }
                pw.flush();
                sw.flush();
                logList.add(sw.toString());
            }
        } catch (Exception ignored) {
        }

        logList.add(z + " Fehlernr: " + fehlerNummer);
        if (ex != null) {
            logList.add(z + " Exception: " + ex.getMessage());
        }
        logList.add(z + " " + FEHLER + kl);
        for (String aTexte : texte) {
            logList.add(z + "           " + aTexte);
        }
        logList.add("");
        printLog();
    }
}

From source file:com.github.errantlinguist.latticevisualiser.ArgParser.java

/**
 * Gets the name of the class of which the main method was called; This is
 * not necessarily {@code main(String[])} (which is what creates this
 * method useful). NOTE: Might be inaccurate if used in a multi-threaded
 * application, because it gets the head of the stack of only the thread in
 * which the method was called./*from  ww w.  ja va 2  s .c  o  m*/
 * 
 * @return The name of the class of which the main method was called.
 */
private static final String getMainClassName() {
    final StackTraceElement[] stack = Thread.currentThread().getStackTrace();
    final StackTraceElement main = stack[stack.length - 1];
    final String mainClassName = main.getClassName();
    return mainClassName;
}

From source file:schemacrawler.test.utility.TestUtility.java

private static StackTraceElement currentMethodStackTraceElement() {
    final StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
    final Iterator<StackTraceElement> stackTraceIterator = Arrays.asList(stackTrace).iterator();
    int i = 0;//from  www. j  a  va2s  .  c  om
    while (stackTraceIterator.hasNext()) {
        final StackTraceElement stackTraceElement = stackTraceIterator.next();
        if (stackTraceElement.getClassName().equals(TestUtility.class.getName())) {
            while (stackTraceIterator.hasNext()) {
                final String className = stackTraceIterator.next().getClassName();
                if (!className.equals(TestUtility.class.getName())
                        && !className.equals(TestWriter.class.getName())
                        && !className.matches(".*\\.Base.*Test")) {
                    return stackTrace[i + 1];
                }
                i++;
            }
        }
        i++;
    }

    return null;
}

From source file:com.google.gdt.eclipse.designer.util.GwtInvocationEvaluatorInterceptor.java

/**
 * @return <code>true</code> if given {@link Throwable} is caused by GWT Mail example.
 *///from  w  w w  . j av a2 s.c o m
private static boolean isMailExampleException(Throwable e) {
    StackTraceElement[] stackTrace = e.getStackTrace();
    StackTraceElement element = stackTrace[0];
    return "com.google.gwt.sample.mail.client.MailList".equals(element.getClassName())
            && "selectRow".equals(element.getMethodName());
}

From source file:org.apache.cxf.common.logging.LogUtils.java

private static void doLog(Logger log, Level level, String msg, Throwable t) {
    LogRecord record = new LogRecord(level, msg);

    record.setLoggerName(log.getName());
    record.setResourceBundleName(log.getResourceBundleName());
    record.setResourceBundle(log.getResourceBundle());

    if (t != null) {
        record.setThrown(t);//from www  .j av  a2 s  . co  m
    }

    //try to get the right class name/method name - just trace
    //back the stack till we get out of this class
    StackTraceElement stack[] = (new Throwable()).getStackTrace();
    String cname = LogUtils.class.getName();
    for (int x = 0; x < stack.length; x++) {
        StackTraceElement frame = stack[x];
        if (!frame.getClassName().equals(cname)) {
            record.setSourceClassName(frame.getClassName());
            record.setSourceMethodName(frame.getMethodName());
            break;
        }
    }
    log.log(record);
}

From source file:org.apache.hadoop.util.TestShell.java

private static int countTimerThreads() {
    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();

    int count = 0;
    ThreadInfo[] infos = threadBean.getThreadInfo(threadBean.getAllThreadIds(), 20);
    for (ThreadInfo info : infos) {
        if (info == null)
            continue;
        for (StackTraceElement elem : info.getStackTrace()) {
            if (elem.getClassName().contains("Timer")) {
                count++;//from w  w  w .j  av  a 2 s. c  o m
                break;
            }
        }
    }
    return count;
}