package com.google.gson.internal.bind;

import com.google.gson.internal.Excluder;
import com.google.gson.q;
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;
import uk.h;
import uk.j;

/* loaded from: classes3.dex */
public final class ReflectiveTypeAdapterFactory implements v {

    /* renamed from: p, reason: collision with root package name */
    private final uk.c f14903p;

    /* renamed from: q, reason: collision with root package name */
    private final com.google.gson.c f14904q;

    /* renamed from: r, reason: collision with root package name */
    private final Excluder f14905r;

    /* renamed from: s, reason: collision with root package name */
    private final JsonAdapterAnnotationTypeAdapterFactory f14906s;

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes3.dex */
    public class a extends c {

        /* renamed from: d, reason: collision with root package name */
        final /* synthetic */ Field f14907d;

        /* renamed from: e, reason: collision with root package name */
        final /* synthetic */ boolean f14908e;

        /* renamed from: f, reason: collision with root package name */
        final /* synthetic */ u f14909f;

        /* renamed from: g, reason: collision with root package name */
        final /* synthetic */ com.google.gson.d f14910g;

        /* renamed from: h, reason: collision with root package name */
        final /* synthetic */ com.google.gson.reflect.a f14911h;

        /* renamed from: i, reason: collision with root package name */
        final /* synthetic */ boolean f14912i;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        a(String str, boolean z10, boolean z11, Field field, boolean z12, u uVar, com.google.gson.d dVar, com.google.gson.reflect.a aVar, boolean z13) {
            super(str, z10, z11);
            this.f14907d = field;
            this.f14908e = z12;
            this.f14909f = uVar;
            this.f14910g = dVar;
            this.f14911h = aVar;
            this.f14912i = z13;
        }

        @Override // com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.c
        void a(xk.a aVar, Object obj) {
            Object read = this.f14909f.read(aVar);
            if (read == null && this.f14912i) {
                return;
            }
            this.f14907d.set(obj, read);
        }

        @Override // com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.c
        void b(xk.c cVar, Object obj) {
            (this.f14908e ? this.f14909f : new d(this.f14910g, this.f14909f, this.f14911h.getType())).write(cVar, this.f14907d.get(obj));
        }

        @Override // com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.c
        public boolean c(Object obj) {
            return this.f14917b && this.f14907d.get(obj) != obj;
        }
    }

    /* loaded from: classes3.dex */
    public static final class b extends u {

        /* renamed from: a, reason: collision with root package name */
        private final h f14914a;

        /* renamed from: b, reason: collision with root package name */
        private final Map f14915b;

        b(h hVar, Map map) {
            this.f14914a = hVar;
            this.f14915b = map;
        }

        @Override // com.google.gson.u
        public Object read(xk.a aVar) {
            if (aVar.L() == xk.b.NULL) {
                aVar.G();
                return null;
            }
            Object a10 = this.f14914a.a();
            try {
                aVar.b();
                while (aVar.p()) {
                    c cVar = (c) this.f14915b.get(aVar.A());
                    if (cVar != null && cVar.f14918c) {
                        cVar.a(aVar, a10);
                    }
                    aVar.g0();
                }
                aVar.g();
                return a10;
            } catch (IllegalAccessException e10) {
                throw new AssertionError(e10);
            } catch (IllegalStateException e11) {
                throw new q(e11);
            }
        }

        @Override // com.google.gson.u
        public void write(xk.c cVar, Object obj) {
            if (obj == null) {
                cVar.t();
                return;
            }
            cVar.d();
            try {
                for (c cVar2 : this.f14915b.values()) {
                    if (cVar2.c(obj)) {
                        cVar.r(cVar2.f14916a);
                        cVar2.b(cVar, obj);
                    }
                }
                cVar.g();
            } catch (IllegalAccessException e10) {
                throw new AssertionError(e10);
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes3.dex */
    public static abstract class c {

        /* renamed from: a, reason: collision with root package name */
        final String f14916a;

        /* renamed from: b, reason: collision with root package name */
        final boolean f14917b;

        /* renamed from: c, reason: collision with root package name */
        final boolean f14918c;

        protected c(String str, boolean z10, boolean z11) {
            this.f14916a = str;
            this.f14917b = z10;
            this.f14918c = z11;
        }

        abstract void a(xk.a aVar, Object obj);

        abstract void b(xk.c cVar, Object obj);

        abstract boolean c(Object obj);
    }

    public ReflectiveTypeAdapterFactory(uk.c cVar, com.google.gson.c cVar2, Excluder excluder, JsonAdapterAnnotationTypeAdapterFactory jsonAdapterAnnotationTypeAdapterFactory) {
        this.f14903p = cVar;
        this.f14904q = cVar2;
        this.f14905r = excluder;
        this.f14906s = jsonAdapterAnnotationTypeAdapterFactory;
    }

    private c a(com.google.gson.d dVar, Field field, String str, com.google.gson.reflect.a aVar, boolean z10, boolean z11) {
        boolean a10 = j.a(aVar.getRawType());
        tk.b bVar = (tk.b) field.getAnnotation(tk.b.class);
        u a11 = bVar != null ? this.f14906s.a(this.f14903p, dVar, aVar, bVar) : null;
        boolean z12 = a11 != null;
        if (a11 == null) {
            a11 = dVar.m(aVar);
        }
        return new a(str, z10, z11, field, z12, a11, dVar, aVar, a10);
    }

    static boolean c(Field field, boolean z10, Excluder excluder) {
        return (excluder.b(field.getType(), z10) || excluder.f(field, z10)) ? false : true;
    }

    private Map d(com.google.gson.d dVar, com.google.gson.reflect.a aVar, Class cls) {
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        if (cls.isInterface()) {
            return linkedHashMap;
        }
        Type type = aVar.getType();
        com.google.gson.reflect.a aVar2 = aVar;
        Class cls2 = cls;
        while (cls2 != Object.class) {
            Field[] declaredFields = cls2.getDeclaredFields();
            int length = declaredFields.length;
            boolean z10 = false;
            int i10 = 0;
            while (i10 < length) {
                Field field = declaredFields[i10];
                boolean b10 = b(field, true);
                boolean b11 = b(field, z10);
                if (b10 || b11) {
                    wk.a.b(field);
                    Type p10 = uk.b.p(aVar2.getType(), cls2, field.getGenericType());
                    List e10 = e(field);
                    int size = e10.size();
                    c cVar = null;
                    int i11 = z10;
                    while (i11 < size) {
                        String str = (String) e10.get(i11);
                        boolean z11 = i11 != 0 ? z10 : b10;
                        int i12 = i11;
                        c cVar2 = cVar;
                        int i13 = size;
                        List list = e10;
                        Field field2 = field;
                        cVar = cVar2 == null ? (c) linkedHashMap.put(str, a(dVar, field, str, com.google.gson.reflect.a.get(p10), z11, b11)) : cVar2;
                        i11 = i12 + 1;
                        b10 = z11;
                        e10 = list;
                        size = i13;
                        field = field2;
                        z10 = false;
                    }
                    c cVar3 = cVar;
                    if (cVar3 != null) {
                        throw new IllegalArgumentException(type + " declares multiple JSON fields named " + cVar3.f14916a);
                    }
                }
                i10++;
                z10 = false;
            }
            aVar2 = com.google.gson.reflect.a.get(uk.b.p(aVar2.getType(), cls2, cls2.getGenericSuperclass()));
            cls2 = aVar2.getRawType();
        }
        return linkedHashMap;
    }

    private List e(Field field) {
        tk.c cVar = (tk.c) field.getAnnotation(tk.c.class);
        if (cVar == null) {
            return Collections.singletonList(this.f14904q.translateName(field));
        }
        String value = cVar.value();
        String[] alternate = cVar.alternate();
        if (alternate.length == 0) {
            return Collections.singletonList(value);
        }
        ArrayList arrayList = new ArrayList(alternate.length + 1);
        arrayList.add(value);
        for (String str : alternate) {
            arrayList.add(str);
        }
        return arrayList;
    }

    public boolean b(Field field, boolean z10) {
        return c(field, z10, this.f14905r);
    }

    @Override // com.google.gson.v
    public u create(com.google.gson.d dVar, com.google.gson.reflect.a aVar) {
        Class<Object> rawType = aVar.getRawType();
        if (Object.class.isAssignableFrom(rawType)) {
            return new b(this.f14903p.a(aVar), d(dVar, aVar, rawType));
        }
        return null;
    }
}
