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.moss.greenshell.wizard.catastrophe.PostMortemScreen.java

public static void submitErrorReport(final Throwable cause, final ErrorReportDecorator... decorators)
        throws Exception {

    List<ErrorReportChunk> chunks = new LinkedList<ErrorReportChunk>();

    try {/*  ww  w .  j  a va2 s  .  c  om*/
        if (cause instanceof InternalErrorException) {
            InternalErrorException ie = (InternalErrorException) cause;
            ErrorReportChunk chunk = new ErrorReportChunk("internal-error-id", "text/plain",
                    ie.id().getBytes("UTF8"));
            chunks.add(chunk);
        } else if (cause instanceof SOAPFaultException) {
            SOAPFaultException soapFault = (SOAPFaultException) cause;
            String content = soapFault.getFault().getFirstChild().getTextContent();
            String prefix = "Internal Service Error Occurred: ";
            if (content.startsWith(prefix)) {
                String id = content.substring(prefix.length());
                ErrorReportChunk chunk = new ErrorReportChunk("internal-error-id", "text/plain",
                        id.getBytes("UTF8"));
                chunks.add(chunk);
            }
        }
    } catch (Throwable t) {
        t.printStackTrace();
    }

    // STACK TRACE
    ByteArrayOutputStream stackBytes = new ByteArrayOutputStream();
    PrintStream stackPrintStream = new PrintStream(stackBytes);
    cause.printStackTrace(stackPrintStream);
    stackPrintStream.close();
    stackBytes.close();

    ErrorReportChunk chunk = new ErrorReportChunk("stack trace", "text/plain", stackBytes.toByteArray());
    chunks.add(chunk);

    // THREAD DUMP

    ByteArrayOutputStream dumpBytes = new ByteArrayOutputStream();
    PrintStream out = new PrintStream(dumpBytes);
    Map<Thread, StackTraceElement[]> traceMap = Thread.getAllStackTraces();
    for (Map.Entry<Thread, StackTraceElement[]> next : traceMap.entrySet()) {
        out.println();
        out.println(next.getKey().getName());
        for (StackTraceElement line : next.getValue()) {
            String className = emptyIfNull(line.getClassName());
            String methodName = emptyIfNull(line.getMethodName());
            String fileName = emptyIfNull(line.getFileName());

            out.println("    " + className + "." + methodName + " (" + fileName + " line "
                    + line.getLineNumber() + ")");
        }
    }
    out.flush();
    out.close();
    ErrorReportChunk stackDump = new ErrorReportChunk("thread dump", "text/plain", dumpBytes.toByteArray());
    chunks.add(stackDump);

    // SYSTEM PROPERTIES
    ByteArrayOutputStream propsBytes = new ByteArrayOutputStream();
    PrintStream propsOut = new PrintStream(propsBytes);
    for (Map.Entry<Object, Object> next : System.getProperties().entrySet()) {
        propsOut.println(" " + next.getKey() + "=" + next.getValue());
    }
    propsOut.flush();
    propsOut.close();
    chunks.add(new ErrorReportChunk("system properties", "text/plain", propsBytes.toByteArray()));

    // LOCAL CLOCK
    chunks.add(new ErrorReportChunk("local clock", "text/plain", new DateTime().toString().getBytes()));

    // NETWORKING
    StringBuffer networking = new StringBuffer();
    Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
    while (ifaces.hasMoreElements()) {
        NetworkInterface iface = ifaces.nextElement();
        networking.append("INTERFACE: " + iface.getName() + " (" + iface.getDisplayName() + ")\n");
        Enumeration<InetAddress> addresses = iface.getInetAddresses();
        while (addresses.hasMoreElements()) {
            InetAddress address = addresses.nextElement();
            networking.append("  Address:" + address.getHostAddress() + "\n");
            networking.append("      Cannonical Host Name: " + address.getCanonicalHostName() + "\n");
            networking.append("                 Host Name: " + address.getHostName() + "\n");
        }
    }
    chunks.add(new ErrorReportChunk("network configuration", "text/plain", networking.toString().getBytes()));

    // DECORATORS
    if (decorators != null) {
        for (ErrorReportDecorator decorator : decorators) {
            chunks.addAll(decorator.makeChunks(cause));
        }
    }
    ErrorReport report = new ErrorReport(chunks);
    Reporter reporter = new Reporter();
    ReportId id = reporter.submitReport(report);
}

From source file:com.code19.library.L.java

private static String[] wrapperContent(String tag, Object... objects) {
    if (TextUtils.isEmpty(tag)) {
        tag = TAG;/*from w  w w  . j  av a  2 s .c  o  m*/
    }
    StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
    StackTraceElement targetElement = stackTrace[5];
    String className = targetElement.getClassName();
    String[] classNameInfo = className.split("\\.");
    if (classNameInfo.length > 0) {
        className = classNameInfo[classNameInfo.length - 1] + ".java";
    }
    String methodName = targetElement.getMethodName();
    int lineNumber = targetElement.getLineNumber();
    if (lineNumber < 0) {
        lineNumber = 0;
    }
    String methodNameShort = methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
    String msg = (objects == null) ? "Log with null object" : getObjectsString(objects);
    String headString = "[(" + className + ":" + lineNumber + ")#" + methodNameShort + " ] ";
    return new String[] { tag, msg, headString };
}

From source file:org.jboss.loom.utils.Utils.java

/**
 *  Finds a subclass of given $parentClass in given $stackTrace, skipping $skip levels.
 *  Returns null if not found.// w w  w  . j  a  v a 2  s  .  c o  m
 */
public static <T> Class<? extends T> findSubclassInStackTrace(Class<T> parentClass,
        StackTraceElement[] stackTrace, int skip) {
    //for( StackTraceElement call : stackTrace) {
    for (int i = skip; i < stackTrace.length; i++) {
        StackTraceElement call = stackTrace[i];
        try {
            Class<?> callClass = Class.forName(call.getClassName());
            if (parentClass.isAssignableFrom(callClass))
                return (Class<? extends T>) callClass;
        } catch (ClassNotFoundException ex) {
            Reporter.log.error("Can't load class " + call.getClassName() + ":\n    " + ex.getMessage());
        }
    }
    return null;
}

From source file:com.cloud.utils.StringUtils.java

public static String getExceptionStackInfo(final Throwable e) {
    final StringBuffer sb = new StringBuffer();

    sb.append(e.toString()).append("\n");
    final StackTraceElement[] elemnents = e.getStackTrace();
    for (final StackTraceElement element : elemnents) {
        sb.append(element.getClassName()).append(".");
        sb.append(element.getMethodName()).append("(");
        sb.append(element.getFileName()).append(":");
        sb.append(element.getLineNumber()).append(")");
        sb.append("\n");
    }//from  w  w  w.j a v a2s .c o m

    return sb.toString();
}

From source file:griffon.util.GriffonUtil.java

public static void printSanitizedStackTrace(Throwable t, PrintWriter p) {
    t = sanitize(t);//from  w  w  w. j a va  2s  .c o m

    StackTraceElement[] trace = t.getStackTrace();
    for (int i = 0; i < trace.length; i++) {
        StackTraceElement stackTraceElement = trace[i];
        p.println("at " + stackTraceElement.getClassName() + "(" + stackTraceElement.getMethodName() + ":"
                + stackTraceElement.getLineNumber() + ")");
    }
}

From source file:org.evosuite.regression.RegressionExceptionHelper.java

/**
 * Get signature based on the root cause from the stack trace
 * (uses: location of the error triggering line from CUT)
 * @param CUT the class to expect the exception to be thrown from
 * @return signature string//from   w ww .  jav  a2s  .c  om
 */
public static String getExceptionSignature(Throwable throwable, String CUT) {
    String signature = throwable.getClass().getSimpleName();

    StackTraceElement[] stackTrace = throwable.getStackTrace();
    for (StackTraceElement el : stackTrace) {
        String elClass = el.getClassName();
        if (!Objects.equals(elClass, CUT)) {
            continue;
        }

        String method = el.getMethodName();
        int line = el.getLineNumber();
        signature += ":" + method + "-" + line;
        break;
    }
    return signature;
}

From source file:com.taobao.itest.util.XlsUtil.java

private static String getTestClassPackage() {
    int atLeastCallCount = 5;
    StackTraceElement stack[] = (new Throwable()).getStackTrace();

    /**/* ww w .ja v  a 2  s.  c o  m*/
     * Back call stack will know the caller 0getTestClassName
     * 1getExcelRealPath 2: readData 3: readForObject 4: test class
     * 
     */
    if (stack.length < atLeastCallCount) {
        // Not less than 5 times the call, or take less than a call to the
        // test class
        return null;
    } else {
        StackTraceElement ste = stack[atLeastCallCount - 1];
        return ste.getClassName().substring(0, ste.getClassName().lastIndexOf('.'));
    }
}

From source file:org.sonatype.flexmojos.tests.AbstractFlexMojosTests.java

private static String getTestName() {
    StackTraceElement[] stackTrace = new Exception().getStackTrace();
    for (StackTraceElement stack : stackTrace) {
        Class<?> testClass;
        try {//from w w w.  ja  va2  s .co m
            testClass = Class.forName(stack.getClassName());
        } catch (ClassNotFoundException e) {
            // nvm, and should never happen
            continue;
        }
        for (Method method : testClass.getMethods()) {
            if (method.getName().equals(stack.getMethodName())) {
                if (method.getAnnotation(Test.class) != null) {
                    return method.getName();
                }
            }
        }
    }
    return null;
}

From source file:org.eclipse.swt.examples.watchdog.TimedEventWatchdog.java

private static boolean traceElementIs(StackTraceElement e, String className, String method) {
    return className.equals(e.getClassName()) && method.equals(e.getMethodName());
}

From source file:griffon.util.GriffonUtil.java

/**
 * <p>Remove all apparently Griffon-internal trace entries from the exception instance<p>
 * <p>This modifies the original instance and returns it, it does not clone</p>
 * @param t/*from ww  w.  j a v  a2  s .  c o  m*/
 * @return The exception passed in, after cleaning the stack trace
 */
public static Throwable sanitize(Throwable t) {
    // Note that this getProperty access may well be synced...
    if (!Boolean.valueOf(System.getProperty("griffon.full.stacktrace")).booleanValue()) {
        StackTraceElement[] trace = t.getStackTrace();
        List<StackTraceElement> newTrace = new ArrayList<StackTraceElement>();
        for (int i = 0; i < trace.length; i++) {
            StackTraceElement stackTraceElement = trace[i];
            if (isApplicationClass(stackTraceElement.getClassName())) {
                newTrace.add(stackTraceElement);
            }
        }

        // Only trim the trace if there was some application trace on the stack
        // if not we will just skip sanitizing and leave it as is
        if (newTrace.size() > 0) {
            // We don't want to lose anything, so log it
            STACK_LOG.error("Sanitizing stacktrace:", t);
            StackTraceElement[] clean = new StackTraceElement[newTrace.size()];
            newTrace.toArray(clean);
            t.setStackTrace(clean);
        }
    }
    return t;
}