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