Example usage for java.lang StackTraceElement StackTraceElement

List of usage examples for java.lang StackTraceElement StackTraceElement

Introduction

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

Prototype

public StackTraceElement(String declaringClass, String methodName, String fileName, int lineNumber) 

Source Link

Document

Creates a stack trace element representing the specified execution point.

Usage

From source file:org.eclipse.recommenders.stacktraces.rcp.actions.LogErrorsAction.java

private StackTraceElement[] createTrace(int k) {
    StackTraceElement[] trace = new StackTraceElement[k];
    for (int j = k; j-- > 0;) {
        trace[j] = new StackTraceElement("org.eclipse.M", "method" + j, "", 1);
    }//from  w  ww.ja  v  a  2 s .co  m
    return trace;
}

From source file:org.usapi.common.USAPIException.java

private StackTraceElement getCaller() {
    StackTraceElement ste[] = Thread.currentThread().getStackTrace();
    StackTraceElement candidate = null;
    // if browser (chrome?) hangs stacktrace can seemingly not have expected contents (invoke0), so
    // providing default value
    StackTraceElement caller = new StackTraceElement("UNKNOWN_CLASS", "UNKNOWN_METHOD", "UNKNOWN_FILE", -1);
    for (int ndx = 0; ndx < ste.length; ndx++) {
        candidate = ste[ndx];//from   w w w  .  j a v a2 s  .  c om
        if (candidate.getClassName().equals("sun.reflect.NativeMethodAccessorImpl")
                && candidate.getMethodName().equals("invoke0")) {
            // no worries about index out of bounds here, the match is guaranteed to 
            // happen anywhere between index 5-8,  total stack depth ~20
            caller = ste[ndx - 1];
            break;
        }
    }
    return caller;
}

From source file:edu.umd.cs.marmoset.utilities.MarmosetUtilities.java

/**
 * Convert the string-representation of a stackTraceElement back to a StackTraceElement object.
 * @param stackTraceLine The string rep of a stackTraceElement.
 * @return The corresponding StackTraceElement object; null if a StackTraceElement cannot be
 *  reconstructed from the given string.
 *///  ww w.  j a  v  a2s.  c  o  m
public static StackTraceElement parseStackTrace(String stackTraceLine) {
    // Try with source info
    String regexp = "(.*)\\.([\\w<>]+)\\((\\w+\\.java):(\\d+)\\)";
    Pattern pattern = Pattern.compile(regexp);
    Matcher matcher = pattern.matcher(stackTraceLine);
    if (matcher.matches()) {
        String className = matcher.group(1);
        String methodName = matcher.group(2);
        String fileName = matcher.group(3);
        String s = matcher.group(4);
        int lineNumber = Integer.parseInt(s);
        return new StackTraceElement(className, methodName, fileName, lineNumber);
    }

    // Try without source info
    String regexpUnknown = "(.*)\\.([\\w<>]+)\\(Unknown Source\\)";
    pattern = Pattern.compile(regexpUnknown);
    matcher = pattern.matcher(stackTraceLine);
    if (matcher.matches()) {
        String className = matcher.group(1);
        String methodName = matcher.group(2);
        String fileName = null;
        int lineNumber = -1;
        return new StackTraceElement(className, methodName, fileName, lineNumber);
    }

    // Try for native methods
    String regexpNative = "(.*)\\.([\\w<>]+)\\(Native Method\\)";
    pattern = Pattern.compile(regexpNative);
    matcher = pattern.matcher(stackTraceLine);
    if (matcher.matches()) {
        String className = matcher.group(1);
        String methodName = matcher.group(2);
        String fileName = null;
        int lineNumber = -2;
        return new StackTraceElement(className, methodName, fileName, lineNumber);
    }

    //throw new IllegalStateException("Unable to parse stack trace: " +stackTraceLine);
    return null;
}

From source file:org.eclipse.recommenders.stacktraces.rcp.actions.LogErrorsAction.java

private StackTraceElement[] createRandomTrace() {
    StackTraceElement[] trace = new StackTraceElement[10];
    for (int j = 10; j-- > 0;) {
        trace[j] = new StackTraceElement("org.eclipse.M" + randomAlphanumeric(5), "method" + j, "", 1);
    }//from   w  ww .j  ava  2s  . c om
    return trace;
}

From source file:com.thoughtworks.go.plugin.infra.service.DefaultPluginLoggingServiceIntegrationTest.java

@Test
public void shouldLogThrowableDetailsAlongwithMessage() throws Exception {
    Throwable throwable = new RuntimeException("oops");
    throwable.setStackTrace(new StackTraceElement[] { new StackTraceElement("class", "method", "field", 20) });

    pluginLoggingService.error(pluginID(1), "LoggingClass", "error", throwable);

    assertMessageInLog(pluginLog(1), "ERROR", "LoggingClass", "error",
            "java\\.lang\\.RuntimeException:\\soops[\\s\\S]*at\\sclass\\.method\\(field:20\\)[\\s\\S]*$");
}

From source file:net.logstash.logback.encoder.LogstashEncoderTestCommon.java

@Test
public void callerDataIsIncluded() throws Exception {
    ILoggingEvent event = mockBasicILoggingEvent(Level.ERROR);
    when(event.getMDCPropertyMap()).thenReturn(Collections.<String, String>emptyMap());
    final StackTraceElement[] stackTraceElements = {
            new StackTraceElement("caller_class", "method_name", "file_name", 12345) };
    when(event.getCallerData()).thenReturn(stackTraceElements);

    encoder().setIncludeCallerInfo(true);

    encoder().doEncode(event);/*  ww  w  .j a v  a  2  s. c o  m*/
    closeQuietly(outputStream());

    JsonNode node = MAPPER.readTree(outputStream().toByteArray());

    assertThat(field(node, "caller_class_name").textValue(), is(stackTraceElements[0].getClassName()));
    assertThat(field(node, "caller_method_name").textValue(), is(stackTraceElements[0].getMethodName()));
    assertThat(field(node, "caller_file_name").textValue(), is(stackTraceElements[0].getFileName()));
    assertThat(field(node, "caller_line_number").intValue(), is(stackTraceElements[0].getLineNumber()));
}

From source file:org.bug4j.client.Bug4jTest.java

@Test
public void testSameTitle() throws Exception {
    final Random random = new Random();
    {/*ww  w.j  a  va2 s  .c  om*/
        final IllegalStateException e = new IllegalStateException("SameTitle");
        e.setStackTrace(new StackTraceElement[] {
                new StackTraceElement("org.bug4j.SomeClass", "someMethod", "SomeClass.java", 100),
                new StackTraceElement("org.bug4j.SomeClass", "someMethod", "SomeClass.java", random.nextInt()),
                new StackTraceElement("org.bug4j.SomeClass", "someMethod", "SomeClass.java", random.nextInt()),
                new StackTraceElement("org.bug4j.SomeClass", "someMethod", "SomeClass.java",
                        random.nextInt()), });
        Bug4jAgent.report("testSameTitle", e);
    }

    {
        final IllegalStateException e = new IllegalStateException("SameTitle");
        e.setStackTrace(new StackTraceElement[] {
                new StackTraceElement("org.bug4j.SomeClass", "someMethod", "SomeClass.java", 100),
                new StackTraceElement("org.bug4j.someOtherClass", "someOtherMethod", "SomeClass.java",
                        random.nextInt()),
                new StackTraceElement("org.bug4j.someOtherClass", "someOtherMethod", "SomeClass.java",
                        random.nextInt()),
                new StackTraceElement("org.bug4j.someOtherClass", "someMethod", "SomeClass.java",
                        random.nextInt()), });
        Bug4jAgent.report("testSameTitle", e);
    }
    Bug4jAgent.shutdown();
    Assert.assertEquals(2, Bug4jAgent.getReported());
}

From source file:net.logstash.logback.encoder.LogstashEncoderTest.java

@Test
public void callerDataIsIncluded() throws Exception {
    ILoggingEvent event = mockBasicILoggingEvent(Level.ERROR);
    when(event.getMDCPropertyMap()).thenReturn(Collections.<String, String>emptyMap());
    final StackTraceElement[] stackTraceElements = {
            new StackTraceElement("caller_class", "method_name", "file_name", 12345) };
    when(event.getCallerData()).thenReturn(stackTraceElements);

    encoder.setIncludeCallerInfo(true);// www.  j  a va  2  s  .  co m

    encoder.doEncode(event);
    closeQuietly(outputStream);

    JsonNode node = MAPPER.readTree(outputStream.toByteArray());

    assertThat(node.get("caller_class_name").textValue(), is(stackTraceElements[0].getClassName()));
    assertThat(node.get("caller_method_name").textValue(), is(stackTraceElements[0].getMethodName()));
    assertThat(node.get("caller_file_name").textValue(), is(stackTraceElements[0].getFileName()));
    assertThat(node.get("caller_line_number").intValue(), is(stackTraceElements[0].getLineNumber()));
}

From source file:cc.sion.core.utils.Exceptions.java

/**
 * from Netty, ??StackTrace./*from ww  w.  j a v a 2  s  .c  om*/
 *
 * ??, ????StackTrace. ???.
 *
 * ?????.
 *
 * <pre>
 * private static RuntimeException TIMEOUT_EXCEPTION = ExceptionUtil.setStackTrace(new RuntimeException("Timeout"),
 *       MyClass.class, "mymethod");
 *
 * </pre>
 */
public static <T extends Throwable> T setStackTrace(T exception, Class<?> throwClass, String throwClazz) {
    exception.setStackTrace(
            new StackTraceElement[] { new StackTraceElement(throwClass.getName(), throwClazz, null, -1) });
    return exception;// NOSONAR
}

From source file:net.logstash.logback.encoder.LogstashEncoderTestCommon.java

@Test
public void callerDataIsNotIncludedIfSwitchedOff() throws Exception {
    ILoggingEvent event = mock(ILoggingEvent.class);
    when(event.getLoggerName()).thenReturn("LoggerName");
    when(event.getThreadName()).thenReturn("ThreadName");
    when(event.getFormattedMessage()).thenReturn("My message");
    when(event.getLevel()).thenReturn(Level.ERROR);
    when(event.getMDCPropertyMap()).thenReturn(Collections.<String, String>emptyMap());
    final StackTraceElement[] stackTraceElements = {
            new StackTraceElement("caller_class", "method_name", "file_name", 12345) };
    when(event.getCallerData()).thenReturn(stackTraceElements);

    encoder().setIncludeCallerInfo(false);

    encoder().doEncode(event);//w w  w.  j av  a2 s.c  o  m
    closeQuietly(outputStream());

    JsonNode node = MAPPER.readTree(outputStream().toByteArray());
    assertThat(field(node, "caller_class_name"), is(nullValue()));
    assertThat(field(node, "caller_method_name"), is(nullValue()));
    assertThat(field(node, "caller_file_name"), is(nullValue()));
    assertThat(field(node, "caller_line_number"), is(nullValue()));
}