package com.seewo.ota.j.a;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/* JADX INFO: compiled from: ApiFactory.java */
/* JADX INFO: loaded from: classes.dex */
public abstract class a<TInf extends Annotation, TMethod extends Annotation, TParams extends Annotation, TMethodInfo, TParamsInfo> {
    private final Class<TMethod> mApiAnnotationClass;
    private final Class<TParams> mArgsAnnotationClass;
    private final Class<TInf> mInterfaceAnnotationClass;

    public a() {
        Type[] actualTypeArguments = ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments();
        this.mInterfaceAnnotationClass = (Class) actualTypeArguments[0];
        this.mApiAnnotationClass = (Class) actualTypeArguments[1];
        this.mArgsAnnotationClass = (Class) actualTypeArguments[2];
    }

    private List<TMethodInfo> createApiOnce(Object obj) {
        ArrayList arrayList = new ArrayList();
        boolean z = false;
        for (Class<?> cls : obj.getClass().getInterfaces()) {
            if (cls.getAnnotation(this.mInterfaceAnnotationClass) != null) {
                for (Method method : cls.getDeclaredMethods()) {
                    Annotation annotation = method.getAnnotation(this.mApiAnnotationClass);
                    if (annotation != null) {
                        arrayList.add(createMethodInfo(obj, method, annotation, parseMethodParams(method)));
                    }
                }
                z = true;
            }
        }
        if (z) {
            return arrayList;
        }
        throw new IllegalArgumentException("annotation interface not found from object's interface:" + obj.getClass() + " annotation:" + this.mInterfaceAnnotationClass.getName());
    }

    public List<TMethodInfo> createApi(Object... objArr) {
        ArrayList arrayList = new ArrayList();
        for (Object obj : objArr) {
            arrayList.addAll(createApiOnce(obj));
        }
        return arrayList;
    }

    protected abstract TMethodInfo createMethodInfo(Object obj, Method method, TMethod tmethod, List<TParamsInfo> list);

    protected abstract TParamsInfo createParamsInfo(Class<?> cls, TParams tparams);

    protected List<TParamsInfo> parseMethodParams(Method method) {
        ArrayList arrayList = new ArrayList();
        Class<?>[] parameterTypes = method.getParameterTypes();
        Annotation[][] parameterAnnotations = method.getParameterAnnotations();
        for (int i2 = 0; i2 < parameterTypes.length; i2++) {
            Class<?> cls = parameterTypes[i2];
            Annotation[] annotationArr = parameterAnnotations[i2];
            Annotation annotation = null;
            int length = annotationArr.length;
            int i3 = 0;
            while (true) {
                if (i3 >= length) {
                    break;
                }
                Annotation annotation2 = annotationArr[i3];
                if (annotation2.annotationType() == this.mArgsAnnotationClass) {
                    annotation = annotation2;
                    break;
                }
                i3++;
            }
            if (annotation == null) {
                throw new IllegalArgumentException("parameters does not contain annotation:" + this.mArgsAnnotationClass);
            }
            arrayList.add(createParamsInfo(cls, annotation));
        }
        return arrayList;
    }

    public String toString() {
        return "ApiFactory:" + getClass().getName() + "mInterfaceAnnotationClass=" + this.mInterfaceAnnotationClass + " mApiAnnotationClass=" + this.mApiAnnotationClass + " mArgsAnnotationClass=" + this.mArgsAnnotationClass;
    }
}
