• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package io.flutter.plugin.common;
6 
7 import android.support.annotation.Nullable;
8 import java.util.Map;
9 import org.json.JSONObject;
10 
11 import io.flutter.BuildConfig;
12 
13 /**
14  * Command object representing a method call on a {@link MethodChannel}.
15  */
16 public final class MethodCall {
17     /**
18      * The name of the called method.
19      */
20     public final String method;
21 
22     /**
23      * Arguments for the call.
24      *
25      * <p>Consider using {@link #arguments()} for cases where a particular run-time type is expected.
26      * Consider using {@link #argument(String)} when that run-time type is {@link Map} or
27      * {@link JSONObject}.</p>
28      */
29     public final Object arguments;
30 
31     /**
32      * Creates a {@link MethodCall} with the specified method name and arguments.
33      *
34      * @param method the method name String, not null.
35      * @param arguments the arguments, a value supported by the channel's message codec.
36      */
MethodCall(String method, Object arguments)37     public MethodCall(String method, Object arguments) {
38         if (BuildConfig.DEBUG && method == null) {
39             throw new AssertionError("Parameter method must not be null.");
40         }
41         this.method = method;
42         this.arguments = arguments;
43     }
44 
45     /**
46      * Returns the arguments of this method call with a static type determined by the call-site.
47      *
48      * @param <T> the intended type of the arguments.
49      * @return the arguments with static type T
50      */
51     @SuppressWarnings("unchecked")
arguments()52     public <T> T arguments() {
53         return (T) arguments;
54     }
55 
56     /**
57      * Returns a String-keyed argument of this method call, assuming {@link #arguments} is a
58      * {@link Map} or a {@link JSONObject}. The static type of the returned result is determined
59      * by the call-site.
60      *
61      * @param <T> the intended type of the argument.
62      * @param key the String key.
63      * @return the argument value at the specified key, with static type T, or {@code null}, if
64      * such an entry is not present.
65      * @throws ClassCastException if {@link #arguments} can be cast to neither {@link Map} nor
66      * {@link JSONObject}.
67      */
68     @SuppressWarnings("unchecked")
69     @Nullable
argument(String key)70     public <T> T argument(String key) {
71         if (arguments == null) {
72             return null;
73         } else if (arguments instanceof Map) {
74             return (T) ((Map<?, ?>) arguments).get(key);
75         } else if (arguments instanceof JSONObject) {
76             return (T) ((JSONObject) arguments).opt(key);
77         } else {
78             throw new ClassCastException();
79         }
80     }
81 
82     /**
83      * Returns whether this method call involves a mapping for the given argument key,
84      * assuming {@link #arguments} is a {@link Map} or a {@link JSONObject}. The value associated
85      * with the key, as returned by {@link #argument(String)}, is not considered, and may be
86      * {@code null}.
87      *
88      * @param key the String key.
89      * @return {@code true}, if {@link #arguments} is a {@link Map} containing key, or a
90      * {@link JSONObject} with a mapping for key.
91      * @throws ClassCastException if {@link #arguments} can be cast to neither {@link Map} nor
92      * {@link JSONObject}.
93      */
hasArgument(String key)94     public boolean hasArgument(String key) {
95         if (arguments == null) {
96             return false;
97         } else if (arguments instanceof Map) {
98             return ((Map<?, ?>) arguments).containsKey(key);
99         } else if (arguments instanceof JSONObject) {
100             return ((JSONObject) arguments).has(key);
101         } else {
102             throw new ClassCastException();
103         }
104     }
105 }
106