Extract @ReactProp annotated properties from shadow nodes.

Differential Revision: D2536419

fb-gh-sync-id: 643499d4fdcb481349dad1701391059d2362984e
This commit is contained in:
Krzysztof Magiera
2015-10-13 10:08:42 -07:00
committed by facebook-github-bot-2
parent 3d22547e31
commit 05015e8d36
6 changed files with 124 additions and 12 deletions

View File

@@ -36,6 +36,8 @@ import com.facebook.react.bridge.ReadableMap;
// thread sequentially
private static final Object[] VIEW_MGR_ARGS = new Object[2];
private static final Object[] VIEW_MGR_GROUP_ARGS = new Object[3];
private static final Object[] SHADOW_ARGS = new Object[1];
private static final Object[] SHADOW_GROUP_ARGS = new Object[2];
private PropSetter(ReactProp prop, String defaultType, Method setter) {
mPropName = prop.name();
@@ -83,6 +85,25 @@ import com.facebook.react.bridge.ReadableMap;
}
}
public void updateShadowNodeProp(
ReactShadowNode nodeToUpdate,
CatalystStylesDiffMap props) {
try {
if (mIndex == null) {
SHADOW_ARGS[0] = extractProperty(props);
mSetter.invoke(nodeToUpdate, SHADOW_ARGS);
} else {
SHADOW_GROUP_ARGS[0] = mIndex;
SHADOW_GROUP_ARGS[1] = extractProperty(props);
mSetter.invoke(nodeToUpdate, SHADOW_GROUP_ARGS);
}
} catch (Throwable t) {
FLog.e(ViewManager.class, "Error while updating prop " + mPropName, t);
throw new JSApplicationIllegalArgumentException("Error while updating property '" +
mPropName + "' in shadow node of type: " + nodeToUpdate.getViewClass(), t);
}
}
protected abstract @Nullable Object extractProperty(CatalystStylesDiffMap props);
}
@@ -227,7 +248,8 @@ import com.facebook.react.bridge.ReadableMap;
}
/*package*/ static Map<String, String> getNativePropsForView(
Class<? extends ViewManager> viewManagerTopClass) {
Class<? extends ViewManager> viewManagerTopClass,
Class<? extends ReactShadowNode> shadowNodeTopClass) {
Map<String, String> nativeProps = new HashMap<>();
Map<String, PropSetter> viewManagerProps =
@@ -236,12 +258,18 @@ import com.facebook.react.bridge.ReadableMap;
nativeProps.put(setter.getPropName(), setter.getPropType());
}
Map<String, PropSetter> shadowNodeProps =
getNativePropSettersForShadowNodeClass(shadowNodeTopClass);
for (PropSetter setter : shadowNodeProps.values()) {
nativeProps.put(setter.getPropName(), setter.getPropType());
}
return nativeProps;
}
/**
* Returns map from property name to setter instances for all the property setters annotated with
* {@link ReactProp} in the given {@link ViewManager} plus all the setter declared by it's
* {@link ReactProp} in the given {@link ViewManager} class plus all the setter declared by its
* parent classes.
*/
/*package*/ static Map<String, PropSetter> getNativePropSettersForViewManagerClass(
@@ -263,6 +291,30 @@ import com.facebook.react.bridge.ReadableMap;
return props;
}
/**
* Returns map from property name to setter instances for all the property setters annotated with
* {@link ReactProp} (or {@link ReactPropGroup} in the given {@link ReactShadowNode} subclass plus
* all the setters declared by its parent classes up to {@link ReactShadowNode} which is treated
* as a base class.
*/
/*package*/ static Map<String, PropSetter> getNativePropSettersForShadowNodeClass(
Class<? extends ReactShadowNode> cls) {
if (cls == ReactShadowNode.class) {
return EMPTY_PROPS_MAP;
}
Map<String, PropSetter> props = CLASS_PROPS_CACHE.get(cls);
if (props != null) {
return props;
}
// This is to include all the setters from parent classes up to ReactShadowNode class
props = new HashMap<>(
getNativePropSettersForShadowNodeClass(
(Class<? extends ReactShadowNode>) cls.getSuperclass()));
extractPropSettersFromShadowNodeClassDefinition(cls, props);
CLASS_PROPS_CACHE.put(cls, props);
return props;
}
private static PropSetter createPropSetter(
ReactProp annotation,
Method method,
@@ -360,4 +412,34 @@ import com.facebook.react.bridge.ReadableMap;
}
}
}
private static void extractPropSettersFromShadowNodeClassDefinition(
Class<? extends ReactShadowNode> cls,
Map<String, PropSetter> props) {
for (Method method : cls.getDeclaredMethods()) {
ReactProp annotation = method.getAnnotation(ReactProp.class);
if (annotation != null) {
Class<?>[] paramTypes = method.getParameterTypes();
if (paramTypes.length != 1) {
throw new RuntimeException("Wrong number of args for prop setter: " +
cls.getName() + "#" + method.getName());
}
props.put(annotation.name(), createPropSetter(annotation, method, paramTypes[0]));
}
ReactPropGroup groupAnnotation = method.getAnnotation(ReactPropGroup.class);
if (groupAnnotation != null) {
Class<?> [] paramTypes = method.getParameterTypes();
if (paramTypes.length != 2) {
throw new RuntimeException("Wrong number of args for group prop setter: " +
cls.getName() + "#" + method.getName());
}
if (paramTypes[0] != int.class) {
throw new RuntimeException("Second argument should be property index: " +
cls.getName() + "#" + method.getName());
}
createPropSetters(groupAnnotation, method, paramTypes[1], props);
}
}
}
}