package com.google.gson.b;

import com.bytedance.covode.number.Covode;
import com.google.gson.internal.b;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.HashMap;
import java.util.Map;

public class a<T> {
    public final int hashCode;
    public final Class<? super T> rawType;
    public final Type type;

    static {
        Covode.recordClassIndex(45375);
    }

    public final int hashCode() {
        return this.hashCode;
    }

    public final String toString() {
        return b.LIZLLL(this.type);
    }

    public a() {
        Type superclassTypeParameter = getSuperclassTypeParameter(getClass());
        this.type = superclassTypeParameter;
        this.rawType = b.LIZJ(superclassTypeParameter);
        this.hashCode = superclassTypeParameter.hashCode();
    }

    public static <T> a<T> get(Class<T> cls) {
        return new a<>(cls);
    }

    public boolean isAssignableFrom(a<?> aVar) {
        return isAssignableFrom(aVar.type);
    }

    public static a<?> get(Type type2) {
        return new a<>(type2);
    }

    public static a<?> getArray(Type type2) {
        return new a<>(b.LIZ(type2));
    }

    public boolean isAssignableFrom(Class<?> cls) {
        return isAssignableFrom((Type) cls);
    }

    public static Type getSuperclassTypeParameter(Class<?> cls) {
        Type genericSuperclass = cls.getGenericSuperclass();
        if (!(genericSuperclass instanceof Class)) {
            return b.LIZIZ(((ParameterizedType) genericSuperclass).getActualTypeArguments()[0]);
        }
        throw new RuntimeException("Missing type parameter.");
    }

    public final boolean equals(Object obj) {
        if (!(obj instanceof a) || !b.LIZ(this.type, ((a) obj).type)) {
            return false;
        }
        return true;
    }

    public a(Type type2) {
        com.google.gson.internal.a.LIZ(type2);
        Type LIZIZ = b.LIZIZ(type2);
        this.type = LIZIZ;
        this.rawType = b.LIZJ(LIZIZ);
        this.hashCode = LIZIZ.hashCode();
    }

    public boolean isAssignableFrom(Type type2) {
        if (type2 == null) {
            return false;
        }
        if (this.type.equals(type2)) {
            return true;
        }
        Type type3 = this.type;
        if (type3 instanceof Class) {
            return this.rawType.isAssignableFrom(b.LIZJ(type2));
        }
        if (type3 instanceof ParameterizedType) {
            return isAssignableFrom(type2, (ParameterizedType) type3, new HashMap());
        }
        if (!(type3 instanceof GenericArrayType)) {
            throw buildUnexpectedTypeError(type3, Class.class, ParameterizedType.class, GenericArrayType.class);
        } else if (!this.rawType.isAssignableFrom(b.LIZJ(type2)) || !isAssignableFrom(type2, (GenericArrayType) this.type)) {
            return false;
        } else {
            return true;
        }
    }

    public static a<?> getParameterized(Type type2, Type... typeArr) {
        return new a<>(b.LIZ((Type) null, type2, typeArr));
    }

    public static boolean isAssignableFrom(Type type2, GenericArrayType genericArrayType) {
        Class<?> cls;
        Type genericComponentType = genericArrayType.getGenericComponentType();
        if (!(genericComponentType instanceof ParameterizedType)) {
            return true;
        }
        if (type2 instanceof GenericArrayType) {
            cls = ((GenericArrayType) type2).getGenericComponentType();
        } else {
            boolean z = type2 instanceof Class;
            cls = type2;
            if (z) {
                Class<?> cls2 = (Class) type2;
                while (cls2.isArray()) {
                    cls2 = cls2.getComponentType();
                }
                cls = cls2;
            }
        }
        return isAssignableFrom(cls, (ParameterizedType) genericComponentType, new HashMap());
    }

    public static AssertionError buildUnexpectedTypeError(Type type2, Class<?>... clsArr) {
        StringBuilder sb = new StringBuilder("Unexpected type. Expected one of: ");
        for (Class<?> name : clsArr) {
            sb.append(name.getName());
            sb.append(", ");
        }
        sb.append("but got: ");
        sb.append(type2.getClass().getName());
        sb.append(", for type token: ");
        sb.append(type2.toString());
        sb.append('.');
        return new AssertionError(sb.toString());
    }

    public static boolean matches(Type type2, Type type3, Map<String, Type> map) {
        if (type3.equals(type2)) {
            return true;
        }
        if (!(type2 instanceof TypeVariable) || !type3.equals(map.get(((TypeVariable) type2).getName()))) {
            return false;
        }
        return true;
    }

    public static boolean typeEquals(ParameterizedType parameterizedType, ParameterizedType parameterizedType2, Map<String, Type> map) {
        if (!parameterizedType.getRawType().equals(parameterizedType2.getRawType())) {
            return false;
        }
        Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
        Type[] actualTypeArguments2 = parameterizedType2.getActualTypeArguments();
        for (int i = 0; i < actualTypeArguments.length; i++) {
            if (!matches(actualTypeArguments[i], actualTypeArguments2[i], map)) {
                return false;
            }
        }
        return true;
    }

    public static boolean isAssignableFrom(Type type2, ParameterizedType parameterizedType, Map<String, Type> map) {
        ParameterizedType parameterizedType2;
        while (true) {
            if (type2 == null) {
                return false;
            }
            if (parameterizedType.equals(type2)) {
                return true;
            }
            Class<?> LIZJ = b.LIZJ(type2);
            if ((type2 instanceof ParameterizedType) && (parameterizedType2 = (ParameterizedType) type2) != null) {
                Type[] actualTypeArguments = parameterizedType2.getActualTypeArguments();
                TypeVariable[] typeParameters = LIZJ.getTypeParameters();
                for (int i = 0; i < actualTypeArguments.length; i++) {
                    Type type3 = actualTypeArguments[i];
                    TypeVariable typeVariable = typeParameters[i];
                    while (type3 instanceof TypeVariable) {
                        type3 = map.get(((TypeVariable) type3).getName());
                    }
                    map.put(typeVariable.getName(), type3);
                }
                if (typeEquals(parameterizedType2, parameterizedType, map)) {
                    return true;
                }
            }
            for (Type isAssignableFrom : LIZJ.getGenericInterfaces()) {
                if (isAssignableFrom(isAssignableFrom, parameterizedType, new HashMap(map))) {
                    return true;
                }
            }
            type2 = LIZJ.getGenericSuperclass();
            map = new HashMap<>(map);
        }
    }
}
