React-native Android Java Module如何暴露自己的方法给js

   最近在看React-native,对内部实现很感兴趣,自己写了Module注册进去,并且在js中调用,很好奇自己的方法是怎么暴露给js的,看了BaseJavaModule的源码,里面有方法是如何暴露的。

@Override
public final Map<String, NativeMethod> getMethods() {
  Map<String, NativeMethod> methods = new HashMap<String, NativeMethod>();
  Method[] targetMethods = getClass().getDeclaredMethods();
  for (int i = 0; i < targetMethods.length; i++) {
    Method targetMethod = targetMethods[i];
    //找有@ReactMethod注解的方法
    if (targetMethod.getAnnotation(ReactMethod.class) != null) {
      String methodName = targetMethod.getName();
      if (methods.containsKey(methodName)) {
        // We do not support method overloading since js sees a function as an object regardless
        // of number of params.
        throw new IllegalArgumentException(
            "Java Module " + getName() + " method name already registered: " + methodName);
        }
      methods.put(methodName, new JavaMethod(targetMethod));
    }
  }
  return methods;
}


你可能感兴趣的:(React-native Android Java Module如何暴露自己的方法给js)