diff --git a/engine/src/flutter/shell/platform/android/io/flutter/plugin/common/MethodCall.java b/engine/src/flutter/shell/platform/android/io/flutter/plugin/common/MethodCall.java index 6f3418e823..8fafdc8f7d 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/plugin/common/MethodCall.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/common/MethodCall.java @@ -4,6 +4,9 @@ package io.flutter.plugin.common; +import java.util.Map; +import org.json.JSONObject; + /** * Command object representing a method call on a {@link FlutterMethodChannel}. */ @@ -15,6 +18,10 @@ public final class MethodCall { /** * Arguments for the call. + * + * Consider using {@link #arguments()} for cases where a particular run-time type is expected. + * Consider using {@link #argument(String)} when that run-time type is a {@link Map} + * or a {@link JSONObject}. */ public final Object arguments; @@ -29,4 +36,40 @@ public final class MethodCall { this.method = method; this.arguments = arguments; } + + /** + * Returns the arguments of this method call with a static type determined by the call-site. + * + * @param the intended type of the arguments. + * @return the arguments with static type T + */ + @SuppressWarnings("unchecked") + public T arguments() { + return (T) arguments; + } + + /** + * Returns a String-keyed argument of this method call, assuming {@link #arguments} is a + * {@link Map} or a {@link JSONObject}. The static type of the returned result is determined + * by the call-site. + * + * @param the intended type of the argument. + * @param key the String key. + * @return the argument value at the specified key, with static type T, or {@code null}, if + * such an entry is not present. + * @throws ClassCastException if {@link #arguments} can be cast to neither {@link Map} nor + * {@link JSONObject}. + */ + @SuppressWarnings("unchecked") + public T argument(String key) { + if (arguments == null) { + return null; + } else if (arguments instanceof Map) { + return (T) ((Map) arguments).get(key); + } else if (arguments instanceof JSONObject) { + return (T) ((JSONObject) arguments).opt(key); + } else { + throw new ClassCastException(); + } + } }