package kotlinx.coroutines.o2;

import h.l;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.util.Comparator;
import java.util.Iterator;
import java.util.WeakHashMap;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import kotlinx.coroutines.g0;

/* JADX INFO: compiled from: ExceptionsConstuctor.kt */
/* JADX INFO: loaded from: classes.dex */
public final class g {
    private static final int a = d(Throwable.class, -1);
    private static final ReentrantReadWriteLock b = new ReentrantReadWriteLock();

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private static final WeakHashMap<Class<? extends Throwable>, h.x.c.l<Throwable, Throwable>> f1109c = new WeakHashMap<>();

    /* JADX INFO: compiled from: ExceptionsConstuctor.kt */
    public static final class a extends h.x.d.l implements h.x.c.l<Throwable, Throwable> {
        final /* synthetic */ Constructor b;

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

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

    /* JADX INFO: compiled from: ExceptionsConstuctor.kt */
    public static final class b extends h.x.d.l implements h.x.c.l<Throwable, Throwable> {
        final /* synthetic */ Constructor b;

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

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

    /* JADX INFO: compiled from: ExceptionsConstuctor.kt */
    public static final class c extends h.x.d.l implements h.x.c.l<Throwable, Throwable> {
        final /* synthetic */ Constructor b;

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

        @Override // h.x.c.l
        /* JADX INFO: renamed from: b, reason: merged with bridge method [inline-methods] */
        public final Throwable invoke(Throwable th) {
            Object obj;
            Object objNewInstance;
            try {
                l.a aVar = h.l.b;
                objNewInstance = this.b.newInstance(th.getMessage());
            } catch (Throwable th2) {
                l.a aVar2 = h.l.b;
                Object objA = h.m.a(th2);
                h.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);
            h.l.a(th3);
            obj = th3;
            boolean zC = h.l.c(obj);
            Object obj2 = obj;
            if (zC) {
                obj2 = null;
            }
            return (Throwable) obj2;
        }
    }

    /* JADX INFO: compiled from: ExceptionsConstuctor.kt */
    public static final class d extends h.x.d.l implements h.x.c.l<Throwable, Throwable> {
        final /* synthetic */ Constructor b;

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

        @Override // h.x.c.l
        /* JADX INFO: renamed from: b, reason: merged with bridge method [inline-methods] */
        public final Throwable invoke(Throwable th) {
            Object obj;
            Object objNewInstance;
            try {
                l.a aVar = h.l.b;
                objNewInstance = this.b.newInstance(new Object[0]);
            } catch (Throwable th2) {
                l.a aVar2 = h.l.b;
                Object objA = h.m.a(th2);
                h.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);
            h.l.a(th3);
            obj = th3;
            boolean zC = h.l.c(obj);
            Object obj2 = obj;
            if (zC) {
                obj2 = null;
            }
            return (Throwable) obj2;
        }
    }

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

    /* JADX INFO: compiled from: ExceptionsConstuctor.kt */
    static final class f extends h.x.d.l implements h.x.c.l {
        public static final f b = new f();

        f() {
            super(1);
        }

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

    /* JADX INFO: renamed from: kotlinx.coroutines.o2.g$g, reason: collision with other inner class name */
    /* JADX INFO: compiled from: ExceptionsConstuctor.kt */
    static final class C0067g extends h.x.d.l implements h.x.c.l {
        public static final C0067g b = new C0067g();

        C0067g() {
            super(1);
        }

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

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

    private static final int b(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 c(Class cls, int i2, int i3, Object obj) {
        if ((i3 & 1) != 0) {
            i2 = 0;
        }
        return b(cls, i2);
    }

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

    public static final <E extends Throwable> E e(E e2) {
        Object objA;
        if (e2 instanceof g0) {
            try {
                l.a aVar = h.l.b;
                objA = ((g0) e2).a();
                h.l.a(objA);
            } catch (Throwable th) {
                l.a aVar2 = h.l.b;
                objA = h.m.a(th);
                h.l.a(objA);
            }
            return (E) (h.l.c(objA) ? null : objA);
        }
        ReentrantReadWriteLock.ReadLock lock = b.readLock();
        lock.lock();
        try {
            h.x.c.l<Throwable, Throwable> lVar = f1109c.get(e2.getClass());
            if (lVar != null) {
                return (E) lVar.invoke(e2);
            }
            int i2 = 0;
            if (a != d(e2.getClass(), 0)) {
                ReentrantReadWriteLock reentrantReadWriteLock = b;
                ReentrantReadWriteLock.ReadLock lock2 = reentrantReadWriteLock.readLock();
                int readHoldCount = reentrantReadWriteLock.getWriteHoldCount() == 0 ? reentrantReadWriteLock.getReadHoldCount() : 0;
                for (int i3 = 0; i3 < readHoldCount; i3++) {
                    lock2.unlock();
                }
                ReentrantReadWriteLock.WriteLock writeLock = reentrantReadWriteLock.writeLock();
                writeLock.lock();
                try {
                    f1109c.put((Class<? extends Throwable>) e2.getClass(), f.b);
                    h.r rVar = h.r.a;
                    return null;
                } finally {
                    while (i2 < readHoldCount) {
                        lock2.lock();
                        i2++;
                    }
                    writeLock.unlock();
                }
            }
            Iterator it = h.s.h.z(e2.getClass().getConstructors(), new e()).iterator();
            h.x.c.l<Throwable, Throwable> lVarA = null;
            while (it.hasNext() && (lVarA = a((Constructor) it.next())) == null) {
            }
            ReentrantReadWriteLock reentrantReadWriteLock2 = b;
            ReentrantReadWriteLock.ReadLock lock3 = reentrantReadWriteLock2.readLock();
            int readHoldCount2 = reentrantReadWriteLock2.getWriteHoldCount() == 0 ? reentrantReadWriteLock2.getReadHoldCount() : 0;
            for (int i4 = 0; i4 < readHoldCount2; i4++) {
                lock3.unlock();
            }
            ReentrantReadWriteLock.WriteLock writeLock2 = reentrantReadWriteLock2.writeLock();
            writeLock2.lock();
            try {
                f1109c.put((Class<? extends Throwable>) e2.getClass(), lVarA == null ? C0067g.b : lVarA);
                h.r rVar2 = h.r.a;
                while (i2 < readHoldCount2) {
                    lock3.lock();
                    i2++;
                }
                writeLock2.unlock();
                if (lVarA == null) {
                    return null;
                }
                return (E) lVarA.invoke(e2);
            } finally {
                while (i2 < readHoldCount2) {
                    lock3.lock();
                    i2++;
                }
                writeLock2.unlock();
            }
        } finally {
            lock.unlock();
        }
    }
}
