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.apache.drill.common.util.DrillExceptionUtil.java

/**
 * Recreate throwable from exception protoBuf which received from remote or local node.
 * if no match constructor found, the base Throwable (Exception or Error) is created as a substitution
 *
 * @param exceptionWrapper the exception protoBuf
 * @return Throwable deserialized from protoBuf
 *//*from w  w w.j  ava 2 s.c o  m*/
public static Throwable getThrowable(UserBitShared.ExceptionWrapper exceptionWrapper) {
    if (exceptionWrapper == null) {
        return null;
    }
    String className = exceptionWrapper.getExceptionClass();
    if (StringUtils.isBlank(className) || exceptionWrapper.getStackTraceCount() < 1) {
        return null;
    }
    Throwable inner = getThrowable(exceptionWrapper.getCause());
    try {
        Throwable throwable = getInstance(className, exceptionWrapper.getMessage(), inner);
        int size = exceptionWrapper.getStackTraceCount();
        StackTraceElement[] stackTrace = new StackTraceElement[size];
        for (int i = 0; i < size; ++i) {
            UserBitShared.StackTraceElementWrapper w = exceptionWrapper.getStackTrace(i);
            stackTrace[i] = new StackTraceElement(w.getClassName(), w.getMethodName(), w.getFileName(),
                    w.getLineNumber());
        }
        throwable.setStackTrace(stackTrace);
        return throwable;
    } catch (Throwable t) {
        return null;
    }
}

From source file:crittercism.CrittercismModuleModule.java

private static Exception createException(String name, String reason, String stackTrace) {
    Exception ex = new Exception(reason);
    if (stackTrace != null) {
        String[] stackObjs = stackTrace.split("\n");

        int nLength = stackObjs.length;
        StackTraceElement[] elements = new StackTraceElement[nLength];

        for (int nI = 0; nI < nLength; nI++) {
            elements[nI] = new StackTraceElement("Unity3D", stackObjs[nI], "", -1);
        }/*w  w w  .ja v  a 2  s.co m*/
        ex.setStackTrace(elements);
    }
    ;

    return ex;
}

From source file:ch.qos.logback.decoder.CallerStackTraceParser.java

@Override
public void captureField(IStaticLoggingEvent event, String fieldAsStr, PatternInfo info) {

    List<StackTraceElement> stackTrace = new ArrayList<StackTraceElement>();
    Matcher m = PATTERN.matcher(fieldAsStr);
    while (m.find()) {
        String line = m.group(1);

        Matcher m2 = PATTERN2.matcher(line);
        if (!m2.find()) {
            continue;
        }/*  w  w w  . j a  v  a 2s.co m*/

        String className = StringUtils.defaultString(m2.group("class"));
        String fileName = StringUtils.defaultString(m2.group("file"));
        String lineNumberStr = StringUtils.defaultString(m2.group("line"));

        // parse line number from string
        int lineNumber = 0;
        if (lineNumberStr.length() > 0) {
            try {
                lineNumber = Integer.valueOf(lineNumberStr);
            } catch (NumberFormatException e) {
                // ignore
            }
        }

        // parse method name from classname field
        int pos = className.lastIndexOf('.');
        String methodName;
        if (pos > -1) {
            methodName = className.substring(className.lastIndexOf('.') + 1);
            className = className.substring(0, pos);
        } else {
            methodName = className;
            className = "";
        }

        stackTrace.add(new StackTraceElement(className, methodName, fileName, lineNumber));
    }

    event.setCallerStackData(stackTrace);
}

From source file:org.uiautomation.ios.communication.FailedWebDriverLikeResponse.java

public static Exception createThrowable(JSONObject serialized) {
    try {/*  w w  w.j a  v  a2s  .c  o  m*/
        Class<? extends Exception> c = getException(serialized);

        String msg = serialized.getString("message");

        JSONObject cause = serialized.optJSONObject("cause");
        Throwable causeThrowable = null;
        if (cause != null) {
            causeThrowable = createThrowable(cause);
        }

        Object[] args = new Object[] { msg, (Exception) causeThrowable };
        Class<?>[] argsClass = new Class[] { String.class, Throwable.class };
        Constructor<?> constructor = c.getConstructor(argsClass);
        Throwable exception = (Throwable) constructor.newInstance(args);
        JSONArray stack = serialized.optJSONArray("stacktrace");
        if (stack != null) {
            List<StackTraceElement> stacks = new ArrayList<StackTraceElement>();
            for (int i = 0; i < stack.length(); i++) {
                JSONObject o = stack.getJSONObject(i);
                String fileName = o.getString("fileName");
                String className = o.getString("className");
                String methodName = o.getString("methodName");
                int lineNumber = o.getInt("lineNumber");
                StackTraceElement el = new StackTraceElement(className, methodName, fileName, lineNumber);
                stacks.add(el);
            }
            exception.setStackTrace(stacks.toArray(new StackTraceElement[0]));
        }
        return (Exception) exception;
    } catch (Exception e) {
        throw new IOSAutomationException(
                "error deserializing the exception from the server : " + serialized.toString(), e);
    }

}

From source file:com.hazelcast.stabilizer.Utils.java

public static void fixRemoteStackTrace(Throwable remoteCause, StackTraceElement[] localSideStackTrace) {
    StackTraceElement[] remoteStackTrace = remoteCause.getStackTrace();
    StackTraceElement[] newStackTrace = new StackTraceElement[localSideStackTrace.length
            + remoteStackTrace.length];/*from  w ww . j a  v a 2s . c o m*/
    System.arraycopy(remoteStackTrace, 0, newStackTrace, 0, remoteStackTrace.length);
    newStackTrace[remoteStackTrace.length] = new StackTraceElement(EXCEPTION_SEPARATOR, "", null, -1);
    System.arraycopy(localSideStackTrace, 1, newStackTrace, remoteStackTrace.length + 1,
            localSideStackTrace.length - 1);
    remoteCause.setStackTrace(newStackTrace);
}

From source file:com.msopentech.odatajclient.engine.communication.ODataClientErrorException.java

/**
 * Constructor.//w w  w.  ja  v a  2  s.  c o m
 *
 * @param statusLine request status info.
 * @param error OData error to be wrapped.
 */
public ODataClientErrorException(final StatusLine statusLine, final ODataError error) {
    super((StringUtils.isBlank(error.getCode()) ? StringUtils.EMPTY : "(" + error.getCode() + ") ")
            + error.getMessageValue() + " [" + statusLine.toString() + "]");

    this.statusLine = statusLine;
    this.error = error;

    if (this.error.getInnerErrorType() != null && this.error.getInnerErrorMessage() != null) {
        final RuntimeException cause = new RuntimeException(
                this.error.getInnerErrorType() + ": " + this.error.getInnerErrorMessage());

        if (this.error.getInnerErrorStacktrace() != null) {
            List<String> stLines;
            try {
                stLines = IOUtils.readLines(new StringReader(this.error.getInnerErrorStacktrace()));
            } catch (IOException e) {
                stLines = Collections.<String>emptyList();
            }
            StackTraceElement[] stElements = new StackTraceElement[stLines.size()];
            for (int i = 0; i < stLines.size(); i++) {
                final String stLine = stLines.get(i).substring(stLines.get(i).indexOf("at ") + 3);
                final int lastDotPos = stLine.lastIndexOf('.');
                stElements[i] = new StackTraceElement(stLine.substring(0, lastDotPos),
                        stLine.substring(lastDotPos + 1), null, 0);
            }
            cause.setStackTrace(stElements);
        }

        initCause(cause);
    }
}

From source file:com.facebook.presto.jdbc.client.FailureInfo.java

public static StackTraceElement toStackTraceElement(String stack) {
    Matcher matcher = STACK_TRACE_PATTERN.matcher(stack);
    if (matcher.matches()) {
        String declaringClass = matcher.group(1);
        String methodName = matcher.group(2);
        String fileName = matcher.group(3);
        int number = -1;
        if (fileName.equals("Native Method")) {
            fileName = null;/*  www .j  av a2s .  c  o m*/
            number = -2;
        } else if (matcher.group(4) != null) {
            number = Integer.parseInt(matcher.group(4));
        }
        return new StackTraceElement(declaringClass, methodName, fileName, number);
    }
    return new StackTraceElement("Unknown", stack, null, -1);
}

From source file:cn.bran.play.JapidTemplateBase.java

@Override
protected void handleException(RuntimeException e) {
    if (Play.mode == Mode.PROD)
        throw e;/*  www. j a v a  2 s  . c  o  m*/

    // find the latest japidviews exception
    StackTraceElement[] stackTrace = e.getStackTrace();
    for (StackTraceElement ele : stackTrace) {
        String className = ele.getClassName();
        if (className.startsWith("japidviews")) {
            int lineNumber = ele.getLineNumber();
            // TODO: should really remove the Play reference.  Shall we jump to the file system for the source?
            ApplicationClass applicationClass = Play.classes.getApplicationClass(className);
            if (applicationClass != null) {
                // let's get the line of problem
                String jsrc = applicationClass.javaSource;
                String[] splitSrc = jsrc.split("\n");
                String line = splitSrc[lineNumber - 1];
                // can we have a line marker?
                int lineMarker = line.lastIndexOf("// line ");
                if (lineMarker > 0) {
                    int oriLineNumber = Integer.parseInt(line.substring(lineMarker + 8).trim());
                    StackTraceElement[] newStack = new StackTraceElement[stackTrace.length + 1];
                    newStack[0] = new StackTraceElement(sourceTemplate, "", sourceTemplate, oriLineNumber);
                    System.arraycopy(stackTrace, 0, newStack, 1, stackTrace.length);
                    e.setStackTrace(newStack);

                    File file = new File("app/" + sourceTemplate);
                    //         
                    JapidPlayTemplate jpt = new JapidPlayTemplate();
                    jpt.name = sourceTemplate;
                    try {
                        jpt.source = FileUtils.readFileToString(file);
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    throw new TemplateExecutionException(jpt, oriLineNumber, e.getMessage(), e);
                }
            }
        }
    }
    throw e;
}

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

@Test
public void testForceNew() throws Exception {
    final IllegalStateException e = new IllegalStateException("oh, c 'est d\u00e9j\u00e0 cass\u00e9?");
    e.setStackTrace(new StackTraceElement[] {
            new StackTraceElement("org.bug4j.SomeClass", buildRandomMethodName(), "SomeClass.java",
                    (int) (Math.random() * 10000)),
            new StackTraceElement("org.bug4j.SomeClass", buildRandomMethodName(), "SomeClass.java",
                    (int) (Math.random() * 10000)),
            new StackTraceElement("org.bug4j.SomeClass", buildRandomMethodName(), "SomeClass.java",
                    (int) (Math.random() * 10000)),
            new StackTraceElement("org.bug4j.SomeClass", buildRandomMethodName(), "SomeClass.java",
                    (int) (Math.random() * 10000)),
            new StackTraceElement("org.bug4j.SomeClass", buildRandomMethodName(), "SomeClass.java",
                    (int) (Math.random() * 10000)),
            new StackTraceElement("org.bug4j.SomeClass", buildRandomMethodName(), "SomeClass.java",
                    (int) (Math.random() * 10000)),
            new StackTraceElement("org.bug4j.SomeClass", buildRandomMethodName(), "SomeClass.java",
                    (int) (Math.random() * 10000)),
            new StackTraceElement("org.bug4j.SomeClass", buildRandomMethodName(), "SomeClass.java",
                    (int) (Math.random() * 10000)) });
    Bug4jAgent.report("Forcing a new exception", e);

    Bug4jAgent.shutdown();//from  w w w .j a  va2 s.c  om
    Assert.assertEquals(1, Bug4jAgent.getReported());
}

From source file:com.axibase.tsd.driver.jdbc.strategies.Consumer.java

private static List<StackTraceElement> getStackTrace(AtsdExceptionRepresentation section) {
    final List<ExceptionSection> exceptions = section.getException();
    final List<StackTraceElement> list = new ArrayList<>(exceptions.size());
    for (ExceptionSection exc : exceptions) {
        list.add(new StackTraceElement(exc.getClassName(), exc.getMethodName(), exc.getFileName(),
                exc.getLineNumber()));//w  w  w.ja v a  2 s.  c om
    }
    return list;
}