Java Throwable to Root Cause getRootCauseStackTrace(@Nonnull final Throwable throwable)

Here you can find the source of getRootCauseStackTrace(@Nonnull final Throwable throwable)

Description

Yoinked from apache commons 3 because that's what we were using.

License

Open Source License

Declaration

@Nonnull
@Deprecated
static String[] getRootCauseStackTrace(@Nonnull final Throwable throwable) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import javax.annotation.Nonnull;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class Main {
    /**//from ww  w .j a  v  a  2s  .c  om
     * Yoinked from apache commons 3 because that's what we were using.
     * Keeping this method because of backward compatibility reasons.
     * We don't want commons-lang-3 as a dependency though.
     *
     * @deprecated figure out how to stop using this.
     */
    @Nonnull
    @Deprecated
    static String[] getRootCauseStackTrace(@Nonnull final Throwable throwable) {
        final Throwable throwables[] = getThrowables(throwable);
        final int count = throwables.length;
        final List<String> frames = new ArrayList<>();
        List<String> nextTrace = getStackFrameList(throwables[count - 1]);
        for (int i = count; --i >= 0;) {
            final List<String> trace = nextTrace;
            if (i != 0) {
                nextTrace = getStackFrameList(throwables[i - 1]);
                removeCommonFrames(trace, nextTrace);
            }
            if (i == count - 1) {
                frames.add(throwables[i].toString());
            } else {
                frames.add(" [wrapped] " + throwables[i].toString());
            }
            for (int j = 0; j < trace.size(); j++) {
                frames.add(trace.get(j));
            }
        }
        return frames.toArray(new String[frames.size()]);
    }

    /**
     * Yoinked from apache commons 3 because that's what we were using.
     * Keeping this method because of backward compatibility reasons.
     * We don't want commons-lang-3 as a dependency though.
     *
     * @deprecated figure out how to stop using this.
     */
    @Nonnull
    @Deprecated
    private static Throwable[] getThrowables(Throwable throwable) {
        final List<Throwable> list = new ArrayList<>();
        while (throwable != null && !list.contains(throwable)) {
            list.add(throwable);
            throwable = throwable.getCause();
        }
        return list.toArray(new Throwable[list.size()]);
    }

    /**
     * Yoinked from apache commons 3 because that's what we were using.
     * Keeping this method because of backward compatibility reasons.
     * We don't want commons-lang-3 as a dependency though.
     *
     * @deprecated figure out how to stop using this.
     */
    @Nonnull
    @Deprecated
    private static List<String> getStackFrameList(@Nonnull final Throwable t) {
        final String stackTrace = getStackTrace(t);
        final String linebreak = System.lineSeparator();
        final StringTokenizer frames = new StringTokenizer(stackTrace, linebreak);
        final List<String> list = new ArrayList<>();
        boolean traceStarted = false;
        while (frames.hasMoreTokens()) {
            final String token = frames.nextToken();
            // Determine if the line starts with <whitespace>at
            final int at = token.indexOf("at");
            if (at != -1 && token.substring(0, at).trim().isEmpty()) {
                traceStarted = true;
                list.add(token);
            } else if (traceStarted) {
                break;
            }
        }
        return list;
    }

    /**
     * Yoinked from apache commons 3 because that's what we were using.
     * Keeping this method because of backward compatibility reasons.
     * We don't want commons-lang-3 as a dependency though.
     *
     * @deprecated figure out how to stop using this.
     */
    @Nonnull
    @Deprecated
    private static void removeCommonFrames(final List<String> causeFrames, final List<String> wrapperFrames) {
        if (causeFrames == null || wrapperFrames == null) {
            throw new IllegalArgumentException("The List must not be null");
        }
        int causeFrameIndex = causeFrames.size() - 1;
        int wrapperFrameIndex = wrapperFrames.size() - 1;
        while (causeFrameIndex >= 0 && wrapperFrameIndex >= 0) {
            // Remove the frame from the cause trace if it is the same
            // as in the wrapper trace
            final String causeFrame = causeFrames.get(causeFrameIndex);
            final String wrapperFrame = wrapperFrames.get(wrapperFrameIndex);
            if (causeFrame.equals(wrapperFrame)) {
                causeFrames.remove(causeFrameIndex);
            }
            causeFrameIndex--;
            wrapperFrameIndex--;
        }
    }

    /**
     * Yoinked from apache commons 3 because that's what we were using.
     * Keeping this method because of backward compatibility reasons.
     * We don't want commons-lang-3 as a dependency though.
     *
     * @deprecated figure out how to stop using this.
     */
    @Nonnull
    @Deprecated
    private static String getStackTrace(@Nonnull final Throwable throwable) {
        final StringWriter sw = new StringWriter();
        final PrintWriter pw = new PrintWriter(sw, true);
        throwable.printStackTrace(pw);
        return sw.getBuffer().toString();
    }
}

Related

  1. getRootCauseStackTrace(Throwable throwable)
  2. getRootStackTrace(Throwable t)