android 事件总线 -- Otto(五) EventHandler、EventProducer、AnnotatedHandlerFinder

在EventHandler、EventProducer其实可以看作两个实体类,EventHandler用来保存每一个事件注册的类和注册方法,最后提供一个通过反射机制调用方法;EventProducer用来保存每一个事件生产者类和生产方法,最后提供一个通过反射机制调用方法。

AnnotatedHandlerFinder

这个类用来负责通过反射获取 通过 @Produce 注释的方法和通过 @Subscribe 注释的方法。

/**
   * Load all methods annotated with {@link Produce} or {@link Subscribe} into their respective caches for the
   * specified class.
   * 从事件类中加载用@Producer或@Subscribe注释的方法,并放入到相应的集合中
   */
  private static void loadAnnotatedMethods(Class<?> listenerClass,
      Map<Class<?>, Method> producerMethods, Map<Class<?>, Set<Method>> subscriberMethods) {
	  // 遍历类中所有声明的方法
	  for (Method method : listenerClass.getDeclaredMethods()) {
      // The compiler sometimes creates synthetic bridge methods as part of the
      // type erasure process. As of JDK8 these methods now include the same
      // annotations as the original declarations. They should be ignored for
      // subscribe/produce.
    	/*
    	 * 在JDK8中可能存在同样由JAVA本身提供的@subscribe @produce注释的方法
    	 * method.isBridge()方法判断该方法是否是由JAVA本身提供的
    	 * 如果是由JAVA本身提供的则忽略
    	 */
      if (method.isBridge()) {
        continue;
      }
      // 判断方法的注释类型
      if (method.isAnnotationPresent(Subscribe.class)) { // @Subscribe注释
    	  // 获取方法的所有的参数
        Class<?>[] parameterTypes = method.getParameterTypes();
        // 由@Subscribe注释的方法,只能有一个参数
        if (parameterTypes.length != 1) {
          throw new IllegalArgumentException("Method " + method + " has @Subscribe annotation but requires "
              + parameterTypes.length + " arguments.  Methods must require a single argument.");
        }
        // 由@Subscribe注释的方法参数不能为interface接口类型
        Class<?> eventType = parameterTypes[0];
        if (eventType.isInterface()) {
          throw new IllegalArgumentException("Method " + method + " has @Subscribe annotation on " + eventType
              + " which is an interface.  Subscription must be on a concrete class type.");
        }
        // 判断该方法是否是public方法
        if ((method.getModifiers() & Modifier.PUBLIC) == 0) {
          throw new IllegalArgumentException("Method " + method + " has @Subscribe annotation on " + eventType
              + " but is not 'public'.");
        }
        // 将方法放入到集合中
        Set<Method> methods = subscriberMethods.get(eventType);
        if (methods == null) {
          methods = new HashSet<Method>();
          subscriberMethods.put(eventType, methods);
        }
        methods.add(method);
      } else if (method.isAnnotationPresent(Produce.class)) {
        Class<?>[] parameterTypes = method.getParameterTypes();
        if (parameterTypes.length != 0) {
          throw new IllegalArgumentException("Method " + method + "has @Produce annotation but requires "
              + parameterTypes.length + " arguments.  Methods must require zero arguments.");
        }
        if (method.getReturnType() == Void.class) {
          throw new IllegalArgumentException("Method " + method
              + " has a return type of void.  Must declare a non-void type.");
        }

        Class<?> eventType = method.getReturnType();
        if (eventType.isInterface()) {
          throw new IllegalArgumentException("Method " + method + " has @Produce annotation on " + eventType
              + " which is an interface.  Producers must return a concrete class type.");
        }
        if (eventType.equals(Void.TYPE)) {
          throw new IllegalArgumentException("Method " + method + " has @Produce annotation but has no return type.");
        }

        if ((method.getModifiers() & Modifier.PUBLIC) == 0) {
          throw new IllegalArgumentException("Method " + method + " has @Produce annotation on " + eventType
              + " but is not 'public'.");
        }

        if (producerMethods.containsKey(eventType)) {
          throw new IllegalArgumentException("Producer for type " + eventType + " has already been registered.");
        }
        producerMethods.put(eventType, method);
      }
    }

    PRODUCERS_CACHE.put(listenerClass, producerMethods);
    SUBSCRIBERS_CACHE.put(listenerClass, subscriberMethods);
  }


你可能感兴趣的:(android,otto,事件总线)