package com.google.gson.internal.bind;

import X.2kf;
import com.bytedance.covode.number.Covode;
import com.google.gson.Gson;
import com.google.gson.a.e;
import com.google.gson.d;
import com.google.gson.internal.Excluder;
import com.google.gson.internal.a.b;
import com.google.gson.internal.c;
import com.google.gson.internal.h;
import com.google.gson.internal.j;
import com.google.gson.s;
import com.google.gson.u;
import com.google.gson.v;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public final class ReflectiveTypeAdapterFactory implements v {
    public final d LIZ;
    public final c LIZIZ;
    public final Excluder LIZJ;
    public final JsonAdapterAnnotationTypeAdapterFactory LIZLLL;
    public final b LJ = b.LIZ;

    static {
        Covode.recordClassIndex(45416);
    }

    public static final class Adapter<T> extends u<T> {
        public final h<T> LIZ;
        public final Map<String, a> LIZIZ;

        static {
            Covode.recordClassIndex(45418);
        }

        /* JADX WARNING: type inference failed for: r0v2, types: [java.lang.Throwable, com.google.gson.s] */
        public final T read(com.google.gson.c.a aVar) {
            if (aVar.LJFF() == com.google.gson.c.b.NULL) {
                aVar.LJIIJ();
                return null;
            }
            T LIZ2 = this.LIZ.LIZ();
            try {
                aVar.LIZJ();
                while (aVar.LJ()) {
                    a aVar2 = this.LIZIZ.get(aVar.LJII());
                    if (aVar2 == null || !aVar2.LJIIIZ) {
                        aVar.LJIILJJIL();
                    } else {
                        aVar2.LIZ(aVar, (Object) LIZ2);
                    }
                }
                aVar.LIZLLL();
                return LIZ2;
            } catch (IllegalStateException e) {
                throw new s((Throwable) e);
            } catch (IllegalAccessException e2) {
                throw new AssertionError(e2);
            }
        }

        public Adapter(h<T> hVar, Map<String, a> map) {
            this.LIZ = hVar;
            this.LIZIZ = map;
        }

        public final void write(com.google.gson.c.c cVar, T t) {
            if (t == null) {
                cVar.LJFF();
                return;
            }
            cVar.LIZLLL();
            try {
                for (a next : this.LIZIZ.values()) {
                    if (next.LIZ(t)) {
                        cVar.LIZ(next.LJII);
                        next.LIZ(cVar, (Object) t);
                    }
                }
                cVar.LJ();
            } catch (IllegalAccessException e) {
                throw new AssertionError(e);
            }
        }
    }

    public final <T> u<T> create(Gson gson, com.google.gson.b.a<T> aVar) {
        Class<? super T> cls = aVar.rawType;
        if (!Object.class.isAssignableFrom(cls)) {
            return null;
        }
        return new Adapter(this.LIZIZ.LIZ(aVar), LIZ(gson, aVar, cls));
    }

    public static List LIZ(ReflectiveTypeAdapterFactory reflectiveTypeAdapterFactory, Field field) {
        try {
            com.google.gson.a.c cVar = (com.google.gson.a.c) field.getAnnotation(com.google.gson.a.c.class);
            if (cVar == null) {
                return Collections.singletonList(reflectiveTypeAdapterFactory.LIZ.LIZ(field));
            }
            String LIZ2 = cVar.LIZ();
            String[] LIZIZ2 = cVar.LIZIZ();
            if (LIZIZ2.length == 0) {
                return Collections.singletonList(LIZ2);
            }
            ArrayList arrayList = new ArrayList(LIZIZ2.length + 1);
            arrayList.add(LIZ2);
            for (String add : LIZIZ2) {
                arrayList.add(add);
            }
            return arrayList;
        } catch (IncompatibleClassChangeError e) {
            2kf.LIZ(e);
            return Collections.singletonList(reflectiveTypeAdapterFactory.LIZ.LIZ(field));
        }
    }

    private boolean LIZ(Field field, boolean z) {
        List<com.google.gson.a> list;
        com.google.gson.a.a aVar;
        Excluder excluder = this.LIZJ;
        Class<?> type = field.getType();
        if (excluder.LIZ(type) || excluder.LIZ(type, z) || (excluder.LIZJ & field.getModifiers()) != 0 || ((excluder.LIZIZ != -1.0d && !excluder.LIZ((com.google.gson.a.d) field.getAnnotation(com.google.gson.a.d.class), (e) field.getAnnotation(e.class))) || field.isSynthetic() || ((excluder.LJ && ((aVar = (com.google.gson.a.a) field.getAnnotation(com.google.gson.a.a.class)) == null || (!z ? !aVar.LIZIZ() : !aVar.LIZ()))) || ((!excluder.LIZLLL && excluder.LIZJ(field.getType())) || excluder.LIZIZ(field.getType()))))) {
            return false;
        }
        if (z) {
            list = excluder.LJFF;
        } else {
            list = excluder.LJI;
        }
        if (!list.isEmpty()) {
            com.google.gson.b bVar = new com.google.gson.b(field);
            for (com.google.gson.a shouldSkipField : list) {
                if (shouldSkipField.shouldSkipField(bVar)) {
                    return false;
                }
            }
        }
        return true;
    }

    public static abstract class a {
        public final String LJII;
        public final boolean LJIIIIZZ;
        public final boolean LJIIIZ;

        static {
            Covode.recordClassIndex(45419);
        }

        public abstract void LIZ(com.google.gson.c.a aVar, Object obj);

        public abstract void LIZ(com.google.gson.c.c cVar, Object obj);

        public abstract boolean LIZ(Object obj);

        public a(String str, boolean z, boolean z2) {
            this.LJII = str;
            this.LJIIIIZZ = z;
            this.LJIIIZ = z2;
        }
    }

    private Map<String, a> LIZ(Gson gson, com.google.gson.b.a<?> aVar, Class<?> cls) {
        Class cls2;
        com.google.gson.b.a<?> aVar2;
        Field[] fieldArr;
        int i;
        int i2;
        Type type;
        boolean z;
        int i3;
        boolean z2;
        u<?> LIZ2;
        ReflectiveTypeAdapterFactory reflectiveTypeAdapterFactory = this;
        Gson gson2 = gson;
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        if (cls.isInterface()) {
            return linkedHashMap;
        }
        com.google.gson.b.a<?> aVar3 = aVar;
        Type type2 = aVar3.type;
        Class cls3 = cls;
        com.google.gson.b.a<?> aVar4 = aVar3;
        while (cls3 != Object.class) {
            Field[] declaredFields = cls3.getDeclaredFields();
            int length = declaredFields.length;
            boolean z3 = false;
            int i4 = 0;
            while (i4 < length) {
                final Field field = declaredFields[i4];
                boolean LIZ3 = reflectiveTypeAdapterFactory.LIZ(field, true);
                boolean LIZ4 = reflectiveTypeAdapterFactory.LIZ(field, z3);
                if (LIZ3 || LIZ4) {
                    reflectiveTypeAdapterFactory.LJ.LIZ(field);
                    Type LIZ5 = com.google.gson.internal.b.LIZ(aVar4.type, (Class<?>) cls3, field.getGenericType());
                    List LIZ6 = LIZ(reflectiveTypeAdapterFactory, field);
                    int size = LIZ6.size();
                    aVar2 = aVar4;
                    a aVar5 = null;
                    int i5 = 0;
                    while (i5 < size) {
                        Type type3 = type2;
                        String str = (String) LIZ6.get(i5);
                        if (i5 != 0) {
                            z = false;
                        } else {
                            z = LIZ3;
                        }
                        com.google.gson.b.a<?> aVar6 = com.google.gson.b.a.get(LIZ5);
                        boolean containsKey = j.LIZ.containsKey(aVar6.rawType);
                        com.google.gson.a.b bVar = (com.google.gson.a.b) field.getAnnotation(com.google.gson.a.b.class);
                        if (bVar != null) {
                            i3 = i5;
                            LIZ2 = reflectiveTypeAdapterFactory.LIZLLL.LIZ(reflectiveTypeAdapterFactory.LIZIZ, gson2, aVar6, bVar);
                            if (LIZ2 != null) {
                                z2 = true;
                            }
                            z2 = false;
                            LIZ2 = gson2.LIZ(aVar6);
                        } else {
                            i3 = i5;
                            z2 = false;
                            LIZ2 = gson2.LIZ(aVar6);
                        }
                        int i6 = size;
                        List list = LIZ6;
                        AnonymousClass1 r11 = r0;
                        Field field2 = field;
                        int i7 = i4;
                        final boolean z4 = z2;
                        final u<?> uVar = LIZ2;
                        int i8 = length;
                        final Gson gson3 = gson;
                        Field[] fieldArr2 = declaredFields;
                        final com.google.gson.b.a<?> aVar7 = aVar6;
                        Class cls4 = cls3;
                        final boolean z5 = containsKey;
                        AnonymousClass1 r0 = new a(str, z, LIZ4) {
                            static {
                                Covode.recordClassIndex(45417);
                            }

                            public final boolean LIZ(Object obj) {
                                if (this.LJIIIIZZ && field.get(obj) != obj) {
                                    return true;
                                }
                                return false;
                            }

                            public final void LIZ(com.google.gson.c.a aVar, Object obj) {
                                Object read = uVar.read(aVar);
                                if (read != null || !z5) {
                                    field.set(obj, read);
                                }
                            }

                            public final void LIZ(com.google.gson.c.c cVar, Object obj) {
                                u typeAdapterRuntimeTypeWrapper;
                                Object obj2 = field.get(obj);
                                if (z4) {
                                    typeAdapterRuntimeTypeWrapper = uVar;
                                } else {
                                    typeAdapterRuntimeTypeWrapper = new TypeAdapterRuntimeTypeWrapper(gson3, uVar, aVar7.type);
                                }
                                typeAdapterRuntimeTypeWrapper.write(cVar, obj2);
                            }
                        };
                        a aVar8 = (a) linkedHashMap.put(str, r11);
                        if (aVar5 == null) {
                            aVar5 = aVar8;
                        }
                        i5 = i3 + 1;
                        reflectiveTypeAdapterFactory = this;
                        cls3 = cls4;
                        LIZ3 = z;
                        type2 = type3;
                        length = i8;
                        size = i6;
                        LIZ6 = list;
                        field = field2;
                        i4 = i7;
                        declaredFields = fieldArr2;
                    }
                    i = i4;
                    i2 = length;
                    fieldArr = declaredFields;
                    cls2 = cls3;
                    type = type2;
                    if (aVar5 != null) {
                        throw new IllegalArgumentException(type + " declares multiple JSON fields named " + aVar5.LJII);
                    }
                } else {
                    i = i4;
                    i2 = length;
                    fieldArr = declaredFields;
                    cls2 = cls3;
                    type = type2;
                    aVar2 = aVar4;
                }
                i4 = i + 1;
                reflectiveTypeAdapterFactory = this;
                aVar4 = aVar2;
                cls3 = cls2;
                type2 = type;
                length = i2;
                declaredFields = fieldArr;
                z3 = false;
            }
            Class cls5 = cls3;
            Type type4 = type2;
            aVar4 = com.google.gson.b.a.get(com.google.gson.internal.b.LIZ(aVar4.type, (Class<?>) cls5, cls5.getGenericSuperclass()));
            cls3 = aVar4.rawType;
            reflectiveTypeAdapterFactory = this;
        }
        return linkedHashMap;
    }

    public ReflectiveTypeAdapterFactory(c cVar, d dVar, Excluder excluder, JsonAdapterAnnotationTypeAdapterFactory jsonAdapterAnnotationTypeAdapterFactory) {
        this.LIZIZ = cVar;
        this.LIZ = dVar;
        this.LIZJ = excluder;
        this.LIZLLL = jsonAdapterAnnotationTypeAdapterFactory;
    }
}
