package org.greenrobot.eventbus;

import butterknife.internal.ButterKnifeProcessor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/* JADX INFO: compiled from: SubscriberMethodFinder.java */
/* JADX INFO: loaded from: classes.dex */
class m {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private static final Map<Class<?>, List<l>> f3380a = new ConcurrentHashMap();

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private static final a[] f3381b = new a[4];

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private List<org.greenrobot.eventbus.o.b> f3382c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private final boolean f3383d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private final boolean f3384e;

    /* JADX INFO: compiled from: SubscriberMethodFinder.java */
    static class a {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        final List<l> f3385a = new ArrayList();

        /* JADX INFO: renamed from: b, reason: collision with root package name */
        final Map<Class, Object> f3386b = new HashMap();

        /* JADX INFO: renamed from: c, reason: collision with root package name */
        final Map<String, Class> f3387c = new HashMap();

        /* JADX INFO: renamed from: d, reason: collision with root package name */
        final StringBuilder f3388d = new StringBuilder(128);

        /* JADX INFO: renamed from: e, reason: collision with root package name */
        Class<?> f3389e;

        /* JADX INFO: renamed from: f, reason: collision with root package name */
        Class<?> f3390f;

        /* JADX INFO: renamed from: g, reason: collision with root package name */
        boolean f3391g;
        org.greenrobot.eventbus.o.a h;

        a() {
        }

        private boolean b(Method method, Class<?> cls) {
            this.f3388d.setLength(0);
            this.f3388d.append(method.getName());
            StringBuilder sb = this.f3388d;
            sb.append('>');
            sb.append(cls.getName());
            String string = this.f3388d.toString();
            Class<?> declaringClass = method.getDeclaringClass();
            Class clsPut = this.f3387c.put(string, declaringClass);
            if (clsPut == null || clsPut.isAssignableFrom(declaringClass)) {
                return true;
            }
            this.f3387c.put(string, clsPut);
            return false;
        }

        boolean a(Method method, Class<?> cls) {
            Object objPut = this.f3386b.put(cls, method);
            if (objPut == null) {
                return true;
            }
            if (objPut instanceof Method) {
                if (!b((Method) objPut, cls)) {
                    throw new IllegalStateException();
                }
                this.f3386b.put(cls, this);
            }
            return b(method, cls);
        }

        void c(Class<?> cls) {
            this.f3390f = cls;
            this.f3389e = cls;
            this.f3391g = false;
            this.h = null;
        }

        void d() {
            if (this.f3391g) {
                this.f3390f = null;
                return;
            }
            Class<? super Object> superclass = this.f3390f.getSuperclass();
            this.f3390f = superclass;
            String name = superclass.getName();
            if (name.startsWith(ButterKnifeProcessor.JAVA_PREFIX) || name.startsWith("javax.") || name.startsWith(ButterKnifeProcessor.ANDROID_PREFIX)) {
                this.f3390f = null;
            }
        }

        void e() {
            this.f3385a.clear();
            this.f3386b.clear();
            this.f3387c.clear();
            this.f3388d.setLength(0);
            this.f3389e = null;
            this.f3390f = null;
            this.f3391g = false;
            this.h = null;
        }
    }

    m(List<org.greenrobot.eventbus.o.b> list, boolean z, boolean z2) {
        this.f3382c = list;
        this.f3383d = z;
        this.f3384e = z2;
    }

    private List<l> b(Class<?> cls) {
        a aVarG = g();
        aVarG.c(cls);
        while (aVarG.f3390f != null) {
            org.greenrobot.eventbus.o.a aVarF = f(aVarG);
            aVarG.h = aVarF;
            if (aVarF != null) {
                for (l lVar : aVarF.a()) {
                    if (aVarG.a(lVar.f3374a, lVar.f3376c)) {
                        aVarG.f3385a.add(lVar);
                    }
                }
            } else {
                d(aVarG);
            }
            aVarG.d();
        }
        return e(aVarG);
    }

    private List<l> c(Class<?> cls) {
        a aVarG = g();
        aVarG.c(cls);
        while (aVarG.f3390f != null) {
            d(aVarG);
            aVarG.d();
        }
        return e(aVarG);
    }

    private void d(a aVar) {
        Method[] methods;
        try {
            methods = aVar.f3390f.getDeclaredMethods();
        } catch (Throwable unused) {
            methods = aVar.f3390f.getMethods();
            aVar.f3391g = true;
        }
        for (Method method : methods) {
            int modifiers = method.getModifiers();
            if ((modifiers & 1) != 0 && (modifiers & 5192) == 0) {
                Class<?>[] parameterTypes = method.getParameterTypes();
                if (parameterTypes.length == 1) {
                    j jVar = (j) method.getAnnotation(j.class);
                    if (jVar != null) {
                        Class<?> cls = parameterTypes[0];
                        if (aVar.a(method, cls)) {
                            aVar.f3385a.add(new l(method, cls, jVar.threadMode(), jVar.priority(), jVar.sticky()));
                        }
                    }
                } else if (this.f3383d && method.isAnnotationPresent(j.class)) {
                    throw new e("@Subscribe method " + (method.getDeclaringClass().getName() + "." + method.getName()) + "must have exactly 1 parameter but has " + parameterTypes.length);
                }
            } else if (this.f3383d && method.isAnnotationPresent(j.class)) {
                throw new e((method.getDeclaringClass().getName() + "." + method.getName()) + " is a illegal @Subscribe method: must be public, non-static, and non-abstract");
            }
        }
    }

    private List<l> e(a aVar) {
        ArrayList arrayList = new ArrayList(aVar.f3385a);
        aVar.e();
        synchronized (f3381b) {
            int i = 0;
            while (true) {
                if (i >= 4) {
                    break;
                }
                a[] aVarArr = f3381b;
                if (aVarArr[i] == null) {
                    aVarArr[i] = aVar;
                    break;
                }
                i++;
            }
        }
        return arrayList;
    }

    private org.greenrobot.eventbus.o.a f(a aVar) {
        org.greenrobot.eventbus.o.a aVar2 = aVar.h;
        if (aVar2 != null && aVar2.b() != null) {
            org.greenrobot.eventbus.o.a aVarB = aVar.h.b();
            if (aVar.f3390f == aVarB.c()) {
                return aVarB;
            }
        }
        List<org.greenrobot.eventbus.o.b> list = this.f3382c;
        if (list == null) {
            return null;
        }
        Iterator<org.greenrobot.eventbus.o.b> it = list.iterator();
        while (it.hasNext()) {
            org.greenrobot.eventbus.o.a aVarA = it.next().a(aVar.f3390f);
            if (aVarA != null) {
                return aVarA;
            }
        }
        return null;
    }

    private a g() {
        synchronized (f3381b) {
            for (int i = 0; i < 4; i++) {
                a[] aVarArr = f3381b;
                a aVar = aVarArr[i];
                if (aVar != null) {
                    aVarArr[i] = null;
                    return aVar;
                }
            }
            return new a();
        }
    }

    List<l> a(Class<?> cls) {
        Map<Class<?>, List<l>> map = f3380a;
        List<l> list = map.get(cls);
        if (list != null) {
            return list;
        }
        List<l> listC = this.f3384e ? c(cls) : b(cls);
        if (!listC.isEmpty()) {
            map.put(cls, listC);
            return listC;
        }
        throw new e("Subscriber " + cls + " and its super classes have no public methods with the @Subscribe annotation");
    }
}
