EventBus-为什么订阅的方法必须是public?

在看了源码之后发现是这样的:

private void findUsingReflectionInSingleClass(FindState findState) {
        Method[] methods;
        try {
            // This is faster than getMethods, especially when subscribers are fat classes like Activities 
            //获取所有声明的方法
            methods = findState.clazz.getDeclaredMethods();
        } catch (Throwable th) {
            // Workaround for java.lang.NoClassDefFoundError, see https://github.com/greenrobot/EventBus/issues/149
            methods = findState.clazz.getMethods();
            findState.skipSuperClasses = true;
        }
        for (Method method : methods) {
            //获取方法的标识符
            int modifiers = method.getModifiers();
            //方法的标识符必须是public 
            if ((modifiers & Modifier.PUBLIC) != 0 && (modifiers & MODIFIERS_IGNORE) == 0) {
                Class[] parameterTypes = method.getParameterTypes();
                if (parameterTypes.length == 1) {
                    Subscribe subscribeAnnotation = method.getAnnotation(Subscribe.class);
                    if (subscribeAnnotation != null) {
                        Class eventType = parameterTypes[0];
                        if (findState.checkAdd(method, eventType)) {
                            ThreadMode threadMode = subscribeAnnotation.threadMode();
                            findState.subscriberMethods.add(new SubscriberMethod(method, eventType, threadMode,
                                    subscribeAnnotation.priority(), subscribeAnnotation.sticky()));
                        }
                    }
                } else if (strictMethodVerification && method.isAnnotationPresent(Subscribe.class)) {
                    String methodName = method.getDeclaringClass().getName() + "." + method.getName();
                    throw new EventBusException("@Subscribe method " + methodName +
                            "must have exactly 1 parameter but has " + parameterTypes.length);
                }
            } else if (strictMethodVerification && method.isAnnotationPresent(Subscribe.class)) {
              //否则抛出异常 ,说这个方法必须加上@Subscribe ,并且是public 的,不能是静态的,抽象的
                String methodName = method.getDeclaringClass().getName() + "." + method.getName();
                throw new EventBusException(methodName +
                        " is a illegal @Subscribe method: must be public, non-static, and non-abstract");
            }
        }
    }

然后就没有然后了,之前怀疑private 修饰的方法,反射后不能被其他类调用,后来查了相关资料,发现可以,后来去网上寻找无果,后来在我微信关注的公众号里,群主伯特说:虽然可以反射调用private 的方法,但是必须通过setAccess来修改访问权限。说是修改,实则破坏了java中的作用域,迫不得已不推荐这么做。EventBus 作为三方库,显然不会在你设置为private 时,好心办坏事,强制访问。

仅此而已,仅此而已。分享给还不知道的童鞋。

你可能感兴趣的:(EventBus-为什么订阅的方法必须是public?)