Cleanup 2/n

Summary:
Some more minor cleanups that don't (well, barely) affect
semantics.

- Fix singleton management in multi-threaded environments.
- Remove double assignment.
- Make all things final where they can be.
- Use ugly Hungarian notation.
- Weaken some types.

Reviewed By: mweststrate

Differential Revision: D19471227

fbshipit-source-id: 18833c3fe940f51b8d4cb3948a63387b8cd9316c
This commit is contained in:
Pascal Hartig
2020-01-24 06:58:38 -08:00
committed by Facebook Github Bot
parent cbb8d97d86
commit 4e77b37831

View File

@@ -11,6 +11,7 @@ import com.facebook.flipper.android.AndroidFlipperClient;
import com.facebook.flipper.core.FlipperArray; import com.facebook.flipper.core.FlipperArray;
import com.facebook.flipper.core.FlipperClient; import com.facebook.flipper.core.FlipperClient;
import com.facebook.flipper.core.FlipperObject; import com.facebook.flipper.core.FlipperObject;
import com.facebook.flipper.core.FlipperPlugin;
import com.facebook.flipper.core.FlipperResponder; import com.facebook.flipper.core.FlipperResponder;
import com.facebook.react.bridge.Callback; import com.facebook.react.bridge.Callback;
import java.util.Map; import java.util.Map;
@@ -29,26 +30,25 @@ import org.json.JSONTokener;
* <p>Note that this manager is not bound to a specific FlipperModule instance, as that might be * <p>Note that this manager is not bound to a specific FlipperModule instance, as that might be
* swapped in and out over time. * swapped in and out over time.
*/ */
public class FlipperReactNativeJavaScriptPluginManager { final class FlipperReactNativeJavaScriptPluginManager {
static FlipperReactNativeJavaScriptPluginManager instance; private static FlipperReactNativeJavaScriptPluginManager sInstance;
static FlipperReactNativeJavaScriptPluginManager getInstance() { static synchronized FlipperReactNativeJavaScriptPluginManager getInstance() {
if (instance == null) { if (sInstance == null) {
instance = new FlipperReactNativeJavaScriptPluginManager(); sInstance = new FlipperReactNativeJavaScriptPluginManager();
} }
return instance; return sInstance;
} }
private final FlipperClient flipperClient; private final FlipperClient mFlipperClient;
// uniqueResponderId -> ResponderObject // uniqueResponderId -> ResponderObject
private final Map<String, FlipperResponder> responders = new ConcurrentHashMap<>(); private final Map<String, FlipperResponder> mResponders = new ConcurrentHashMap<>();
// generated the next responder id // generated the next responder id
private final AtomicLong responderId = new AtomicLong(); private final AtomicLong mResponderId = new AtomicLong();
private FlipperReactNativeJavaScriptPluginManager() { private FlipperReactNativeJavaScriptPluginManager() {
instance = this; mFlipperClient = AndroidFlipperClient.getInstanceIfInitialized();
this.flipperClient = AndroidFlipperClient.getInstanceIfInitialized();
} }
public void registerPlugin( public void registerPlugin(
@@ -61,7 +61,7 @@ public class FlipperReactNativeJavaScriptPluginManager {
statusCallback.invoke("noflipper"); statusCallback.invoke("noflipper");
return; return;
} }
FlipperReactNativeJavaScriptPlugin existing = getPlugin(pluginId); final FlipperReactNativeJavaScriptPlugin existing = getPlugin(pluginId);
if (existing != null) { if (existing != null) {
// Make sure events are emitted on the right application context // Make sure events are emitted on the right application context
existing.setModule(module); existing.setModule(module);
@@ -74,23 +74,23 @@ public class FlipperReactNativeJavaScriptPluginManager {
} }
// we always create a new plugin class on the fly, // we always create a new plugin class on the fly,
// as Flipper only allows one plugin per type to be registered! // as Flipper only allows one plugin per type to be registered!
FlipperReactNativeJavaScriptPlugin plugin = final FlipperPlugin plugin =
new FlipperReactNativeJavaScriptPlugin(module, pluginId, inBackground) { new FlipperReactNativeJavaScriptPlugin(module, pluginId, inBackground) {
// inner class with no new members // inner class with no new members
}; };
this.flipperClient.addPlugin(plugin); mFlipperClient.addPlugin(plugin);
statusCallback.invoke("ok"); statusCallback.invoke("ok");
} }
public void send(String pluginId, String method, String data) { void send(String pluginId, String method, String data) {
FlipperReactNativeJavaScriptPlugin plugin = getPlugin(pluginId); final FlipperReactNativeJavaScriptPlugin plugin = getPlugin(pluginId);
if (data == null) { if (data == null) {
plugin.getConnection().send(method, (FlipperObject) null); plugin.getConnection().send(method, (FlipperObject) null);
return; return;
} }
// Optimization: throwing raw strings around to the desktop would probably avoid some double // Optimization: throwing raw strings around to the desktop would probably avoid some double
// parsing... // parsing...
Object parsedData = parseJSON(data); final Object parsedData = parseJSON(data);
if (parsedData instanceof FlipperArray) { if (parsedData instanceof FlipperArray) {
plugin.getConnection().send(method, (FlipperArray) parsedData); plugin.getConnection().send(method, (FlipperArray) parsedData);
} else { } else {
@@ -98,28 +98,27 @@ public class FlipperReactNativeJavaScriptPluginManager {
} }
} }
public void reportErrorWithMetadata(String pluginId, String reason, String stackTrace) { void reportErrorWithMetadata(String pluginId, String reason, String stackTrace) {
getPlugin(pluginId).getConnection().reportErrorWithMetadata(reason, stackTrace); getPlugin(pluginId).getConnection().reportErrorWithMetadata(reason, stackTrace);
} }
public void reportError(String pluginId, String error) { void reportError(String pluginId, String error) {
getPlugin(pluginId).getConnection().reportError(new Error(error)); getPlugin(pluginId).getConnection().reportError(new Error(error));
} }
public void subscribe(FlipperModule module, String pluginId, String method) { void subscribe(FlipperModule module, String pluginId, String method) {
String key = pluginId + "#" + method; final FlipperReactNativeJavaScriptReceiver receiver =
FlipperReactNativeJavaScriptReceiver receiver =
new FlipperReactNativeJavaScriptReceiver(this, module, pluginId, method); new FlipperReactNativeJavaScriptReceiver(this, module, pluginId, method);
// Fresh connection should be the case for a new subscribe... // Fresh connection should be the case for a new subscribe...
getPlugin(pluginId).getConnection().receive(method, receiver); getPlugin(pluginId).getConnection().receive(method, receiver);
} }
public void respondSuccess(String responderId, String data) { void respondSuccess(String responderId, String data) {
FlipperResponder responder = responders.remove(responderId); final FlipperResponder responder = mResponders.remove(responderId);
if (data == null) { if (data == null) {
responder.success(); responder.success();
} else { } else {
Object parsedData = parseJSON(data); final Object parsedData = parseJSON(data);
if (parsedData instanceof FlipperArray) { if (parsedData instanceof FlipperArray) {
responder.success((FlipperArray) parsedData); responder.success((FlipperArray) parsedData);
} else { } else {
@@ -128,9 +127,9 @@ public class FlipperReactNativeJavaScriptPluginManager {
} }
} }
public void respondError(String responderId, String data) { void respondError(String responderId, String data) {
FlipperResponder responder = responders.remove(responderId); final FlipperResponder responder = mResponders.remove(responderId);
Object parsedData = parseJSON(data); final Object parsedData = parseJSON(data);
if (parsedData instanceof FlipperArray) { if (parsedData instanceof FlipperArray) {
responder.success((FlipperArray) parsedData); responder.success((FlipperArray) parsedData);
} else { } else {
@@ -138,13 +137,13 @@ public class FlipperReactNativeJavaScriptPluginManager {
} }
} }
FlipperReactNativeJavaScriptPlugin getPlugin(String pluginId) { private FlipperReactNativeJavaScriptPlugin getPlugin(String pluginId) {
return this.flipperClient.<FlipperReactNativeJavaScriptPlugin>getPlugin(pluginId); return mFlipperClient.getPlugin(pluginId);
} }
public String createResponderId(FlipperResponder responder) { String createResponderId(FlipperResponder responder) {
String id = String.valueOf(responderId.incrementAndGet()); final String id = String.valueOf(mResponderId.incrementAndGet());
responders.put(id, responder); mResponders.put(id, responder);
return id; return id;
} }
@@ -154,15 +153,15 @@ public class FlipperReactNativeJavaScriptPluginManager {
} }
// returns either a FlipperObject or Flipper array, pending the data // returns either a FlipperObject or Flipper array, pending the data
try { try {
JSONTokener tokener = new JSONTokener(json); final JSONTokener tokener = new JSONTokener(json);
char firstChar = tokener.nextClean(); final char firstChar = tokener.nextClean();
tokener.back(); tokener.back();
if (firstChar == '[') { if (firstChar == '[') {
return new FlipperArray(new JSONArray(tokener)); return new FlipperArray(new JSONArray(tokener));
} else { } else {
return new FlipperObject(new JSONObject(tokener)); return new FlipperObject(new JSONObject(tokener));
} }
} catch (JSONException e) { } catch (final JSONException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }