// 
// Decompiled by Procyon v0.6.0
// 

package com.google.common.flogger.util;

import java.lang.reflect.InvocationTargetException;
import org.checkerframework.checker.nullness.compatqual.NullableDecl;
import java.lang.reflect.Method;
import com.google.errorprone.annotations.CheckReturnValue;

@CheckReturnValue
final class FastStackGetter
{
    private final Object javaLangAccess;
    private final Method getElementMethod;
    private final Method getDepthMethod;
    
    @NullableDecl
    public static FastStackGetter createIfSupported() {
        try {
            final Object javaLangAccess = Class.forName("sun.misc.SharedSecrets").getMethod("getJavaLangAccess", (Class<?>[])new Class[0]).invoke(null, new Object[0]);
            final Method getElementMethod = Class.forName("sun.misc.JavaLangAccess").getMethod("getStackTraceElement", Throwable.class, Integer.TYPE);
            final Method getDepthMethod = Class.forName("sun.misc.JavaLangAccess").getMethod("getStackTraceDepth", Throwable.class);
            final StackTraceElement unusedElement = (StackTraceElement)getElementMethod.invoke(javaLangAccess, new Throwable(), 0);
            final int unusedDepth = (int)getDepthMethod.invoke(javaLangAccess, new Throwable());
            return new FastStackGetter(javaLangAccess, getElementMethod, getDepthMethod);
        }
        catch (final ThreadDeath t) {
            throw t;
        }
        catch (final Throwable t2) {
            return null;
        }
    }
    
    private FastStackGetter(final Object javaLangAccess, final Method getElementMethod, final Method getDepthMethod) {
        this.javaLangAccess = javaLangAccess;
        this.getElementMethod = getElementMethod;
        this.getDepthMethod = getDepthMethod;
    }
    
    public StackTraceElement getStackTraceElement(final Throwable throwable, final int n) {
        try {
            return (StackTraceElement)this.getElementMethod.invoke(this.javaLangAccess, throwable, n);
        }
        catch (final InvocationTargetException e) {
            if (e.getCause() instanceof RuntimeException) {
                throw (RuntimeException)e.getCause();
            }
            if (e.getCause() instanceof Error) {
                throw (Error)e.getCause();
            }
            throw new RuntimeException(e.getCause());
        }
        catch (final IllegalAccessException e2) {
            throw new AssertionError((Object)e2);
        }
    }
    
    public int getStackTraceDepth(final Throwable throwable) {
        try {
            return (int)this.getDepthMethod.invoke(this.javaLangAccess, throwable);
        }
        catch (final InvocationTargetException e) {
            if (e.getCause() instanceof RuntimeException) {
                throw (RuntimeException)e.getCause();
            }
            if (e.getCause() instanceof Error) {
                throw (Error)e.getCause();
            }
            throw new RuntimeException(e.getCause());
        }
        catch (final IllegalAccessException e2) {
            throw new AssertionError((Object)e2);
        }
    }
}
