/// <summary> /// Java object method callers /// </summary> /// <typeparam name="T">expect return type</typeparam> /// <param name="javaClass">Java class pointer</param> /// <param name="javaObject">Java Object pointer</param> /// <param name="methodName">Name of the method to call</param> /// <param name="sig">Method's JNI signature</param> /// <param name="param">Paramters of the method call</param> /// <returns></returns> public T CallMethod <T>(IntPtr javaClass, IntPtr javaObject, string methodName, string sig, params object[] param) { IntPtr methodId = env.GetMethodID(javaClass, methodName, sig); try { if (typeof(T) == typeof(byte)) { // Call the byte method byte res = env.CallByteMethod(javaObject, methodId, ParseParameters(javaClass, sig, param)); return((T)(object)res); } else if (typeof(T) == typeof(bool)) { // Call the boolean method bool res = env.CallBooleanMethod(javaObject, methodId, ParseParameters(javaClass, sig, param)); return((T)(object)res); } if (typeof(T) == typeof(char)) { // Call the char method char res = env.CallCharMethod(javaObject, methodId, ParseParameters(javaClass, sig, param)); return((T)(object)res); } else if (typeof(T) == typeof(short)) { // Call the short method short res = env.CallShortMethod(javaObject, methodId, ParseParameters(javaClass, sig, param)); return((T)(object)res); } else if (typeof(T) == typeof(int)) { // Call the int method int res = env.CallIntMethod(javaObject, methodId, ParseParameters(javaClass, sig, param)); return((T)(object)res); } else if (typeof(T) == typeof(long)) { // Call the long method long res = env.CallLongMethod(javaObject, methodId, ParseParameters(javaClass, sig, param)); return((T)(object)res); } else if (typeof(T) == typeof(float)) { // Call the float method float res = env.CallFloatMethod(javaObject, methodId, ParseParameters(javaClass, sig, param)); return((T)(object)res); } else if (typeof(T) == typeof(double)) { // Call the double method double res = env.CallDoubleMethod(javaObject, methodId, ParseParameters(javaClass, sig, param)); return((T)(object)res); // need to fix this } else if (typeof(T) == typeof(string)) { // Call the string method IntPtr jstr = env.CallObjectMethod(javaObject, methodId, ParseParameters(javaClass, sig, param)); string res = env.JStringToString(jstr); env.DeleteLocalRef(jstr); return((T)(object)res); } else if (typeof(T) == typeof(byte[])) { // Call the byte method IntPtr jobj = env.CallStaticObjectMethod(javaObject, methodId, ParseParameters(javaClass, sig, param)); if (jobj == IntPtr.Zero) { return(default(T)); } byte[] res = env.JStringToByte(jobj); env.DeleteLocalRef(jobj); return((T)(object)res); } else if (typeof(T) == typeof(string[])) { // Call the string array method IntPtr jobj = env.CallObjectMethod(javaObject, methodId, ParseParameters(javaClass, sig, param)); if (jobj == IntPtr.Zero) { return(default(T)); } IntPtr[] objArray = env.GetObjectArray(jobj); string[] res = new string[objArray.Length]; for (int i = 0; i < objArray.Length; i++) { res[i] = env.JStringToString(objArray[i]); } env.DeleteLocalRef(jobj); return((T)(object)res); } else if (typeof(T) == typeof(int[])) { // Call the int array method IntPtr jobj = env.CallObjectMethod(javaObject, methodId, ParseParameters(javaClass, sig, param)); if (jobj == IntPtr.Zero) { return(default(T)); } int[] res = env.GetIntArray(jobj); env.DeleteLocalRef(jobj); return((T)(object)res); } else if (typeof(T) == typeof(IntPtr)) { // Call the object method and deal with whatever comes back in the call code IntPtr res = env.CallObjectMethod(javaObject, methodId, ParseParameters(javaClass, sig, param)); return((T)(object)res); } return(default(T)); } catch { throw new Exception(env.CatchJavaException()); } }