public static SentryStackTraceElement[] fromStackTraceElements(StackTraceElement[] stackTraceElements,
                                                                   Frame[] cachedFrames) {

        /*
        This loop is a bit hairy because it has to deal with cached frames (from FrameCache, set by
        the agent if it is in use).

        - If cachedFrames is null (most commonly: because the agent isn't being used) nothing fancy
          needs to happen.
        - If cachedFrames is not null we need to pair frames from stackTraceElements (the exception
          being captured) to frames found in the FrameCache.

          The issue is that some frameworks/libraries seem to trim stacktraces in some cases, and
          so the array in the FrameCache (set when the original exception was *thrown*) may slightly
          differ (be larger) than the actual exception that is being captured. For this reason we
          need to iterate cachedFrames separately, sometimes advancing 'further' than the equivalent
          index in stackTraceElements (thus skipping elements). In addition, the only information
          we have to "match" frames with is the method name, which is checked for equality between
          the two arrays at each step.

          In the worst case, if something is mangled or weird, we just iterate through the cachedFrames
          immediately (not finding a match) and locals are not set/sent with the event.
         */
        SentryStackTraceElement[] sentryStackTraceElements = new SentryStackTraceElement[stackTraceElements.length];
        for (int i = 0, j = 0; i < stackTraceElements.length; i++, j++) {
            StackTraceElement stackTraceElement = stackTraceElements[i];

            Map<String, Object> locals = null;
            if (cachedFrames != null) {
                // step through cachedFrames until we hit a match on the method in the stackTraceElement
                while (j < cachedFrames.length
                    && !cachedFrames[j].getMethod().getName().equals(stackTraceElement.getMethodName())) {
                    j++;
                }

                // only use cachedFrame locals if we haven't exhausted the array
                if (j < cachedFrames.length) {
                    locals = cachedFrames[j].getLocals();
                }
            }

            sentryStackTraceElements[i] = fromStackTraceElement(stackTraceElement, locals);
        }

        return sentryStackTraceElements;
    }
public static SentryStackTraceElement [ ] fromStackTraceElements ( StackTraceElement [ ] stackTraceElements , Frame [ ] cachedFrames ) { SentryStackTraceElement [ ] sentryStackTraceElements = new SentryStackTraceElement [ stackTraceElements . length ] ; for ( int i = 0 , j = 0 ; i < stackTraceElements . length ; i ++ , j ++ ) { StackTraceElement stackTraceElement = stackTraceElements [ i ] ; Map < String , Object > locals = null ; if ( cachedFrames != null ) { while ( j < cachedFrames . length && ! cachedFrames [ j ] . getMethod ( ) . getName ( ) . equals ( stackTraceElement . getMethodName ( ) ) ) { j ++ ; } if ( j < cachedFrames . length ) { locals = cachedFrames [ j ] . getLocals ( ) ; } } sentryStackTraceElements [ i ] = fromStackTraceElement ( stackTraceElement , locals ) ; } return sentryStackTraceElements ; }
Convert an array of {@link StackTraceElement}s to {@link SentryStackTraceElement}s.

@param stackTraceElements Array of {@link StackTraceElement}s to convert.
@param cachedFrames Array of cached {@link Frame}s (from the Sentry agent) if available,
or null.
@return Array of {@link SentryStackTraceElement}s.