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:org.mule.config.ExceptionHelper.java

public static String getExceptionStack(Throwable t) {
    StringBuffer buf = new StringBuffer();
    // get exception stack
    List exceptions = getExceptionsAsList(t);

    int i = 1;// w  w  w  . j  ava2 s  . c o m
    for (Iterator iterator = exceptions.iterator(); iterator.hasNext(); i++) {
        if (i > exceptionThreshold && exceptionThreshold > 0) {
            buf.append("(").append(exceptions.size() - i + 1).append(" more...)");
            break;
        }
        Throwable throwable = (Throwable) iterator.next();
        ExceptionReader er = getExceptionReader(throwable);
        buf.append(i).append(". ").append(er.getMessage(throwable)).append(" (");
        buf.append(throwable.getClass().getName()).append(")\n");
        if (verbose && throwable.getStackTrace().length > 0) {
            StackTraceElement e = throwable.getStackTrace()[0];
            buf.append("  ").append(e.getClassName()).append(":").append(e.getLineNumber()).append(" (")
                    .append(getJavaDocUrl(throwable.getClass())).append(")\n");
        }
    }
    return buf.toString();
}

From source file:datafu.hourglass.jobs.StagedOutputJob.java

/**
 * Gets the class for the caller./*from  w  w  w .  j  a v  a 2s  . c o  m*/
 * 
 * @return caller class
 */
private static Class<?> getCallersClass() {
    StackTraceElement[] stack = Thread.currentThread().getStackTrace();
    boolean foundSelf = false;
    for (StackTraceElement element : stack) {
        if (foundSelf && !StagedOutputJob.class.getName().equals(element.getClassName())) {
            try {
                return Class.forName(element.getClassName());
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        } else if (StagedOutputJob.class.getName().equals(element.getClassName())
                && "getCallersClass".equals(element.getMethodName())) {
            foundSelf = true;
        }
    }
    return StagedOutputJob.class;
}

From source file:com.simiacryptus.util.io.MarkdownNotebookOutput.java

/**
 * Get markdown notebook output.//from  w w  w  . jav  a 2  s.  c o m
 *
 * @return the markdown notebook output
 */
public static com.simiacryptus.util.io.MarkdownNotebookOutput get() {
    try {
        final StackTraceElement callingFrame = Thread.currentThread().getStackTrace()[2];
        final String className = callingFrame.getClassName();
        final String methodName = callingFrame.getMethodName();
        @javax.annotation.Nonnull
        final String fileName = methodName + ".md";
        @javax.annotation.Nonnull
        File path = new File(Util.mkString(File.separator, "reports",
                className.replaceAll("\\.", "/").replaceAll("\\$", "/")));
        path = new File(path, fileName);
        path.getParentFile().mkdirs();
        return new com.simiacryptus.util.io.MarkdownNotebookOutput(path, methodName);
    } catch (@javax.annotation.Nonnull final FileNotFoundException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.simiacryptus.util.io.MarkdownNotebookOutput.java

/**
 * Get markdown notebook output./*from  ww w  .  j av a  2 s  . c  o m*/
 *
 * @param source the source
 * @return the markdown notebook output
 */
public static MarkdownNotebookOutput get(Object source) {
    try {
        StackTraceElement callingFrame = Thread.currentThread().getStackTrace()[2];
        String className = null == source ? callingFrame.getClassName() : source.getClass().getCanonicalName();
        String methodName = callingFrame.getMethodName();
        String fileName = methodName + ".md";
        File path = new File(Util.mkString(File.separator, "reports",
                className.replaceAll("\\.", "/").replaceAll("\\$", "/"), fileName));
        path.getParentFile().mkdirs();
        return new MarkdownNotebookOutput(path, methodName);
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.openspaces.test.client.executor.ExecutorUtils.java

/**
 * Strip stack trace of provided {@link Throwable} object up to supplied classname <b>and</b>
 * method name. This method is useful when you wish to hide internal stackTrace.<br> This method
 * scans the {@link Throwable#getStackTrace()} and removes all {@link StackTraceElement} up to
 * supplied classname and methodName. The Throwable's cause is also scanned in a
 * <i>recursive</i> manner. <p> <u>NOTE</u>: If the provided classname and method was not found
 * in the stackTrace, the {@link Throwable} stackTrace stays unmodified.
 *
 * <pre>//  w ww  .  j  a va  2s  .  com
 * For example:
 * Original stackTrace:
 * java.lang.Exception: Example stack trace...
 * at test.TestSTrace.applCall3(Test.java:926)
 * at test.TestSTrace.applCall2(Test.java:921)
 * at test.TestSTrace.applCall1(Test.java:916)
 * at test.TestSTrace.startApplCall(Test.java:912)
 * at test.TestSTrace.internalCall3(Test.java:941)
 * at test.TestSTrace.internalCall2(Test.java:936)
 * at test.TestSTrace.internalCall1(Test.java:931)
 * at test.TestSTrace.main(Test.java:977)
 *
 * TGridHelper.stripStackTrace(throwable, "test.TestSTrace", "startApplCall");
 * throwable.printStackTrace();
 *
 * After:
 * java.lang.Exception: Example stack trace...
 * at test.TestSTrace.applCall3(Test.java:941)
 * at test.TestSTrace.applCall2(Test.java:936)
 * at test.TestSTrace.applCall1(Test.java:931)
 * at test.TestSTrace.startApplCall(Test.java:912)
 * </pre>
 *
 * @param ex         the throwable object to strip stack trace.
 * @param className  strip stackTrace up to classname.
 * @param methodName strip stackTrace up to methodName.
 */
public static void stripStackTrace(Throwable ex, String className, String methodName) {
    boolean found = false;
    StackTraceElement[] ste = ex.getStackTrace();
    List<StackTraceElement> stList = asList(ste);
    for (int i = stList.size() - 1; i >= 0; i--) {
        StackTraceElement traceList = stList.get(i);
        String traceClassName = traceList.getClassName();
        String traceMethodName = traceList.getMethodName();

        /* only if classname and method are equals with StackTraceElement */
        if (!(traceClassName.equals(className) && traceMethodName.equals(methodName)))
            stList.remove(i);
        else {
            found = true;
            break;
        }
    }

    /* replace only if provided classname and method was found the the stackTrace */
    if (found)
        ex.setStackTrace(stList.toArray(new StackTraceElement[stList.size()]));

    /* recursive call on all causes */
    Throwable cause = ex.getCause();
    if (cause != null)
        stripStackTrace(cause, className, methodName);
}

From source file:test.jamocha.languages.clips.SystemTest.java

/**
 * Get the calling method name. <br /> Utility function
 *
 * @return method name/*w ww  .  ja va  2 s .co m*/
 */
public static String getMethodName() {
    // index 0 is getStackTrace, index 1 is getMethodName, index 2 is invoking method
    final StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[2];
    return stackTraceElement.getClassName() + "::" + stackTraceElement.getMethodName();
}

From source file:org.eclipse.epp.internal.logging.aeri.ui.model.Reports.java

public static String traceIdentityHash(ErrorReport report) {
    final Hasher hasher = Hashing.murmur3_128().newHasher();
    visit(report, new ModelSwitch<Hasher>() {

        @Override/*from ww  w.  j a  v  a2s . co  m*/
        public Hasher caseStackTraceElement(StackTraceElement element) {
            hasher.putString(element.getClassName(), UTF_8);
            hasher.putString(element.getMethodName(), UTF_8);
            hasher.putInt(element.getLineNumber());
            return null;
        }
    });
    String hash = hasher.hash().toString();
    return hash;
}

From source file:com.codepunk.codepunklib.util.log.FormattingLogger.java

/**
 * Returns the {@link Class} that corresponds to the given {@link StackTraceElement}.
 * @param element A {@link StackTraceElement}.
 * @return The Class object for the class with the name specified by <code>element</code>.
 * @throws ClassNotFoundException If the class cannot be located.
 *//*ww  w.ja  v  a2 s  .  c  om*/
private static Class getClass(@NonNull StackTraceElement element) throws ClassNotFoundException {
    String className = element.getClassName();
    if (className.contains(ANONYMOUS_CLASS_INDICATOR)) {
        className = className.split(ANONYMOUS_CLASS_INDICATOR_REGEX)[0];
    }
    return Class.forName(className);
}

From source file:org.eclipse.wb.internal.core.utils.exception.DesignerExceptionUtils.java

/**
 * @return <code>true</code> if given {@link Throwable} has given sequence on class/method names.
 *//*w  ww . j  a v a2  s.  c  o  m*/
public static boolean hasTraceElementsSequence(Throwable e, String[][] expected) {
    StackTraceElement[] elements = e.getStackTrace();
    traceElements: for (int i = 0; i < elements.length; i++) {
        for (int j = 0; j < expected.length; j++) {
            if (i + j < elements.length) {
                StackTraceElement element = elements[i + j];
                String expectedClass = expected[j][0];
                String expectedMethod = expected[j][1];
                if (element.getClassName().equals(expectedClass)
                        && element.getMethodName().equals(expectedMethod)) {
                    continue;
                }
            }
            continue traceElements;
        }
        return true;
    }
    return false;
}

From source file:org.apache.jorphan.util.JOrphanUtils.java

/**
 * Display currently running threads on system.out
 * This may be expensive to run./* w  w  w.  ja  va 2  s  .  co  m*/
 * Mainly designed for use at the end of a non-GUI test to check for threads that might prevent the JVM from exiting.
 *
 * @param includeDaemons whether to include daemon threads or not.
 */
public static void displayThreads(boolean includeDaemons) {
    Map<Thread, StackTraceElement[]> m = Thread.getAllStackTraces();
    String lineSeparator = System.getProperty("line.separator");
    StringBuilder builder = new StringBuilder();
    for (Map.Entry<Thread, StackTraceElement[]> e : m.entrySet()) {
        boolean daemon = e.getKey().isDaemon();
        if (includeDaemons || !daemon) {
            builder.setLength(0);
            StackTraceElement[] ste = e.getValue();
            for (StackTraceElement stackTraceElement : ste) {
                int lineNumber = stackTraceElement.getLineNumber();
                builder.append(stackTraceElement.getClassName() + "#" + stackTraceElement.getMethodName()
                        + (lineNumber >= 0 ? " at line:" + stackTraceElement.getLineNumber() : "")
                        + lineSeparator);
            }
            System.out.println(e.getKey().toString() + ((daemon ? " (daemon)" : "")) + ", stackTrace:"
                    + builder.toString());
        }
    }
}