JavaObjWrapper.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. // <copyright file="JavaObjWrapper.cs" company="Google Inc.">
  2. // Copyright (C) 2014 Google Inc.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. // </copyright>
  16. #if UNITY_ANDROID
  17. namespace Google.Developers
  18. {
  19. using System;
  20. using System.Reflection;
  21. using System.Runtime.InteropServices;
  22. using UnityEngine;
  23. /// <summary>
  24. /// Java object wrapper. This class encapsulates the the java object
  25. /// pointer and provides the functionality to call methods and properties.
  26. /// </summary>
  27. public class JavaObjWrapper
  28. {
  29. /// <summary>
  30. /// The raw java object pointer.
  31. /// </summary>
  32. IntPtr raw;
  33. IntPtr cachedRawClass = IntPtr.Zero;
  34. /// <summary>
  35. /// Initializes a new instance of the <see cref="Google.Developers.JavaObjWrapper"/> class.
  36. /// Does not create an instance of the java class.
  37. /// </summary>
  38. protected JavaObjWrapper()
  39. {
  40. }
  41. /// <summary>
  42. /// Creates a new instance of the given class by calling the
  43. /// no-arg constructor.
  44. /// </summary>
  45. /// <param name="clazzName">Clazz name.</param>
  46. public JavaObjWrapper(string clazzName)
  47. {
  48. this.raw = AndroidJNI.AllocObject(AndroidJNI.FindClass(clazzName));
  49. }
  50. /// <summary>
  51. /// Wraps the given pointer.
  52. /// </summary>
  53. /// <param name="rawObject">Raw object.</param>
  54. public JavaObjWrapper(IntPtr rawObject)
  55. {
  56. raw = rawObject;
  57. }
  58. /// <summary>
  59. /// Gets the raw object.
  60. /// </summary>
  61. /// <value>The raw object.</value>
  62. public IntPtr RawObject
  63. {
  64. get
  65. {
  66. return raw;
  67. }
  68. }
  69. public virtual IntPtr RawClass
  70. {
  71. get
  72. {
  73. if (cachedRawClass == IntPtr.Zero && raw != IntPtr.Zero)
  74. {
  75. cachedRawClass = AndroidJNI.GetObjectClass(raw);
  76. }
  77. return cachedRawClass;
  78. }
  79. }
  80. /// <summary>
  81. /// Creates an instance of the java object. The arguments are for
  82. /// the constructor.
  83. /// </summary>
  84. /// <param name="clazzName">Clazz name.</param>
  85. /// <param name="args">Arguments.</param>
  86. public void CreateInstance(string clazzName, params object[] args)
  87. {
  88. // Can't create an instance if there is already a pointer.
  89. if (raw != IntPtr.Zero)
  90. {
  91. throw new Exception("Java object already set");
  92. }
  93. // TODO: use a specific signature. This could be problematic when
  94. // using arguments that are subclasses of the declared parameter types.
  95. IntPtr method = AndroidJNIHelper.GetConstructorID(RawClass, args);
  96. jvalue[] jArgs = ConstructArgArray(args);
  97. // assign the raw object.
  98. raw = AndroidJNI.NewObject(RawClass, method, jArgs);
  99. }
  100. /// <summary>
  101. /// Constructs the argument array for calling a JNI method.
  102. ///
  103. /// This wraps the JNIHelper method so JavaObjWrapper arguments can be
  104. /// found and use the raw pointer.
  105. /// </summary>
  106. /// <returns>The argument array.</returns>
  107. /// <param name="args">Arguments.</param>
  108. protected static jvalue[] ConstructArgArray(object[] theArgs)
  109. {
  110. object[] a = new object[theArgs.Length];
  111. for (int i = 0; i < theArgs.Length; i++)
  112. {
  113. if (theArgs[i] is JavaObjWrapper)
  114. {
  115. a[i] = ((JavaObjWrapper)theArgs[i]).raw;
  116. }
  117. else
  118. {
  119. a[i] = theArgs[i];
  120. }
  121. }
  122. jvalue[] args = AndroidJNIHelper.CreateJNIArgArray(a);
  123. for (int i = 0; i < theArgs.Length; i++)
  124. {
  125. if (theArgs[i] is JavaObjWrapper)
  126. {
  127. args[i].l = ((JavaObjWrapper)theArgs[i]).raw;
  128. }
  129. else if (theArgs[i] is JavaInterfaceProxy)
  130. {
  131. IntPtr v = AndroidJNIHelper.CreateJavaProxy(
  132. (AndroidJavaProxy)theArgs[i]);
  133. args[i].l = v;
  134. }
  135. }
  136. if (args.Length == 1)
  137. {
  138. for (int i = 0; i < args.Length; i++)
  139. {
  140. Debug.Log("---- [" + i + "] -- " + args[i].l);
  141. }
  142. }
  143. return args;
  144. }
  145. /// <summary>
  146. /// Calls a static method with an object return type.
  147. /// </summary>
  148. /// <returns>The invoke call.</returns>
  149. /// <param name="type">Type.</param>
  150. /// <param name="name">Name.</param>
  151. /// <param name="sig">Sig.</param>
  152. /// <param name="args">Arguments.</param>
  153. /// <typeparam name="T">The 1st type parameter.</typeparam>
  154. public static T StaticInvokeObjectCall<T>(
  155. string type, string name, string sig, params object[] args)
  156. {
  157. IntPtr rawClass = AndroidJNI.FindClass(type);
  158. IntPtr method = AndroidJNI.GetStaticMethodID(rawClass, name, sig);
  159. jvalue[] jArgs = ConstructArgArray(args);
  160. IntPtr val = AndroidJNI.CallStaticObjectMethod(rawClass, method, jArgs);
  161. ConstructorInfo c = typeof(T).GetConstructor(new Type[] { val.GetType() });
  162. if (c != null)
  163. {
  164. return (T)c.Invoke(new object[] { val });
  165. }
  166. if (typeof(T).IsArray)
  167. {
  168. // make an array
  169. //TODO: handle arrays of objects
  170. return AndroidJNIHelper.ConvertFromJNIArray<T>(val);
  171. }
  172. Debug.Log("Trying cast....");
  173. Type t = typeof(T);
  174. return (T)Marshal.PtrToStructure(val, t);
  175. }
  176. /// <summary>
  177. /// Invokes a static method with void return type.
  178. /// </summary>
  179. /// <param name="type">Type.</param>
  180. /// <param name="name">Name.</param>
  181. /// <param name="sig">Sig.</param>
  182. /// <param name="args">Arguments.</param>
  183. public static void StaticInvokeCallVoid(
  184. string type, string name, string sig, params object[] args)
  185. {
  186. IntPtr rawClass = AndroidJNI.FindClass(type);
  187. IntPtr method = AndroidJNI.GetStaticMethodID(rawClass, name, sig);
  188. jvalue[] jArgs = ConstructArgArray(args);
  189. AndroidJNI.CallStaticVoidMethod(rawClass, method, jArgs);
  190. }
  191. /// <summary>
  192. /// Gets the value of a static field returning an object.
  193. /// </summary>
  194. /// <returns>The static object field.</returns>
  195. /// <param name="clsName">Cls name.</param>
  196. /// <param name="name">Name.</param>
  197. /// <param name="sig">Sig.</param>
  198. /// <typeparam name="T">The 1st type parameter.</typeparam>
  199. public static T GetStaticObjectField<T>(string clsName, string name, string sig)
  200. {
  201. IntPtr rawClass = AndroidJNI.FindClass(clsName);
  202. IntPtr id = AndroidJNI.GetStaticFieldID(rawClass, name, sig);
  203. IntPtr val = AndroidJNI.GetStaticObjectField(rawClass, id);
  204. ConstructorInfo c = typeof(T).GetConstructor(new Type[] { val.GetType() });
  205. if (c != null)
  206. {
  207. return (T)c.Invoke(new object[] { val });
  208. }
  209. Type t = typeof(T);
  210. return (T)Marshal.PtrToStructure(val, t);
  211. }
  212. /// <summary>
  213. /// Gets the value of a static int field.
  214. /// </summary>
  215. /// <returns>The static int field.</returns>
  216. /// <param name="clsName">Cls name.</param>
  217. /// <param name="name">Name.</param>
  218. public static int GetStaticIntField(string clsName, string name)
  219. {
  220. IntPtr rawClass = AndroidJNI.FindClass(clsName);
  221. IntPtr method = AndroidJNI.GetStaticFieldID(rawClass, name, "I");
  222. return AndroidJNI.GetStaticIntField(rawClass, method);
  223. }
  224. /// <summary>
  225. /// Gets the value of a static string field.
  226. /// </summary>
  227. /// <returns>The static string field.</returns>
  228. /// <param name="clsName">Cls name.</param>
  229. /// <param name="name">Name.</param>
  230. public static string GetStaticStringField(string clsName, string name)
  231. {
  232. IntPtr rawClass = AndroidJNI.FindClass(clsName);
  233. IntPtr method = AndroidJNI.GetStaticFieldID(rawClass, name, "Ljava/lang/String;");
  234. return AndroidJNI.GetStaticStringField(rawClass, method);
  235. }
  236. /// <summary>
  237. /// Gets the value of a static float field.
  238. /// </summary>
  239. /// <returns>The static float field.</returns>
  240. /// <param name="clsName">Cls name.</param>
  241. /// <param name="name">Name.</param>
  242. public static float GetStaticFloatField(string clsName, string name)
  243. {
  244. IntPtr rawClass = AndroidJNI.FindClass(clsName);
  245. IntPtr method = AndroidJNI.GetStaticFieldID(rawClass, name, "F");
  246. return AndroidJNI.GetStaticFloatField(rawClass, method);
  247. }
  248. /// <summary>
  249. /// Calls a non-static method with a void return type.
  250. /// </summary>
  251. /// <param name="name">Name.</param>
  252. /// <param name="sig">Sig.</param>
  253. /// <param name="args">Arguments.</param>
  254. public void InvokeCallVoid(string name, string sig, params object[] args)
  255. {
  256. IntPtr method = AndroidJNI.GetMethodID(RawClass, name, sig);
  257. jvalue[] jArgs = ConstructArgArray(args);
  258. AndroidJNI.CallVoidMethod(raw, method, jArgs);
  259. }
  260. public T InvokeCall<T>(string name, string sig, params object[] args)
  261. {
  262. Type t = typeof(T);
  263. IntPtr method = AndroidJNI.GetMethodID(RawClass, name, sig);
  264. jvalue[] jArgs = ConstructArgArray(args);
  265. if (method == IntPtr.Zero)
  266. {
  267. Debug.LogError("Cannot get method for " + name);
  268. throw new Exception("Cannot get method for " + name);
  269. }
  270. if (t == typeof(bool))
  271. {
  272. return (T)(object)AndroidJNI.CallBooleanMethod(raw, method, jArgs);
  273. }
  274. else if (t == typeof(string))
  275. {
  276. return (T)(object)AndroidJNI.CallStringMethod(raw, method, jArgs);
  277. }
  278. else if (t == typeof(int))
  279. {
  280. return (T)(object)AndroidJNI.CallIntMethod(raw, method, jArgs);
  281. }
  282. else if (t == typeof(float))
  283. {
  284. return (T)(object)AndroidJNI.CallFloatMethod(raw, method, jArgs);
  285. }
  286. else if (t == typeof(double))
  287. {
  288. return (T)(object)AndroidJNI.CallDoubleMethod(raw, method, jArgs);
  289. }
  290. else if (t == typeof(byte))
  291. {
  292. return (T)(object)AndroidJNI.CallByteMethod(raw, method, jArgs);
  293. }
  294. else if (t == typeof(char))
  295. {
  296. return (T)(object)AndroidJNI.CallCharMethod(raw, method, jArgs);
  297. }
  298. else if (t == typeof(long))
  299. {
  300. return (T)(object)AndroidJNI.CallLongMethod(raw, method, jArgs);
  301. }
  302. else if (t == typeof(short))
  303. {
  304. return (T)(object)AndroidJNI.CallShortMethod(raw, method, jArgs);
  305. }
  306. else
  307. {
  308. return InvokeObjectCall<T>(name, sig, args);
  309. }
  310. }
  311. public static T StaticInvokeCall<T>(string type, string name, string sig, params object[] args)
  312. {
  313. Type t = typeof(T);
  314. IntPtr rawClass = AndroidJNI.FindClass(type);
  315. IntPtr method = AndroidJNI.GetStaticMethodID(rawClass, name, sig);
  316. jvalue[] jArgs = ConstructArgArray(args);
  317. if (t == typeof(bool))
  318. {
  319. return (T)(object)AndroidJNI.CallStaticBooleanMethod(
  320. rawClass, method, jArgs);
  321. }
  322. else if (t == typeof(string))
  323. {
  324. return (T)(object)AndroidJNI.CallStaticStringMethod(
  325. rawClass, method, jArgs);
  326. }
  327. else if (t == typeof(int))
  328. {
  329. return (T)(object)AndroidJNI.CallStaticIntMethod(
  330. rawClass, method, jArgs);
  331. }
  332. else if (t == typeof(float))
  333. {
  334. return (T)(object)AndroidJNI.CallStaticFloatMethod(
  335. rawClass, method, jArgs);
  336. }
  337. else if (t == typeof(double))
  338. {
  339. return (T)(object)AndroidJNI.CallStaticDoubleMethod(
  340. rawClass, method, jArgs);
  341. }
  342. else if (t == typeof(byte))
  343. {
  344. return (T)(object)AndroidJNI.CallStaticByteMethod(
  345. rawClass, method, jArgs);
  346. }
  347. else if (t == typeof(char))
  348. {
  349. return (T)(object)AndroidJNI.CallStaticCharMethod(
  350. rawClass, method, jArgs);
  351. }
  352. else if (t == typeof(long))
  353. {
  354. return (T)(object)AndroidJNI.CallStaticLongMethod(
  355. rawClass, method, jArgs);
  356. }
  357. else if (t == typeof(short))
  358. {
  359. return (T)(object)AndroidJNI.CallStaticShortMethod(
  360. rawClass, method, jArgs);
  361. }
  362. else
  363. {
  364. return StaticInvokeObjectCall<T>(type, name, sig, args);
  365. }
  366. }
  367. /// <summary>
  368. /// Invokes a method that returns an object.
  369. /// </summary>
  370. /// <returns>The object call.</returns>
  371. /// <param name="name">Name.</param>
  372. /// <param name="sig">Sig.</param>
  373. /// <param name="theArgs">The arguments.</param>
  374. /// <typeparam name="T">The 1st type parameter.</typeparam>
  375. public T InvokeObjectCall<T>(string name, string sig,
  376. params object[] theArgs)
  377. {
  378. IntPtr methodId = AndroidJNI.GetMethodID(RawClass, name, sig);
  379. jvalue[] jArgs = ConstructArgArray(theArgs);
  380. IntPtr val = AndroidJNI.CallObjectMethod(raw, methodId, jArgs);
  381. if (val.Equals(IntPtr.Zero))
  382. {
  383. return default(T);
  384. }
  385. ConstructorInfo ctor = typeof(T).GetConstructor(new Type[] { val.GetType() });
  386. if (ctor != null)
  387. {
  388. return (T)ctor.Invoke(new object[] { val });
  389. }
  390. Type t = typeof(T);
  391. return (T)Marshal.PtrToStructure(val, t);
  392. }
  393. }
  394. }
  395. #endif