package g.a.n2;

import f.l;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.util.Comparator;
import java.util.Iterator;

/* JADX INFO: compiled from: ExceptionsConstructor.kt */
/* JADX INFO: loaded from: classes.dex */
public final class i {
    private static final int a = f(Throwable.class, -1);

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private static final g.a.n2.f f4071b;

    /* JADX INFO: compiled from: Comparisons.kt */
    public static final class a<T> implements Comparator {
        /* JADX WARN: Multi-variable type inference failed */
        @Override // java.util.Comparator
        public final int compare(T t, T t2) {
            return f.v.b.a(Integer.valueOf(((Constructor) t2).getParameterTypes().length), Integer.valueOf(((Constructor) t).getParameterTypes().length));
        }
    }

    /* JADX INFO: compiled from: ExceptionsConstructor.kt */
    static final class b extends f.z.d.l implements f.z.c.l {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        public static final b f4072b = new b();

        b() {
            super(1);
        }

        @Override // f.z.c.l
        /* JADX INFO: renamed from: b, reason: merged with bridge method [inline-methods] */
        public final Void c(Throwable th) {
            return null;
        }
    }

    /* JADX INFO: compiled from: ExceptionsConstructor.kt */
    public static final class c extends f.z.d.l implements f.z.c.l<Throwable, Throwable> {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        final /* synthetic */ Constructor f4073b;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public c(Constructor constructor) {
            super(1);
            this.f4073b = constructor;
        }

        @Override // f.z.c.l
        /* JADX INFO: renamed from: b, reason: merged with bridge method [inline-methods] */
        public final Throwable c(Throwable th) {
            Object objA;
            Object objNewInstance;
            try {
                l.a aVar = f.l.f3940b;
                objNewInstance = this.f4073b.newInstance(th.getMessage(), th);
            } catch (Throwable th2) {
                l.a aVar2 = f.l.f3940b;
                objA = f.m.a(th2);
                f.l.a(objA);
            }
            if (objNewInstance == null) {
                throw new NullPointerException("null cannot be cast to non-null type kotlin.Throwable");
            }
            objA = (Throwable) objNewInstance;
            f.l.a(objA);
            if (f.l.c(objA)) {
                objA = null;
            }
            return (Throwable) objA;
        }
    }

    /* JADX INFO: compiled from: ExceptionsConstructor.kt */
    public static final class d extends f.z.d.l implements f.z.c.l<Throwable, Throwable> {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        final /* synthetic */ Constructor f4074b;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public d(Constructor constructor) {
            super(1);
            this.f4074b = constructor;
        }

        @Override // f.z.c.l
        /* JADX INFO: renamed from: b, reason: merged with bridge method [inline-methods] */
        public final Throwable c(Throwable th) {
            Object objA;
            Object objNewInstance;
            try {
                l.a aVar = f.l.f3940b;
                objNewInstance = this.f4074b.newInstance(th);
            } catch (Throwable th2) {
                l.a aVar2 = f.l.f3940b;
                objA = f.m.a(th2);
                f.l.a(objA);
            }
            if (objNewInstance == null) {
                throw new NullPointerException("null cannot be cast to non-null type kotlin.Throwable");
            }
            objA = (Throwable) objNewInstance;
            f.l.a(objA);
            if (f.l.c(objA)) {
                objA = null;
            }
            return (Throwable) objA;
        }
    }

    /* JADX INFO: compiled from: ExceptionsConstructor.kt */
    public static final class e extends f.z.d.l implements f.z.c.l<Throwable, Throwable> {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        final /* synthetic */ Constructor f4075b;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public e(Constructor constructor) {
            super(1);
            this.f4075b = constructor;
        }

        @Override // f.z.c.l
        /* JADX INFO: renamed from: b, reason: merged with bridge method [inline-methods] */
        public final Throwable c(Throwable th) {
            Object obj;
            Object objNewInstance;
            try {
                l.a aVar = f.l.f3940b;
                objNewInstance = this.f4075b.newInstance(th.getMessage());
            } catch (Throwable th2) {
                l.a aVar2 = f.l.f3940b;
                Object objA = f.m.a(th2);
                f.l.a(objA);
                obj = objA;
            }
            if (objNewInstance == null) {
                throw new NullPointerException("null cannot be cast to non-null type kotlin.Throwable");
            }
            Throwable th3 = (Throwable) objNewInstance;
            th3.initCause(th);
            f.l.a(th3);
            obj = th3;
            boolean zC = f.l.c(obj);
            Object obj2 = obj;
            if (zC) {
                obj2 = null;
            }
            return (Throwable) obj2;
        }
    }

    /* JADX INFO: compiled from: ExceptionsConstructor.kt */
    public static final class f extends f.z.d.l implements f.z.c.l<Throwable, Throwable> {

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        final /* synthetic */ Constructor f4076b;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public f(Constructor constructor) {
            super(1);
            this.f4076b = constructor;
        }

        @Override // f.z.c.l
        /* JADX INFO: renamed from: b, reason: merged with bridge method [inline-methods] */
        public final Throwable c(Throwable th) {
            Object obj;
            Object objNewInstance;
            try {
                l.a aVar = f.l.f3940b;
                objNewInstance = this.f4076b.newInstance(new Object[0]);
            } catch (Throwable th2) {
                l.a aVar2 = f.l.f3940b;
                Object objA = f.m.a(th2);
                f.l.a(objA);
                obj = objA;
            }
            if (objNewInstance == null) {
                throw new NullPointerException("null cannot be cast to non-null type kotlin.Throwable");
            }
            Throwable th3 = (Throwable) objNewInstance;
            th3.initCause(th);
            f.l.a(th3);
            obj = th3;
            boolean zC = f.l.c(obj);
            Object obj2 = obj;
            if (zC) {
                obj2 = null;
            }
            return (Throwable) obj2;
        }
    }

    static {
        g.a.n2.f fVar;
        try {
            fVar = k.a() ? j0.a : g.a.n2.d.a;
        } catch (Throwable unused) {
            fVar = j0.a;
        }
        f4071b = fVar;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final <E extends Throwable> f.z.c.l<Throwable, Throwable> b(Class<E> cls) {
        b bVar = b.f4072b;
        if (a != f(cls, 0)) {
            return bVar;
        }
        Iterator it = f.u.f.m(cls.getConstructors(), new a()).iterator();
        while (it.hasNext()) {
            f.z.c.l<Throwable, Throwable> lVarC = c((Constructor) it.next());
            if (lVarC != null) {
                return lVarC;
            }
        }
        return bVar;
    }

    private static final f.z.c.l<Throwable, Throwable> c(Constructor<?> constructor) {
        Class<?>[] parameterTypes = constructor.getParameterTypes();
        int length = parameterTypes.length;
        if (length == 0) {
            return new f(constructor);
        }
        if (length != 1) {
            if (length == 2 && f.z.d.k.a(parameterTypes[0], String.class) && f.z.d.k.a(parameterTypes[1], Throwable.class)) {
                return new c(constructor);
            }
            return null;
        }
        Class<?> cls = parameterTypes[0];
        if (f.z.d.k.a(cls, Throwable.class)) {
            return new d(constructor);
        }
        if (f.z.d.k.a(cls, String.class)) {
            return new e(constructor);
        }
        return null;
    }

    private static final int d(Class<?> cls, int i2) {
        do {
            int length = cls.getDeclaredFields().length;
            int i3 = 0;
            for (int i4 = 0; i4 < length; i4++) {
                if (!Modifier.isStatic(r0[i4].getModifiers())) {
                    i3++;
                }
            }
            i2 += i3;
            cls = cls.getSuperclass();
        } while (cls != null);
        return i2;
    }

    static /* synthetic */ int e(Class cls, int i2, int i3, Object obj) {
        if ((i3 & 1) != 0) {
            i2 = 0;
        }
        return d(cls, i2);
    }

    private static final int f(Class<?> cls, int i2) {
        Object objA;
        f.z.a.c(cls);
        try {
            l.a aVar = f.l.f3940b;
            objA = Integer.valueOf(e(cls, 0, 1, null));
            f.l.a(objA);
        } catch (Throwable th) {
            l.a aVar2 = f.l.f3940b;
            objA = f.m.a(th);
            f.l.a(objA);
        }
        Integer numValueOf = Integer.valueOf(i2);
        if (f.l.c(objA)) {
            objA = numValueOf;
        }
        return ((Number) objA).intValue();
    }

    /* JADX WARN: Multi-variable type inference failed */
    public static final <E extends Throwable> E g(E e2) {
        Object objA;
        if (!(e2 instanceof g.a.z)) {
            return (E) f4071b.a(e2.getClass()).c(e2);
        }
        try {
            l.a aVar = f.l.f3940b;
            objA = ((g.a.z) e2).a();
            f.l.a(objA);
        } catch (Throwable th) {
            l.a aVar2 = f.l.f3940b;
            objA = f.m.a(th);
            f.l.a(objA);
        }
        if (f.l.c(objA)) {
            objA = null;
        }
        return (E) objA;
    }
}
