Let JS modules use strings instead of ids on Android

Reviewed By: nicklockwood

Differential Revision: D3229626

fb-gh-sync-id: f8b2e8c9fc0d25d67f623cdbbe9930a02a40d1de
fbshipit-source-id: f8b2e8c9fc0d25d67f623cdbbe9930a02a40d1de
This commit is contained in:
Alexander Blom
2016-05-04 10:29:26 -07:00
committed by Facebook Github Bot 4
parent 5071907b27
commit 041185edfd
11 changed files with 45 additions and 82 deletions
@@ -13,12 +13,13 @@ import javax.annotation.concurrent.Immutable;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.facebook.react.common.MapBuilder;
import com.facebook.infer.annotation.Assertions;
import com.facebook.react.common.build.ReactBuildConfig;
/**
* Registration info for a {@link JavaScriptModule}. Maps its methods to method ids.
@@ -26,54 +27,32 @@ import com.facebook.infer.annotation.Assertions;
@Immutable
public class JavaScriptModuleRegistration {
private final int mModuleId;
private final Class<? extends JavaScriptModule> mModuleInterface;
private final Map<Method, Integer> mMethodsToIds;
private final Map<Method, String> mMethodsToTracingNames;
public JavaScriptModuleRegistration(int moduleId, Class<? extends JavaScriptModule> moduleInterface) {
mModuleId = moduleId;
public JavaScriptModuleRegistration(Class<? extends JavaScriptModule> moduleInterface) {
mModuleInterface = moduleInterface;
mMethodsToTracingNames = new HashMap<>();
mMethodsToIds = MapBuilder.newHashMap();
mMethodsToTracingNames = MapBuilder.newHashMap();
final Method[] declaredMethods = mModuleInterface.getDeclaredMethods();
Arrays.sort(declaredMethods, new Comparator<Method>() {
@Override
public int compare(Method lhs, Method rhs) {
return lhs.getName().compareTo(rhs.getName());
if (ReactBuildConfig.DEBUG) {
Set<String> methodNames = new LinkedHashSet<>();
for (Method method : mModuleInterface.getDeclaredMethods()) {
if (!methodNames.add(method.getName())) {
throw new AssertionError(
"Method overloading is unsupported: " + mModuleInterface.getName() + "#" +
method.getName());
}
}
});
// Methods are sorted by name so we can dupe check and have obvious ordering
String previousName = null;
for (int i = 0; i < declaredMethods.length; i++) {
Method method = declaredMethods[i];
String name = method.getName();
Assertions.assertCondition(
!name.equals(previousName),
"Method overloading is unsupported: " + mModuleInterface.getName() + "#" + name);
previousName = name;
mMethodsToIds.put(method, i);
mMethodsToTracingNames.put(method, "JSCall__" + getName() + "_" + method.getName());
}
}
public int getModuleId() {
return mModuleId;
}
public int getMethodId(Method method) {
final Integer id = mMethodsToIds.get(method);
if (id == null) {
Assertions.assertUnreachable("Unknown method: " + method.getName());
}
return id.intValue();
}
public String getTracingName(Method method) {
return Assertions.assertNotNull(mMethodsToTracingNames.get(method));
String name = mMethodsToTracingNames.get(method);
if (name == null) {
name = "JSCall__" + getName() + "_" + method.getName();
mMethodsToTracingNames.put(method, name);
}
return name;
}
public Class<? extends JavaScriptModule> getModuleInterface() {
@@ -92,7 +71,7 @@ public class JavaScriptModuleRegistration {
return name;
}
public Set<Method> getMethods() {
return mMethodsToIds.keySet();
public List<Method> getMethods() {
return Arrays.asList(mModuleInterface.getDeclaredMethods());
}
}