package p138.p395.p396;

import java.io.ObjectInputStream;
import java.io.ObjectStreamClass;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import javax.annotation.Nullable;
import p138.p139.p143.p144.C2803;
import p138.p176.p177.p242.p246.p251.C4303;
import p138.p395.p396.AbstractC6487;
import p138.p395.p396.AbstractC6491;
import p138.p395.p396.p397.C6527;

/* JADX INFO: renamed from: ދ.ބ.֏.ބ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes.dex */
public final class C6476<T> extends AbstractC6487<T> {

    /* JADX INFO: renamed from: ށ, reason: contains not printable characters */
    public static final AbstractC6487.InterfaceC6488 f19173 = new C6477();

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public final AbstractC6475<T> f19174;

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public final C6478<?>[] f19175;

    /* JADX INFO: renamed from: ހ, reason: contains not printable characters */
    public final AbstractC6491.C6492 f19176;

    /* JADX INFO: renamed from: ދ.ބ.֏.ބ$֏, reason: contains not printable characters */
    public class C6477 implements AbstractC6487.InterfaceC6488 {
        @Override // p138.p395.p396.AbstractC6487.InterfaceC6488
        @Nullable
        /* JADX INFO: renamed from: ֏ */
        public AbstractC6487<?> mo9674(Type type, Set<? extends Annotation> set, C6509 c6509) {
            AbstractC6475 c6474;
            Class<Object> cls;
            Class<?> cls2;
            Type typeM9758 = type;
            Class<Object> cls3 = Object.class;
            LinkedHashSet linkedHashSet = null;
            if (!(typeM9758 instanceof Class) && !(typeM9758 instanceof ParameterizedType)) {
                return null;
            }
            Class<?> clsM7003 = C4303.m7003(type);
            if (clsM7003.isInterface() || clsM7003.isEnum() || !set.isEmpty()) {
                return null;
            }
            if (C6527.m9761(clsM7003)) {
                String strM4878 = C2803.m4878("Platform ", clsM7003);
                if (typeM9758 instanceof ParameterizedType) {
                    strM4878 = strM4878 + " in " + typeM9758;
                }
                throw new IllegalArgumentException(C2803.m4879(strM4878, " requires explicit JsonAdapter to be registered"));
            }
            if (clsM7003.isAnonymousClass()) {
                throw new IllegalArgumentException(C2803.m4868(clsM7003, C2803.m4889("Cannot serialize anonymous class ")));
            }
            if (clsM7003.isLocalClass()) {
                throw new IllegalArgumentException(C2803.m4868(clsM7003, C2803.m4889("Cannot serialize local class ")));
            }
            if (clsM7003.getEnclosingClass() != null && !Modifier.isStatic(clsM7003.getModifiers())) {
                throw new IllegalArgumentException(C2803.m4868(clsM7003, C2803.m4889("Cannot serialize non-static nested class ")));
            }
            if (Modifier.isAbstract(clsM7003.getModifiers())) {
                throw new IllegalArgumentException(C2803.m4868(clsM7003, C2803.m4889("Cannot serialize abstract class ")));
            }
            Class<? extends Annotation> cls4 = C6527.f19296;
            int i = 0;
            if (cls4 != null && clsM7003.isAnnotationPresent(cls4)) {
                StringBuilder sbM4889 = C2803.m4889("Cannot serialize Kotlin type ");
                sbM4889.append(clsM7003.getName());
                sbM4889.append(". Reflective serialization of Kotlin classes without using kotlin-reflect has undefined and unexpected behavior. Please use KotlinJsonAdapter from the moshi-kotlin artifact or use code gen from the moshi-kotlin-codegen artifact.");
                throw new IllegalArgumentException(sbM4889.toString());
            }
            try {
                try {
                    Constructor<?> declaredConstructor = clsM7003.getDeclaredConstructor(new Class[0]);
                    declaredConstructor.setAccessible(true);
                    c6474 = new C6471(declaredConstructor, clsM7003);
                } catch (NoSuchMethodException unused) {
                    Class<?> cls5 = Class.forName("sun.misc.Unsafe");
                    Field declaredField = cls5.getDeclaredField("theUnsafe");
                    declaredField.setAccessible(true);
                    c6474 = new C6472(cls5.getMethod("allocateInstance", Class.class), declaredField.get(null), clsM7003);
                }
            } catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException unused2) {
                try {
                    try {
                        Method declaredMethod = ObjectStreamClass.class.getDeclaredMethod("getConstructorId", Class.class);
                        declaredMethod.setAccessible(true);
                        int iIntValue = ((Integer) declaredMethod.invoke(null, cls3)).intValue();
                        Method declaredMethod2 = ObjectStreamClass.class.getDeclaredMethod("newInstance", Class.class, Integer.TYPE);
                        declaredMethod2.setAccessible(true);
                        c6474 = new C6473(declaredMethod2, clsM7003, iIntValue);
                    } catch (Exception unused3) {
                        throw new IllegalArgumentException(C2803.m4868(clsM7003, C2803.m4889("cannot construct instances of ")));
                    }
                } catch (IllegalAccessException unused4) {
                    throw new AssertionError();
                } catch (NoSuchMethodException unused5) {
                    Method declaredMethod3 = ObjectInputStream.class.getDeclaredMethod("newInstance", Class.class, Class.class);
                    declaredMethod3.setAccessible(true);
                    c6474 = new C6474(declaredMethod3, clsM7003);
                } catch (InvocationTargetException e) {
                    C6527.m9754(e);
                    throw null;
                }
            } catch (IllegalAccessException unused6) {
                throw new AssertionError();
            }
            TreeMap treeMap = new TreeMap();
            while (typeM9758 != cls3) {
                Class<?> clsM70032 = C4303.m7003(typeM9758);
                boolean zM9761 = C6527.m9761(clsM70032);
                Field[] declaredFields = clsM70032.getDeclaredFields();
                int length = declaredFields.length;
                int i2 = 0;
                while (i2 < length) {
                    Field field = declaredFields[i2];
                    int modifiers = field.getModifiers();
                    if ((Modifier.isStatic(modifiers) || Modifier.isTransient(modifiers) || (!Modifier.isPublic(modifiers) && !Modifier.isProtected(modifiers) && zM9761)) ? false : true) {
                        Type typeM97582 = C6527.m9758(typeM9758, clsM70032, field.getGenericType());
                        Annotation[] annotations = field.getAnnotations();
                        int length2 = annotations.length;
                        while (i < length2) {
                            Annotation annotation = annotations[i];
                            Class<Object> cls6 = cls3;
                            Class<?> cls7 = clsM70032;
                            if (annotation.annotationType().isAnnotationPresent(InterfaceC6490.class)) {
                                if (linkedHashSet == null) {
                                    linkedHashSet = new LinkedHashSet();
                                }
                                linkedHashSet.add(annotation);
                            }
                            i++;
                            clsM70032 = cls7;
                            cls3 = cls6;
                        }
                        cls = cls3;
                        cls2 = clsM70032;
                        Set<Annotation> setUnmodifiableSet = linkedHashSet != null ? Collections.unmodifiableSet(linkedHashSet) : C6527.f19294;
                        String name = field.getName();
                        AbstractC6487<T> abstractC6487M9748 = c6509.m9748(typeM97582, setUnmodifiableSet, name);
                        field.setAccessible(true);
                        InterfaceC6483 interfaceC6483 = (InterfaceC6483) field.getAnnotation(InterfaceC6483.class);
                        if (interfaceC6483 != null) {
                            name = interfaceC6483.name();
                        }
                        C6478 c6478 = new C6478(name, field, abstractC6487M9748);
                        C6478 c64782 = (C6478) treeMap.put(name, c6478);
                        if (c64782 != null) {
                            StringBuilder sbM48892 = C2803.m4889("Conflicting fields:\n    ");
                            sbM48892.append(c64782.f19178);
                            sbM48892.append("\n    ");
                            sbM48892.append(c6478.f19178);
                            throw new IllegalArgumentException(sbM48892.toString());
                        }
                    } else {
                        cls = cls3;
                        cls2 = clsM70032;
                    }
                    i2++;
                    linkedHashSet = null;
                    i = 0;
                    clsM70032 = cls2;
                    cls3 = cls;
                }
                Class<Object> cls8 = cls3;
                Class<?> clsM70033 = C4303.m7003(typeM9758);
                typeM9758 = C6527.m9758(typeM9758, clsM70033, clsM70033.getGenericSuperclass());
                linkedHashSet = null;
                i = 0;
                cls3 = cls8;
            }
            return new C6476(c6474, treeMap).m9682();
        }
    }

    /* JADX INFO: renamed from: ދ.ބ.֏.ބ$ؠ, reason: contains not printable characters */
    public static class C6478<T> {

        /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
        public final String f19177;

        /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
        public final Field f19178;

        /* JADX INFO: renamed from: ހ, reason: contains not printable characters */
        public final AbstractC6487<T> f19179;

        public C6478(String str, Field field, AbstractC6487<T> abstractC6487) {
            this.f19177 = str;
            this.f19178 = field;
            this.f19179 = abstractC6487;
        }
    }

    public C6476(AbstractC6475<T> abstractC6475, Map<String, C6478<?>> map) {
        this.f19174 = abstractC6475;
        this.f19175 = (C6478[]) map.values().toArray(new C6478[map.size()]);
        this.f19176 = AbstractC6491.C6492.m9699((String[]) map.keySet().toArray(new String[map.size()]));
    }

    public String toString() {
        StringBuilder sbM4889 = C2803.m4889("JsonAdapter(");
        sbM4889.append(this.f19174);
        sbM4889.append(")");
        return sbM4889.toString();
    }

    @Override // p138.p395.p396.AbstractC6487
    /* JADX INFO: renamed from: ֏ */
    public T mo9672(AbstractC6491 abstractC6491) {
        try {
            T tMo9675 = this.f19174.mo9675();
            try {
                abstractC6491.mo9687();
                while (abstractC6491.mo9691()) {
                    int iMo9683 = abstractC6491.mo9683(this.f19176);
                    if (iMo9683 == -1) {
                        abstractC6491.mo9697();
                        abstractC6491.mo9698();
                    } else {
                        C6478<?> c6478 = this.f19175[iMo9683];
                        c6478.f19178.set(tMo9675, c6478.f19179.mo9672(abstractC6491));
                    }
                }
                abstractC6491.mo9689();
                return tMo9675;
            } catch (IllegalAccessException unused) {
                throw new AssertionError();
            }
        } catch (IllegalAccessException unused2) {
            throw new AssertionError();
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e2) {
            C6527.m9754(e2);
            throw null;
        }
    }

    @Override // p138.p395.p396.AbstractC6487
    /* JADX INFO: renamed from: ֏ */
    public void mo9673(AbstractC6497 abstractC6497, T t) {
        try {
            abstractC6497.mo9719();
            for (C6478<?> c6478 : this.f19175) {
                abstractC6497.mo9721(c6478.f19177);
                c6478.f19179.mo9673(abstractC6497, c6478.f19178.get(t));
            }
            abstractC6497.mo9724();
        } catch (IllegalAccessException unused) {
            throw new AssertionError();
        }
    }
}
