From 312fbf439ed874bb5f4f25ee6d2c9a2b3c2f5a8b Mon Sep 17 00:00:00 2001 From: Jonathan Pryor Date: Tue, 4 Jan 2022 20:02:20 -0500 Subject: [PATCH] [jnienv-gen] Add possible C#9 function pointer backend (#938) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Context: 926e4bc46ede2cad861d199a5f4290195488a5af Context: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-9.0/function-pointers Context? https://github.com/xamarin/java.interop/issues/666 Commit 926e4bc4 allowed `jnienv-gen` to emit multiple different JNIEnv invocation strategies at the same time, allowing `tests/invocation-overhead` to try them "all at once" for side-by- side comparisons. Add support for a new JNIEnv invocation strategy which relies on C#9 Function Pointers, a'la: partial struct JNIEnv { public delegate* unmanaged ExceptionOccurred; } partial class JniEnvironment { partial class Exceptions { public static unsafe JniObjectReference ExceptionOccurred () { IntPtr __env = JniEnvironment.EnvironmentPointer; var tmp = (*((JNIEnv**)__env))->ExceptionOccurred (__env); return new JniObjectReference (tmp, JniObjectReferenceType.Local); } } } This *could* allow for performance better than "JIPinvokeTiming", as it avoids P/Invoke overheads to a set of `java_interop_*` C functions (926e4bc4), while *also* avoiding the overheads involved with using `Marshal.GetDelegateForFunctionPointer()` as used by "JIIntPtrs". …but it doesn't necessarily provide better performance: $ JI_JVM_PATH=$HOME/android-toolchain/jdk-11/lib/jli/libjli.dylib dotnet tests/invocation-overhead/bin/Debug/net6.0/invocation-overhead.dll # SafeTiming timing: 00:00:04.2123508 # Average Invocation: 0.00042123508ms # XAIntPtrTiming timing: 00:00:02.1625501 # Average Invocation: 0.00021625500999999998ms # JIIntPtrTiming timing: 00:00:02.3620239 # Average Invocation: 0.00023620239ms # JIPinvokeTiming timing: 00:00:01.8993587 # Average Invocation: 0.00018993587ms # JIFunctionPointersTiming timing: 00:00:02.0278083 # Average Invocation: 0.00020278083ms (Compare and contrast with 926e4bc4, circa 2015!) Of particular note is that the Average Invocation time for JIFunctionPointersTiming takes 7% longer than JIPinvokeTiming. Though that's slightly reversed when a *Release* build of `invocation-overhead.dll` is used: % JI_JVM_PATH=$HOME/android-toolchain/jdk-11/lib/jli/libjli.dylib dotnet tests/invocation-overhead/bin/Release/net6.0/invocation-overhead.dll # SafeTiming timing: 00:00:03.4128431 # Average Invocation: 0.00034128431000000003ms # XAIntPtrTiming timing: 00:00:01.8857456 # Average Invocation: 0.00018857455999999999ms # JIIntPtrTiming timing: 00:00:01.9075412 # Average Invocation: 0.00019075412ms # JIPinvokeTiming timing: 00:00:01.6993644 # Average Invocation: 0.00016993643999999998ms # JIFunctionPointersTiming timing: 00:00:01.6561349 # Average Invocation: 0.00016561349ms With a Release build, the Average Invocation time for JIFunctionPointersTiming takes 97% of the time as JIPinvokeTiming, i.e. is 3% faster. We may or may not continue investigation of C#9 Function Pointers for `JNIEnv` binding purposes. We will preserve this code for future investigation. --- build-tools/jnienv-gen/Generator.cs | 176 +- .../invocation-overhead.cs | 92 +- .../invocation-overhead.csproj | 1 + tests/invocation-overhead/jni.cs | 3256 ++++++++++++++++- 4 files changed, 3507 insertions(+), 18 deletions(-) diff --git a/build-tools/jnienv-gen/Generator.cs b/build-tools/jnienv-gen/Generator.cs index c0fe2786f..e0e582d47 100644 --- a/build-tools/jnienv-gen/Generator.cs +++ b/build-tools/jnienv-gen/Generator.cs @@ -111,13 +111,13 @@ static void GenerateFile (TextWriter o) o.WriteLine (); o.WriteLine ("using JNIEnvPtr = System.IntPtr;"); o.WriteLine (); - o.WriteLine ("#if FEATURE_JNIENVIRONMENT_JI_INTPTRS || FEATURE_JNIENVIRONMENT_JI_PINVOKES"); + o.WriteLine ("#if FEATURE_JNIENVIRONMENT_JI_INTPTRS || FEATURE_JNIENVIRONMENT_JI_PINVOKES || FEATURE_JNIENVIRONMENT_JI_FUNCTION_POINTERS"); o.WriteLine ("\tusing jinstanceFieldID = System.IntPtr;"); o.WriteLine ("\tusing jstaticFieldID = System.IntPtr;"); o.WriteLine ("\tusing jinstanceMethodID = System.IntPtr;"); o.WriteLine ("\tusing jstaticMethodID = System.IntPtr;"); o.WriteLine ("\tusing jobject = System.IntPtr;"); - o.WriteLine ("#endif // FEATURE_JNIENVIRONMENT_JI_INTPTRS || FEATURE_JNIENVIRONMENT_JI_PINVOKES"); + o.WriteLine ("#endif // FEATURE_JNIENVIRONMENT_JI_INTPTRS || FEATURE_JNIENVIRONMENT_JI_PINVOKES || FEATURE_JNIENVIRONMENT_JI_FUNCTION_POINTERS"); o.WriteLine (); o.WriteLine ("namespace Java.Interop {"); GenerateJniNativeInterface (o); @@ -126,6 +126,7 @@ static void GenerateFile (TextWriter o) WriteSection (o, HandleStyle.JIIntPtr, "FEATURE_JNIENVIRONMENT_JI_INTPTRS", "Java.Interop.JIIntPtrs"); WriteSection (o, HandleStyle.JIIntPtrPinvokeWithErrors, "FEATURE_JNIENVIRONMENT_JI_PINVOKES", "Java.Interop.JIPinvokes"); WriteSection (o, HandleStyle.XAIntPtr, "FEATURE_JNIENVIRONMENT_XA_INTPTRS", "Java.Interop.XAIntPtrs"); + WriteSection (o, HandleStyle.JIFunctionPtrWithErrors, "FEATURE_JNIENVIRONMENT_JI_FUNCTION_POINTERS", "Java.Interop.JIFunctionPointers"); } static void WriteSection (TextWriter o, HandleStyle style, string define, string specificNamespace) @@ -139,7 +140,7 @@ static void WriteSection (TextWriter o, HandleStyle style, string define, string o.WriteLine ("#endif"); o.WriteLine ("{"); o.WriteLine (); - if (style != HandleStyle.JIIntPtrPinvokeWithErrors) { + if (style != HandleStyle.JIIntPtrPinvokeWithErrors && style != HandleStyle.JIFunctionPtrWithErrors) { GenerateDelegates (o, style); o.WriteLine (); } @@ -166,6 +167,10 @@ static void GenerateDelegates (TextWriter o, HandleStyle style) static void GenerateJniNativeInterface (TextWriter o) { + o.WriteLine ("#pragma warning disable 0649 // Field is assigned to, and will always have its default value `null`; ignore as it'll be set in native code."); + o.WriteLine ("#pragma warning disable 0169 // Field never used; ignore since these fields make the structure have the right layout."); + o.WriteLine (); + o.WriteLine ("#if FEATURE_JNIENVIRONMENT_SAFEHANDLES || FEATURE_JNIENVIRONMENT_JI_INTPTRS || FEATURE_JNIENVIRONMENT_XA_INTPTRS"); o.WriteLine ("\t[StructLayout (LayoutKind.Sequential)]"); o.WriteLine ("\tpartial struct JniNativeInterfaceStruct {"); @@ -173,19 +178,45 @@ static void GenerateJniNativeInterface (TextWriter o) int maxName = JNIEnvEntries.Max (e => e.Name.Length); - o.WriteLine ("#pragma warning disable 0649 // Field is assigned to, and will always have its default value `null`; ignore as it'll be set in native code."); - o.WriteLine ("#pragma warning disable 0169 // Field never used; ignore since these fields make the structure have the right layout."); - for (int i = 0; i < 4; i++) o.WriteLine ("\t\tprivate IntPtr reserved{0}; // void*", i); foreach (var e in JNIEnvEntries) { o.WriteLine ("\t\tpublic IntPtr {0};{1} // {2}", e.Name, new string (' ', maxName - e.Name.Length), e.Prototype); } - o.WriteLine ("#pragma warning restore 0169"); - o.WriteLine ("#pragma warning restore 0649"); o.WriteLine ("\t}"); o.WriteLine ("#endif // FEATURE_JNIENVIRONMENT_SAFEHANDLES || FEATURE_JNIENVIRONMENT_JI_INTPTRS || FEATURE_JNIENVIRONMENT_XA_INTPTRS"); + o.WriteLine (); + + o.WriteLine ("#if FEATURE_JNIENVIRONMENT_JI_FUNCTION_POINTERS"); + o.WriteLine ("\t[StructLayout (LayoutKind.Sequential)]"); + o.WriteLine ("\tunsafe partial struct JNIEnv {"); + + for (int i = 0; i < 4; i++) + o.WriteLine ("\t\tprivate IntPtr reserved{0}; // void*", i); + + foreach (var e in JNIEnvEntries) { + if (e.Parameters.Length > 0 && + "va_list" == e.Parameters [e.Parameters.Length-1].Type.GetManagedType (HandleStyle.JIFunctionPtrWithErrors, isReturn: false, isPinvoke: true)) { + o.WriteLine ("\t\tpublic IntPtr {0};{1} // {2}", e.Name, new string (' ', maxName - e.Name.Length), e.Prototype); + continue; + } + o.Write ("\t\tpublic delegate* unmanaged {e.Name};"); + } + o.WriteLine ("\t}"); + o.WriteLine ("#endif // FEATURE_JNIENVIRONMENT_JI_FUNCTION_POINTERS"); + + o.WriteLine (); + o.WriteLine ("#pragma warning restore 0169"); + o.WriteLine ("#pragma warning restore 0649"); } static string Initialize (JniFunction e, string prefix, string delegateType) @@ -379,9 +410,12 @@ static void GenerateJniEnv (TextWriter o, string type, string visibility, Handle o.WriteLine (")"); o.WriteLine ("\t\t{"); NullCheckParameters (o, entry.Parameters, style); + PrepareParameters (o, entry.Parameters, style); if (style == HandleStyle.JIIntPtrPinvokeWithErrors) { if (entry.Throws) o.WriteLine ("\t\t\tIntPtr thrown;"); + } else if (style == HandleStyle.JIFunctionPtrWithErrors) { + o.WriteLine ($"\t\t\tIntPtr __env = JniEnvironment.EnvironmentPointer;"); } else { o.WriteLine ("\t\t\tvar __info = JniEnvironment.CurrentInfo;"); } @@ -392,17 +426,27 @@ static void GenerateJniEnv (TextWriter o, string type, string visibility, Handle o.Write ("NativeMethods.{0} (JniEnvironment.EnvironmentPointer{1}", GetPinvokeName (entry.Name), entry.Throws ? ", out thrown" : ""); + } else if (style == HandleStyle.JIFunctionPtrWithErrors) { + o.Write ($"(*((JNIEnv**)__env))->{entry.Name} (__env"); } else { o.Write ("__info.Invoker.{0} (__info.EnvironmentPointer", entry.Name); } for (int i = 0; i < entry.Parameters.Length; i++) { var p = entry.Parameters [i]; o.Write (", "); - if (p.Type.GetManagedType (style, isReturn: false).StartsWith ("out ", StringComparison.Ordinal)) + var needOut = p.Type.GetManagedType (style, isReturn: false).StartsWith ("out ", StringComparison.Ordinal); + if (needOut && style == HandleStyle.JIFunctionPtrWithErrors) { + o.Write ("&"); + } else if (needOut) { o.Write ("out "); + } o.Write (p.Type.GetManagedToMarshalExpression (style, Escape (entry.Parameters [i].Name))); } o.WriteLine (");"); + if (style == HandleStyle.JIFunctionPtrWithErrors && entry.Throws) { + o.WriteLine ("\t\t\tIntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env);"); + } + CleanupParameters (o, entry.Parameters, style); RaiseException (o, entry, style); if (is_void) { } else { @@ -436,6 +480,28 @@ static void NullCheckParameters (TextWriter o, ParamInfo[] ps, HandleStyle style o.WriteLine (); } + static void PrepareParameters (TextWriter o, ParamInfo[] ps, HandleStyle style) + { + bool haveChecks = false; + foreach (var e in ps) { + foreach (var s in e.Type.GetManagedToMarshalPrepareStatements (style, Escape (e.Name))) { + haveChecks = true; + o.WriteLine ($"\t\t\t{s}"); + } + } + if (haveChecks) + o.WriteLine (); + } + + static void CleanupParameters (TextWriter o, ParamInfo[] ps, HandleStyle style) + { + foreach (var e in ps) { + foreach (var s in e.Type.GetManagedToMarshalCleanupStatements (style, Escape (e.Name))) { + o.WriteLine ($"\t\t\t{s}"); + } + } + } + static void RaiseException (TextWriter o, JniFunction entry, HandleStyle style) { if (!entry.Throws) @@ -443,7 +509,9 @@ static void RaiseException (TextWriter o, JniFunction entry, HandleStyle style) o.WriteLine (); o.WriteLine ("\t\t\tException __e = JniEnvironment.GetExceptionForLastThrowable ({0});", - style == HandleStyle.JIIntPtrPinvokeWithErrors ? "thrown" : ""); + (style == HandleStyle.JIIntPtrPinvokeWithErrors || style == HandleStyle.JIFunctionPtrWithErrors) + ? "thrown" + : ""); o.WriteLine ("\t\t\tif (__e != null)"); o.WriteLine ("\t\t\t\tExceptionDispatchInfo.Capture (__e).Throw ();"); o.WriteLine (); @@ -728,6 +796,9 @@ public virtual string[] VerifyParameter (HandleStyle style, string variable) { return new string [0]; } + + public virtual string[] GetManagedToMarshalPrepareStatements (HandleStyle style, string variable) => Array.Empty (); + public virtual string[] GetManagedToMarshalCleanupStatements (HandleStyle style, string variable) => Array.Empty (); } class BuiltinTypeInfo : TypeInfo { @@ -822,6 +893,9 @@ public StringTypeInfo (string jni) public override string GetMarshalType (HandleStyle style, bool isReturn, bool isPinvoke) { + if (style == HandleStyle.JIFunctionPtrWithErrors && isPinvoke) { + return "IntPtr"; + } return "string"; } @@ -830,6 +904,16 @@ public override string GetManagedType (HandleStyle style, bool isReturn, bool is return "string"; } + public override string GetManagedToMarshalExpression (HandleStyle style, string variable) + { + switch (style) { + case HandleStyle.JIFunctionPtrWithErrors: + return $"_{variable}_ptr"; + default: + return variable; + } + } + public override string[] GetMarshalToManagedStatements (HandleStyle style, string variable, JniFunction entry) { switch (style) { @@ -841,6 +925,7 @@ public override string[] GetMarshalToManagedStatements (HandleStyle style, strin }; case HandleStyle.JIIntPtr: case HandleStyle.JIIntPtrPinvokeWithErrors: + case HandleStyle.JIFunctionPtrWithErrors: return new [] { string.Format ("JniEnvironment.LogCreateLocalRef ({0});", variable), string.Format ("return new JniObjectReference ({0}, JniObjectReferenceType.Local);", variable), @@ -859,6 +944,30 @@ public override string[] VerifyParameter (HandleStyle style, string variable) string.Format ("\tthrow new ArgumentNullException (\"{0}\");", variableName), }; } + + public override string[] GetManagedToMarshalPrepareStatements (HandleStyle style, string variable) + { + switch (style) { + case HandleStyle.JIFunctionPtrWithErrors: + return new[]{ + $"var _{variable}_ptr = Marshal.StringToCoTaskMemUTF8 ({variable});", + }; + default: + return base.GetManagedToMarshalPrepareStatements (style, variable); + } + } + + public override string[] GetManagedToMarshalCleanupStatements (HandleStyle style, string variable) + { + switch (style) { + case HandleStyle.JIFunctionPtrWithErrors: + return new[]{ + $"Marshal.ZeroFreeCoTaskMemUTF8 (_{variable}_ptr);", + }; + default: + return base.GetManagedToMarshalCleanupStatements (style, variable); + } + } } class JniReleaseArrayElementsModeTypeInfo : TypeInfo { @@ -920,6 +1029,7 @@ public override string GetManagedType (HandleStyle style, bool isReturn, bool is case HandleStyle.SafeHandle: case HandleStyle.JIIntPtr: case HandleStyle.JIIntPtrPinvokeWithErrors: + case HandleStyle.JIFunctionPtrWithErrors: return type; case HandleStyle.XAIntPtr: return "IntPtr"; @@ -933,6 +1043,7 @@ public override string GetManagedToMarshalExpression (HandleStyle style, string case HandleStyle.SafeHandle: case HandleStyle.JIIntPtr: case HandleStyle.JIIntPtrPinvokeWithErrors: + case HandleStyle.JIFunctionPtrWithErrors: return string.Format ("{0}.ID", variable); } return variable; @@ -947,6 +1058,7 @@ public override string[] VerifyParameter (HandleStyle style, string variable) case HandleStyle.SafeHandle: case HandleStyle.JIIntPtr: case HandleStyle.JIIntPtrPinvokeWithErrors: + case HandleStyle.JIFunctionPtrWithErrors: return new [] { string.Format ("if ({0} == null)", variable), string.Format ("\tthrow new ArgumentNullException (\"{0}\");", variableName), @@ -969,6 +1081,7 @@ public override string[] GetMarshalToManagedStatements (HandleStyle style, strin case HandleStyle.SafeHandle: case HandleStyle.JIIntPtr: case HandleStyle.JIIntPtrPinvokeWithErrors: + case HandleStyle.JIFunctionPtrWithErrors: return new[] { string.Format ("if ({0} == IntPtr.Zero)", variable), string.Format ("\treturn null;"), @@ -1045,6 +1158,7 @@ public override string GetMarshalType (HandleStyle style, bool isReturn, bool is return isReturn ? safeType : "JniReferenceSafeHandle"; case HandleStyle.JIIntPtr: case HandleStyle.JIIntPtrPinvokeWithErrors: + case HandleStyle.JIFunctionPtrWithErrors: case HandleStyle.XAIntPtr: return "jobject"; } @@ -1057,6 +1171,7 @@ public override string GetManagedType (HandleStyle style, bool isReturn, bool is case HandleStyle.SafeHandle: case HandleStyle.JIIntPtr: case HandleStyle.JIIntPtrPinvokeWithErrors: + case HandleStyle.JIFunctionPtrWithErrors: return "JniObjectReference"; case HandleStyle.XAIntPtr: return "IntPtr"; @@ -1071,6 +1186,7 @@ public override string GetManagedToMarshalExpression (HandleStyle style, string return string.Format ("{0}.SafeHandle", variable); case HandleStyle.JIIntPtr: case HandleStyle.JIIntPtrPinvokeWithErrors: + case HandleStyle.JIFunctionPtrWithErrors: return string.Format ("{0}.Handle", variable); case HandleStyle.XAIntPtr: return variable; @@ -1084,6 +1200,7 @@ public override string[] GetMarshalToManagedStatements (HandleStyle style, strin case HandleStyle.SafeHandle: case HandleStyle.JIIntPtr: case HandleStyle.JIIntPtrPinvokeWithErrors: + case HandleStyle.JIFunctionPtrWithErrors: return new [] { string.Format ("return new JniObjectReference ({0}, {1});", variable, refType), }; @@ -1104,6 +1221,7 @@ public override string[] VerifyParameter (HandleStyle style, string variable) case HandleStyle.SafeHandle: case HandleStyle.JIIntPtr: case HandleStyle.JIIntPtrPinvokeWithErrors: + case HandleStyle.JIFunctionPtrWithErrors: return new [] { string.Format ("if (!{0}.IsValid)", variable), string.Format ("\tthrow new ArgumentException (\"Handle must be valid.\", \"{0}\");", variableName), @@ -1160,6 +1278,9 @@ public JavaVMPointerTypeInfo (string jni) public override string GetMarshalType (HandleStyle style, bool isReturn, bool isPinvoke) { + if (style == HandleStyle.JIFunctionPtrWithErrors && isPinvoke) { + return "IntPtr*"; + } return "out IntPtr"; } @@ -1167,6 +1288,40 @@ public override string GetManagedType (HandleStyle style, bool isReturn, bool is { return "out IntPtr"; } + + public override string GetManagedToMarshalExpression (HandleStyle style, string variable) + { + switch (style) { + case HandleStyle.JIFunctionPtrWithErrors: + return $"_{variable}_ptr"; + default: + return variable; + } + } + + public override string[] GetManagedToMarshalPrepareStatements (HandleStyle style, string variable) + { + switch (style) { + case HandleStyle.JIFunctionPtrWithErrors: + return new[]{ + $"IntPtr _{variable}_ptr = IntPtr.Zero;", + }; + default: + return base.GetManagedToMarshalPrepareStatements (style, variable); + } + } + + public override string[] GetManagedToMarshalCleanupStatements (HandleStyle style, string variable) + { + switch (style) { + case HandleStyle.JIFunctionPtrWithErrors: + return new[]{ + $"{variable} = _{variable}_ptr;", + }; + default: + return base.GetManagedToMarshalCleanupStatements (style, variable); + } + } } class ParamInfo @@ -1204,6 +1359,7 @@ enum HandleStyle { JIIntPtr, JIIntPtrPinvokeWithErrors, XAIntPtr, + JIFunctionPtrWithErrors, } } diff --git a/tests/invocation-overhead/invocation-overhead.cs b/tests/invocation-overhead/invocation-overhead.cs index 39a30f0b9..2fe7f54c3 100644 --- a/tests/invocation-overhead/invocation-overhead.cs +++ b/tests/invocation-overhead/invocation-overhead.cs @@ -9,6 +9,9 @@ using JIIntPtrEnv = Java.Interop.JIIntPtrs.JniEnvironment; using PinvokeEnv = Java.Interop.JIPinvokes.JniEnvironment; using XAIntPtrEnv = Java.Interop.XAIntPtrs.JniEnvironment; +#if NET +using JIFuncPtrEnv = Java.Interop.JIFunctionPointers.JniEnvironment; +#endif // NET public class XFieldInfo { @@ -343,9 +346,9 @@ public static Exception GetExceptionForLastThrowable () var h = v.Handle; if (h == IntPtr.Zero) return null; - SafeEnv.Exceptions.ExceptionClear (); + PinvokeEnv.Exceptions.ExceptionClear (); LogCreateLocalRef (h); - JIIntPtrEnv.References.DeleteLocalRef (h); + PinvokeEnv.References.DeleteLocalRef (h); return new Exception ("yada yada yada"); } } @@ -402,6 +405,67 @@ public static Exception GetExceptionForLastThrowable () } } +#if NET +namespace Java.Interop.JIFunctionPointers { + public class JniFieldInfo : XFieldInfo { + public JniFieldInfo (string name, string signature, IntPtr id, bool isStatic) + : base (name, signature, id, isStatic) + { + } + } + public class JniMethodInfo : XMethodInfo { + public JniMethodInfo (string name, string signature, IntPtr id, bool isStatic) + : base (name, signature, id, isStatic) + { + } + } + public struct JniObjectReference + { + public IntPtr Handle {get; private set;} + public JniObjectReferenceType Type {get; private set;} + public bool IsValid { + get { + return Handle != IntPtr.Zero; + } + } + + public JniObjectReference (IntPtr handle, JniObjectReferenceType type = JniObjectReferenceType.Invalid) + { + Handle = handle; + Type = type; + } + } + public static partial class JniEnvironment { + public static IntPtr EnvironmentPointer; + + internal static void LogCreateLocalRef (IntPtr value) + { + } + public static Exception GetExceptionForLastThrowable (IntPtr h) + { + if (h == IntPtr.Zero) + return null; + JIFuncPtrEnv.Exceptions.ExceptionClear (); + LogCreateLocalRef (h); + var r = new JniObjectReference (h, JniObjectReferenceType.Local); + JIFuncPtrEnv.References.DeleteLocalRef (r.Handle); + return new Exception ("yada yada yada"); + } + public static Exception GetExceptionForLastThrowable () + { + var v = JIIntPtrEnv.Exceptions.ExceptionOccurred (); + var h = v.Handle; + if (h == IntPtr.Zero) + return null; + JIFuncPtrEnv.Exceptions.ExceptionClear (); + LogCreateLocalRef (h); + JIFuncPtrEnv.References.DeleteLocalRef (h); + return new Exception ("yada yada yada"); + } + } +} +#endif // NET + class App { public static void Main () @@ -416,6 +480,7 @@ public static void Main () XAIntPtrTiming (_env); JIIntPtrTiming (_env); JIPinvokeTiming (_env); + JIFunctionPointersTiming (_env); GlobalRuntime.Dispose (); } @@ -517,4 +582,27 @@ static unsafe void XAIntPtrTiming (IntPtr _env) Console.WriteLine ("# {0} timing: {1}", nameof (XAIntPtrTiming), t.Elapsed); Console.WriteLine ("#\tAverage Invocation: {0}ms", t.Elapsed.TotalMilliseconds / C); } + + static unsafe void JIFunctionPointersTiming (IntPtr _env) + { +#if NET + JIFuncPtrEnv.EnvironmentPointer = _env; + var Arrays_class = JIFuncPtrEnv.Types._FindClass ("java/util/Arrays"); + var Arrays_binarySearch = JIFuncPtrEnv.StaticMethods.GetStaticMethodID (Arrays_class, "binarySearch", "([II)I"); + var intArray = JIFuncPtrEnv.Arrays.NewIntArray (3); + fixed (int* p = new int[]{1,2,3}) + JIFuncPtrEnv.Arrays.SetIntArrayRegion (intArray, 0, 3, p); + + var t = Stopwatch.StartNew (); + var args = stackalloc JniArgumentValue [2]; + args [0] = new JniArgumentValue (intArray.Handle); + args [1] = new JniArgumentValue (2); + for (int i = 0; i < C; ++i) { + int r = JIFuncPtrEnv.StaticMethods.CallStaticIntMethod (Arrays_class, Arrays_binarySearch, args); + } + t.Stop (); + Console.WriteLine ("# {0} timing: {1}", nameof (JIFunctionPointersTiming), t.Elapsed); + Console.WriteLine ("#\tAverage Invocation: {0}ms", t.Elapsed.TotalMilliseconds / C); +#endif // NET + } } diff --git a/tests/invocation-overhead/invocation-overhead.csproj b/tests/invocation-overhead/invocation-overhead.csproj index 880f080c7..eb984eadd 100644 --- a/tests/invocation-overhead/invocation-overhead.csproj +++ b/tests/invocation-overhead/invocation-overhead.csproj @@ -6,6 +6,7 @@ True True FEATURE_JNIENVIRONMENT_JI_INTPTRS;FEATURE_JNIENVIRONMENT_JI_PINVOKES;FEATURE_JNIENVIRONMENT_SAFEHANDLES;FEATURE_JNIENVIRONMENT_XA_INTPTRS + $(DefineConstants);FEATURE_JNIENVIRONMENT_JI_FUNCTION_POINTERS diff --git a/tests/invocation-overhead/jni.cs b/tests/invocation-overhead/jni.cs index 39054e122..f1006a05a 100644 --- a/tests/invocation-overhead/jni.cs +++ b/tests/invocation-overhead/jni.cs @@ -26,21 +26,22 @@ using JNIEnvPtr = System.IntPtr; -#if FEATURE_JNIENVIRONMENT_JI_INTPTRS || FEATURE_JNIENVIRONMENT_JI_PINVOKES +#if FEATURE_JNIENVIRONMENT_JI_INTPTRS || FEATURE_JNIENVIRONMENT_JI_PINVOKES || FEATURE_JNIENVIRONMENT_JI_FUNCTION_POINTERS using jinstanceFieldID = System.IntPtr; using jstaticFieldID = System.IntPtr; using jinstanceMethodID = System.IntPtr; using jstaticMethodID = System.IntPtr; using jobject = System.IntPtr; -#endif // FEATURE_JNIENVIRONMENT_JI_INTPTRS || FEATURE_JNIENVIRONMENT_JI_PINVOKES +#endif // FEATURE_JNIENVIRONMENT_JI_INTPTRS || FEATURE_JNIENVIRONMENT_JI_PINVOKES || FEATURE_JNIENVIRONMENT_JI_FUNCTION_POINTERS namespace Java.Interop { +#pragma warning disable 0649 // Field is assigned to, and will always have its default value `null`; ignore as it'll be set in native code. +#pragma warning disable 0169 // Field never used; ignore since these fields make the structure have the right layout. + #if FEATURE_JNIENVIRONMENT_SAFEHANDLES || FEATURE_JNIENVIRONMENT_JI_INTPTRS || FEATURE_JNIENVIRONMENT_XA_INTPTRS [StructLayout (LayoutKind.Sequential)] partial struct JniNativeInterfaceStruct { -#pragma warning disable 0649 // Field is assigned to, and will always have its default value `null`; ignore as it'll be set in native code. -#pragma warning disable 0169 // Field never used; ignore since these fields make the structure have the right layout. private IntPtr reserved0; // void* private IntPtr reserved1; // void* private IntPtr reserved2; // void* @@ -274,10 +275,250 @@ partial struct JniNativeInterfaceStruct { public IntPtr GetDirectBufferAddress; // void* (*GetDirectBufferAddress)(JNIEnv*, jobject); public IntPtr GetDirectBufferCapacity; // jlong (*GetDirectBufferCapacity)(JNIEnv*, jobject); public IntPtr GetObjectRefType; // jobjectRefType (*GetObjectRefType)(JNIEnv*, jobject); -#pragma warning restore 0169 -#pragma warning restore 0649 } #endif // FEATURE_JNIENVIRONMENT_SAFEHANDLES || FEATURE_JNIENVIRONMENT_JI_INTPTRS || FEATURE_JNIENVIRONMENT_XA_INTPTRS + +#if FEATURE_JNIENVIRONMENT_JI_FUNCTION_POINTERS + [StructLayout (LayoutKind.Sequential)] + unsafe partial struct JNIEnv { + private IntPtr reserved0; // void* + private IntPtr reserved1; // void* + private IntPtr reserved2; // void* + private IntPtr reserved3; // void* + public delegate* unmanaged GetVersion; + public delegate* unmanaged DefineClass; + public delegate* unmanaged FindClass; + public delegate* unmanaged FromReflectedMethod; + public delegate* unmanaged FromReflectedField; + public delegate* unmanaged ToReflectedMethod; + public delegate* unmanaged GetSuperclass; + public delegate* unmanaged IsAssignableFrom; + public delegate* unmanaged ToReflectedField; + public delegate* unmanaged Throw; + public delegate* unmanaged ThrowNew; + public delegate* unmanaged ExceptionOccurred; + public delegate* unmanaged ExceptionDescribe; + public delegate* unmanaged ExceptionClear; + public delegate* unmanaged FatalError; + public delegate* unmanaged PushLocalFrame; + public delegate* unmanaged PopLocalFrame; + public delegate* unmanaged NewGlobalRef; + public delegate* unmanaged DeleteGlobalRef; + public delegate* unmanaged DeleteLocalRef; + public delegate* unmanaged IsSameObject; + public delegate* unmanaged NewLocalRef; + public delegate* unmanaged EnsureLocalCapacity; + public delegate* unmanaged AllocObject; + public delegate* unmanaged NewObject; + public IntPtr NewObjectV; // jobject (*NewObjectV)(JNIEnv*, jclass, jmethodID, va_list); + public delegate* unmanaged NewObjectA; + public delegate* unmanaged GetObjectClass; + public delegate* unmanaged IsInstanceOf; + public delegate* unmanaged GetMethodID; + public delegate* unmanaged CallObjectMethod; + public IntPtr CallObjectMethodV; // jobject (*CallObjectMethodV)(JNIEnv*, jobject, jmethodID, va_list); + public delegate* unmanaged CallObjectMethodA; + public delegate* unmanaged CallBooleanMethod; + public IntPtr CallBooleanMethodV; // jboolean (*CallBooleanMethodV)(JNIEnv*, jobject, jmethodID, va_list); + public delegate* unmanaged CallBooleanMethodA; + public delegate* unmanaged CallByteMethod; + public IntPtr CallByteMethodV; // jbyte (*CallByteMethodV)(JNIEnv*, jobject, jmethodID, va_list); + public delegate* unmanaged CallByteMethodA; + public delegate* unmanaged CallCharMethod; + public IntPtr CallCharMethodV; // jchar (*CallCharMethodV)(JNIEnv*, jobject, jmethodID, va_list); + public delegate* unmanaged CallCharMethodA; + public delegate* unmanaged CallShortMethod; + public IntPtr CallShortMethodV; // jshort (*CallShortMethodV)(JNIEnv*, jobject, jmethodID, va_list); + public delegate* unmanaged CallShortMethodA; + public delegate* unmanaged CallIntMethod; + public IntPtr CallIntMethodV; // jint (*CallIntMethodV)(JNIEnv*, jobject, jmethodID, va_list); + public delegate* unmanaged CallIntMethodA; + public delegate* unmanaged CallLongMethod; + public IntPtr CallLongMethodV; // jlong (*CallLongMethodV)(JNIEnv*, jobject, jmethodID, va_list); + public delegate* unmanaged CallLongMethodA; + public delegate* unmanaged CallFloatMethod; + public IntPtr CallFloatMethodV; // jfloat (*CallFloatMethodV)(JNIEnv*, jobject, jmethodID, va_list); + public delegate* unmanaged CallFloatMethodA; + public delegate* unmanaged CallDoubleMethod; + public IntPtr CallDoubleMethodV; // jdouble (*CallDoubleMethodV)(JNIEnv*, jobject, jmethodID, va_list); + public delegate* unmanaged CallDoubleMethodA; + public delegate* unmanaged CallVoidMethod; + public IntPtr CallVoidMethodV; // void (*CallVoidMethodV)(JNIEnv*, jobject, jmethodID, va_list); + public delegate* unmanaged CallVoidMethodA; + public delegate* unmanaged CallNonvirtualObjectMethod; + public IntPtr CallNonvirtualObjectMethodV; // jobject (*CallNonvirtualObjectMethodV)(JNIEnv*, jobject, jclass, jmethodID, va_list); + public delegate* unmanaged CallNonvirtualObjectMethodA; + public delegate* unmanaged CallNonvirtualBooleanMethod; + public IntPtr CallNonvirtualBooleanMethodV; // jboolean (*CallNonvirtualBooleanMethodV)(JNIEnv*, jobject, jclass, jmethodID, va_list); + public delegate* unmanaged CallNonvirtualBooleanMethodA; + public delegate* unmanaged CallNonvirtualByteMethod; + public IntPtr CallNonvirtualByteMethodV; // jbyte (*CallNonvirtualByteMethodV)(JNIEnv*, jobject, jclass, jmethodID, va_list); + public delegate* unmanaged CallNonvirtualByteMethodA; + public delegate* unmanaged CallNonvirtualCharMethod; + public IntPtr CallNonvirtualCharMethodV; // jchar (*CallNonvirtualCharMethodV)(JNIEnv*, jobject, jclass, jmethodID, va_list); + public delegate* unmanaged CallNonvirtualCharMethodA; + public delegate* unmanaged CallNonvirtualShortMethod; + public IntPtr CallNonvirtualShortMethodV; // jshort (*CallNonvirtualShortMethodV)(JNIEnv*, jobject, jclass, jmethodID, va_list); + public delegate* unmanaged CallNonvirtualShortMethodA; + public delegate* unmanaged CallNonvirtualIntMethod; + public IntPtr CallNonvirtualIntMethodV; // jint (*CallNonvirtualIntMethodV)(JNIEnv*, jobject, jclass, jmethodID, va_list); + public delegate* unmanaged CallNonvirtualIntMethodA; + public delegate* unmanaged CallNonvirtualLongMethod; + public IntPtr CallNonvirtualLongMethodV; // jlong (*CallNonvirtualLongMethodV)(JNIEnv*, jobject, jclass, jmethodID, va_list); + public delegate* unmanaged CallNonvirtualLongMethodA; + public delegate* unmanaged CallNonvirtualFloatMethod; + public IntPtr CallNonvirtualFloatMethodV; // jfloat (*CallNonvirtualFloatMethodV)(JNIEnv*, jobject, jclass, jmethodID, va_list); + public delegate* unmanaged CallNonvirtualFloatMethodA; + public delegate* unmanaged CallNonvirtualDoubleMethod; + public IntPtr CallNonvirtualDoubleMethodV; // jdouble (*CallNonvirtualDoubleMethodV)(JNIEnv*, jobject, jclass, jmethodID, va_list); + public delegate* unmanaged CallNonvirtualDoubleMethodA; + public delegate* unmanaged CallNonvirtualVoidMethod; + public IntPtr CallNonvirtualVoidMethodV; // void (*CallNonvirtualVoidMethodV)(JNIEnv*, jobject, jclass, jmethodID, va_list); + public delegate* unmanaged CallNonvirtualVoidMethodA; + public delegate* unmanaged GetFieldID; + public delegate* unmanaged GetObjectField; + public delegate* unmanaged GetBooleanField; + public delegate* unmanaged GetByteField; + public delegate* unmanaged GetCharField; + public delegate* unmanaged GetShortField; + public delegate* unmanaged GetIntField; + public delegate* unmanaged GetLongField; + public delegate* unmanaged GetFloatField; + public delegate* unmanaged GetDoubleField; + public delegate* unmanaged SetObjectField; + public delegate* unmanaged SetBooleanField; + public delegate* unmanaged SetByteField; + public delegate* unmanaged SetCharField; + public delegate* unmanaged SetShortField; + public delegate* unmanaged SetIntField; + public delegate* unmanaged SetLongField; + public delegate* unmanaged SetFloatField; + public delegate* unmanaged SetDoubleField; + public delegate* unmanaged GetStaticMethodID; + public delegate* unmanaged CallStaticObjectMethod; + public IntPtr CallStaticObjectMethodV; // jobject (*CallStaticObjectMethodV)(JNIEnv*, jclass, jmethodID, va_list); + public delegate* unmanaged CallStaticObjectMethodA; + public delegate* unmanaged CallStaticBooleanMethod; + public IntPtr CallStaticBooleanMethodV; // jboolean (*CallStaticBooleanMethodV)(JNIEnv*, jclass, jmethodID, va_list); + public delegate* unmanaged CallStaticBooleanMethodA; + public delegate* unmanaged CallStaticByteMethod; + public IntPtr CallStaticByteMethodV; // jbyte (*CallStaticByteMethodV)(JNIEnv*, jclass, jmethodID, va_list); + public delegate* unmanaged CallStaticByteMethodA; + public delegate* unmanaged CallStaticCharMethod; + public IntPtr CallStaticCharMethodV; // jchar (*CallStaticCharMethodV)(JNIEnv*, jclass, jmethodID, va_list); + public delegate* unmanaged CallStaticCharMethodA; + public delegate* unmanaged CallStaticShortMethod; + public IntPtr CallStaticShortMethodV; // jshort (*CallStaticShortMethodV)(JNIEnv*, jclass, jmethodID, va_list); + public delegate* unmanaged CallStaticShortMethodA; + public delegate* unmanaged CallStaticIntMethod; + public IntPtr CallStaticIntMethodV; // jint (*CallStaticIntMethodV)(JNIEnv*, jclass, jmethodID, va_list); + public delegate* unmanaged CallStaticIntMethodA; + public delegate* unmanaged CallStaticLongMethod; + public IntPtr CallStaticLongMethodV; // jlong (*CallStaticLongMethodV)(JNIEnv*, jclass, jmethodID, va_list); + public delegate* unmanaged CallStaticLongMethodA; + public delegate* unmanaged CallStaticFloatMethod; + public IntPtr CallStaticFloatMethodV; // jfloat (*CallStaticFloatMethodV)(JNIEnv*, jclass, jmethodID, va_list); + public delegate* unmanaged CallStaticFloatMethodA; + public delegate* unmanaged CallStaticDoubleMethod; + public IntPtr CallStaticDoubleMethodV; // jdouble (*CallStaticDoubleMethodV)(JNIEnv*, jclass, jmethodID, va_list); + public delegate* unmanaged CallStaticDoubleMethodA; + public delegate* unmanaged CallStaticVoidMethod; + public IntPtr CallStaticVoidMethodV; // void (*CallStaticVoidMethodV)(JNIEnv*, jclass, jmethodID, va_list); + public delegate* unmanaged CallStaticVoidMethodA; + public delegate* unmanaged GetStaticFieldID; + public delegate* unmanaged GetStaticObjectField; + public delegate* unmanaged GetStaticBooleanField; + public delegate* unmanaged GetStaticByteField; + public delegate* unmanaged GetStaticCharField; + public delegate* unmanaged GetStaticShortField; + public delegate* unmanaged GetStaticIntField; + public delegate* unmanaged GetStaticLongField; + public delegate* unmanaged GetStaticFloatField; + public delegate* unmanaged GetStaticDoubleField; + public delegate* unmanaged SetStaticObjectField; + public delegate* unmanaged SetStaticBooleanField; + public delegate* unmanaged SetStaticByteField; + public delegate* unmanaged SetStaticCharField; + public delegate* unmanaged SetStaticShortField; + public delegate* unmanaged SetStaticIntField; + public delegate* unmanaged SetStaticLongField; + public delegate* unmanaged SetStaticFloatField; + public delegate* unmanaged SetStaticDoubleField; + public delegate* unmanaged NewString; + public delegate* unmanaged GetStringLength; + public delegate* unmanaged GetStringChars; + public delegate* unmanaged ReleaseStringChars; + public delegate* unmanaged NewStringUTF; + public delegate* unmanaged GetStringUTFLength; + public delegate* unmanaged GetStringUTFChars; + public delegate* unmanaged ReleaseStringUTFChars; + public delegate* unmanaged GetArrayLength; + public delegate* unmanaged NewObjectArray; + public delegate* unmanaged GetObjectArrayElement; + public delegate* unmanaged SetObjectArrayElement; + public delegate* unmanaged NewBooleanArray; + public delegate* unmanaged NewByteArray; + public delegate* unmanaged NewCharArray; + public delegate* unmanaged NewShortArray; + public delegate* unmanaged NewIntArray; + public delegate* unmanaged NewLongArray; + public delegate* unmanaged NewFloatArray; + public delegate* unmanaged NewDoubleArray; + public delegate* unmanaged GetBooleanArrayElements; + public delegate* unmanaged GetByteArrayElements; + public delegate* unmanaged GetCharArrayElements; + public delegate* unmanaged GetShortArrayElements; + public delegate* unmanaged GetIntArrayElements; + public delegate* unmanaged GetLongArrayElements; + public delegate* unmanaged GetFloatArrayElements; + public delegate* unmanaged GetDoubleArrayElements; + public delegate* unmanaged ReleaseBooleanArrayElements; + public delegate* unmanaged ReleaseByteArrayElements; + public delegate* unmanaged ReleaseCharArrayElements; + public delegate* unmanaged ReleaseShortArrayElements; + public delegate* unmanaged ReleaseIntArrayElements; + public delegate* unmanaged ReleaseLongArrayElements; + public delegate* unmanaged ReleaseFloatArrayElements; + public delegate* unmanaged ReleaseDoubleArrayElements; + public delegate* unmanaged GetBooleanArrayRegion; + public delegate* unmanaged GetByteArrayRegion; + public delegate* unmanaged GetCharArrayRegion; + public delegate* unmanaged GetShortArrayRegion; + public delegate* unmanaged GetIntArrayRegion; + public delegate* unmanaged GetLongArrayRegion; + public delegate* unmanaged GetFloatArrayRegion; + public delegate* unmanaged GetDoubleArrayRegion; + public delegate* unmanaged SetBooleanArrayRegion; + public delegate* unmanaged SetByteArrayRegion; + public delegate* unmanaged SetCharArrayRegion; + public delegate* unmanaged SetShortArrayRegion; + public delegate* unmanaged SetIntArrayRegion; + public delegate* unmanaged SetLongArrayRegion; + public delegate* unmanaged SetFloatArrayRegion; + public delegate* unmanaged SetDoubleArrayRegion; + public delegate* unmanaged RegisterNatives; + public delegate* unmanaged UnregisterNatives; + public delegate* unmanaged MonitorEnter; + public delegate* unmanaged MonitorExit; + public delegate* unmanaged GetJavaVM; + public delegate* unmanaged GetStringRegion; + public delegate* unmanaged GetStringUTFRegion; + public delegate* unmanaged GetPrimitiveArrayCritical; + public delegate* unmanaged ReleasePrimitiveArrayCritical; + public delegate* unmanaged GetStringCritical; + public delegate* unmanaged ReleaseStringCritical; + public delegate* unmanaged NewWeakGlobalRef; + public delegate* unmanaged DeleteWeakGlobalRef; + public delegate* unmanaged ExceptionCheck; + public delegate* unmanaged NewDirectByteBuffer; + public delegate* unmanaged GetDirectBufferAddress; + public delegate* unmanaged GetDirectBufferCapacity; + public delegate* unmanaged GetObjectRefType; + } +#endif // FEATURE_JNIENVIRONMENT_JI_FUNCTION_POINTERS + +#pragma warning restore 0169 +#pragma warning restore 0649 } #if FEATURE_JNIENVIRONMENT_SAFEHANDLES namespace @@ -17641,3 +17882,3006 @@ public JniFunc_JNIEnvPtr_jobject_JniObjectReferenceType GetObjectRefType { } } #endif // FEATURE_JNIENVIRONMENT_XA_INTPTRS +#if FEATURE_JNIENVIRONMENT_JI_FUNCTION_POINTERS +namespace +#if _NAMESPACE_PER_HANDLE + Java.Interop.JIFunctionPointers +#else + Java.Interop +#endif +{ + + partial class JniEnvironment { + + public static partial class Arrays { + + public static unsafe int GetArrayLength (JniObjectReference array) + { + if (!array.IsValid) + throw new ArgumentException ("Handle must be valid.", "array"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetArrayLength (__env, array.Handle); + return tmp; + } + + public static unsafe JniObjectReference NewObjectArray (int length, JniObjectReference elementClass, JniObjectReference initialElement) + { + if (!elementClass.IsValid) + throw new ArgumentException ("Handle must be valid.", "elementClass"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->NewObjectArray (__env, length, elementClass.Handle, initialElement.Handle); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + JniEnvironment.LogCreateLocalRef (tmp); + return new JniObjectReference (tmp, JniObjectReferenceType.Local); + } + + public static unsafe JniObjectReference GetObjectArrayElement (JniObjectReference array, int index) + { + if (!array.IsValid) + throw new ArgumentException ("Handle must be valid.", "array"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetObjectArrayElement (__env, array.Handle, index); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + JniEnvironment.LogCreateLocalRef (tmp); + return new JniObjectReference (tmp, JniObjectReferenceType.Local); + } + + public static unsafe void SetObjectArrayElement (JniObjectReference array, int index, JniObjectReference value) + { + if (!array.IsValid) + throw new ArgumentException ("Handle must be valid.", "array"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->SetObjectArrayElement (__env, array.Handle, index, value.Handle); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + } + + public static unsafe JniObjectReference NewBooleanArray (int length) + { + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->NewBooleanArray (__env, length); + JniEnvironment.LogCreateLocalRef (tmp); + return new JniObjectReference (tmp, JniObjectReferenceType.Local); + } + + public static unsafe JniObjectReference NewByteArray (int length) + { + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->NewByteArray (__env, length); + JniEnvironment.LogCreateLocalRef (tmp); + return new JniObjectReference (tmp, JniObjectReferenceType.Local); + } + + public static unsafe JniObjectReference NewCharArray (int length) + { + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->NewCharArray (__env, length); + JniEnvironment.LogCreateLocalRef (tmp); + return new JniObjectReference (tmp, JniObjectReferenceType.Local); + } + + public static unsafe JniObjectReference NewShortArray (int length) + { + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->NewShortArray (__env, length); + JniEnvironment.LogCreateLocalRef (tmp); + return new JniObjectReference (tmp, JniObjectReferenceType.Local); + } + + public static unsafe JniObjectReference NewIntArray (int length) + { + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->NewIntArray (__env, length); + JniEnvironment.LogCreateLocalRef (tmp); + return new JniObjectReference (tmp, JniObjectReferenceType.Local); + } + + public static unsafe JniObjectReference NewLongArray (int length) + { + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->NewLongArray (__env, length); + JniEnvironment.LogCreateLocalRef (tmp); + return new JniObjectReference (tmp, JniObjectReferenceType.Local); + } + + public static unsafe JniObjectReference NewFloatArray (int length) + { + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->NewFloatArray (__env, length); + JniEnvironment.LogCreateLocalRef (tmp); + return new JniObjectReference (tmp, JniObjectReferenceType.Local); + } + + public static unsafe JniObjectReference NewDoubleArray (int length) + { + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->NewDoubleArray (__env, length); + JniEnvironment.LogCreateLocalRef (tmp); + return new JniObjectReference (tmp, JniObjectReferenceType.Local); + } + + public static unsafe bool* GetBooleanArrayElements (JniObjectReference array, bool* isCopy) + { + if (!array.IsValid) + throw new ArgumentException ("Handle must be valid.", "array"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetBooleanArrayElements (__env, array.Handle, isCopy); + return tmp; + } + + public static unsafe sbyte* GetByteArrayElements (JniObjectReference array, bool* isCopy) + { + if (!array.IsValid) + throw new ArgumentException ("Handle must be valid.", "array"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetByteArrayElements (__env, array.Handle, isCopy); + return tmp; + } + + public static unsafe char* GetCharArrayElements (JniObjectReference array, bool* isCopy) + { + if (!array.IsValid) + throw new ArgumentException ("Handle must be valid.", "array"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetCharArrayElements (__env, array.Handle, isCopy); + return tmp; + } + + public static unsafe short* GetShortArrayElements (JniObjectReference array, bool* isCopy) + { + if (!array.IsValid) + throw new ArgumentException ("Handle must be valid.", "array"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetShortArrayElements (__env, array.Handle, isCopy); + return tmp; + } + + public static unsafe int* GetIntArrayElements (JniObjectReference array, bool* isCopy) + { + if (!array.IsValid) + throw new ArgumentException ("Handle must be valid.", "array"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetIntArrayElements (__env, array.Handle, isCopy); + return tmp; + } + + public static unsafe long* GetLongArrayElements (JniObjectReference array, bool* isCopy) + { + if (!array.IsValid) + throw new ArgumentException ("Handle must be valid.", "array"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetLongArrayElements (__env, array.Handle, isCopy); + return tmp; + } + + public static unsafe float* GetFloatArrayElements (JniObjectReference array, bool* isCopy) + { + if (!array.IsValid) + throw new ArgumentException ("Handle must be valid.", "array"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetFloatArrayElements (__env, array.Handle, isCopy); + return tmp; + } + + public static unsafe double* GetDoubleArrayElements (JniObjectReference array, bool* isCopy) + { + if (!array.IsValid) + throw new ArgumentException ("Handle must be valid.", "array"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetDoubleArrayElements (__env, array.Handle, isCopy); + return tmp; + } + + public static unsafe void ReleaseBooleanArrayElements (JniObjectReference array, bool* elements, JniReleaseArrayElementsMode mode) + { + if (!array.IsValid) + throw new ArgumentException ("Handle must be valid.", "array"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->ReleaseBooleanArrayElements (__env, array.Handle, elements, ((int) mode)); + } + + public static unsafe void ReleaseByteArrayElements (JniObjectReference array, sbyte* elements, JniReleaseArrayElementsMode mode) + { + if (!array.IsValid) + throw new ArgumentException ("Handle must be valid.", "array"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->ReleaseByteArrayElements (__env, array.Handle, elements, ((int) mode)); + } + + public static unsafe void ReleaseCharArrayElements (JniObjectReference array, char* elements, JniReleaseArrayElementsMode mode) + { + if (!array.IsValid) + throw new ArgumentException ("Handle must be valid.", "array"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->ReleaseCharArrayElements (__env, array.Handle, elements, ((int) mode)); + } + + public static unsafe void ReleaseShortArrayElements (JniObjectReference array, short* elements, JniReleaseArrayElementsMode mode) + { + if (!array.IsValid) + throw new ArgumentException ("Handle must be valid.", "array"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->ReleaseShortArrayElements (__env, array.Handle, elements, ((int) mode)); + } + + public static unsafe void ReleaseIntArrayElements (JniObjectReference array, int* elements, JniReleaseArrayElementsMode mode) + { + if (!array.IsValid) + throw new ArgumentException ("Handle must be valid.", "array"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->ReleaseIntArrayElements (__env, array.Handle, elements, ((int) mode)); + } + + public static unsafe void ReleaseLongArrayElements (JniObjectReference array, long* elements, JniReleaseArrayElementsMode mode) + { + if (!array.IsValid) + throw new ArgumentException ("Handle must be valid.", "array"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->ReleaseLongArrayElements (__env, array.Handle, elements, ((int) mode)); + } + + public static unsafe void ReleaseFloatArrayElements (JniObjectReference array, float* elements, JniReleaseArrayElementsMode mode) + { + if (!array.IsValid) + throw new ArgumentException ("Handle must be valid.", "array"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->ReleaseFloatArrayElements (__env, array.Handle, elements, ((int) mode)); + } + + public static unsafe void ReleaseDoubleArrayElements (JniObjectReference array, double* elements, JniReleaseArrayElementsMode mode) + { + if (!array.IsValid) + throw new ArgumentException ("Handle must be valid.", "array"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->ReleaseDoubleArrayElements (__env, array.Handle, elements, ((int) mode)); + } + + public static unsafe void GetBooleanArrayRegion (JniObjectReference array, int start, int length, bool* buffer) + { + if (!array.IsValid) + throw new ArgumentException ("Handle must be valid.", "array"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->GetBooleanArrayRegion (__env, array.Handle, start, length, buffer); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + } + + public static unsafe void GetByteArrayRegion (JniObjectReference array, int start, int length, sbyte* buffer) + { + if (!array.IsValid) + throw new ArgumentException ("Handle must be valid.", "array"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->GetByteArrayRegion (__env, array.Handle, start, length, buffer); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + } + + public static unsafe void GetCharArrayRegion (JniObjectReference array, int start, int length, char* buffer) + { + if (!array.IsValid) + throw new ArgumentException ("Handle must be valid.", "array"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->GetCharArrayRegion (__env, array.Handle, start, length, buffer); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + } + + public static unsafe void GetShortArrayRegion (JniObjectReference array, int start, int length, short* buffer) + { + if (!array.IsValid) + throw new ArgumentException ("Handle must be valid.", "array"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->GetShortArrayRegion (__env, array.Handle, start, length, buffer); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + } + + public static unsafe void GetIntArrayRegion (JniObjectReference array, int start, int length, int* buffer) + { + if (!array.IsValid) + throw new ArgumentException ("Handle must be valid.", "array"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->GetIntArrayRegion (__env, array.Handle, start, length, buffer); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + } + + public static unsafe void GetLongArrayRegion (JniObjectReference array, int start, int length, long* buffer) + { + if (!array.IsValid) + throw new ArgumentException ("Handle must be valid.", "array"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->GetLongArrayRegion (__env, array.Handle, start, length, buffer); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + } + + public static unsafe void GetFloatArrayRegion (JniObjectReference array, int start, int length, float* buffer) + { + if (!array.IsValid) + throw new ArgumentException ("Handle must be valid.", "array"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->GetFloatArrayRegion (__env, array.Handle, start, length, buffer); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + } + + public static unsafe void GetDoubleArrayRegion (JniObjectReference array, int start, int length, double* buffer) + { + if (!array.IsValid) + throw new ArgumentException ("Handle must be valid.", "array"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->GetDoubleArrayRegion (__env, array.Handle, start, length, buffer); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + } + + public static unsafe void SetBooleanArrayRegion (JniObjectReference array, int start, int length, bool* buffer) + { + if (!array.IsValid) + throw new ArgumentException ("Handle must be valid.", "array"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->SetBooleanArrayRegion (__env, array.Handle, start, length, buffer); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + } + + public static unsafe void SetByteArrayRegion (JniObjectReference array, int start, int length, sbyte* buffer) + { + if (!array.IsValid) + throw new ArgumentException ("Handle must be valid.", "array"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->SetByteArrayRegion (__env, array.Handle, start, length, buffer); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + } + + public static unsafe void SetCharArrayRegion (JniObjectReference array, int start, int length, char* buffer) + { + if (!array.IsValid) + throw new ArgumentException ("Handle must be valid.", "array"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->SetCharArrayRegion (__env, array.Handle, start, length, buffer); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + } + + public static unsafe void SetShortArrayRegion (JniObjectReference array, int start, int length, short* buffer) + { + if (!array.IsValid) + throw new ArgumentException ("Handle must be valid.", "array"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->SetShortArrayRegion (__env, array.Handle, start, length, buffer); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + } + + public static unsafe void SetIntArrayRegion (JniObjectReference array, int start, int length, int* buffer) + { + if (!array.IsValid) + throw new ArgumentException ("Handle must be valid.", "array"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->SetIntArrayRegion (__env, array.Handle, start, length, buffer); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + } + + public static unsafe void SetLongArrayRegion (JniObjectReference array, int start, int length, long* buffer) + { + if (!array.IsValid) + throw new ArgumentException ("Handle must be valid.", "array"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->SetLongArrayRegion (__env, array.Handle, start, length, buffer); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + } + + public static unsafe void SetFloatArrayRegion (JniObjectReference array, int start, int length, float* buffer) + { + if (!array.IsValid) + throw new ArgumentException ("Handle must be valid.", "array"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->SetFloatArrayRegion (__env, array.Handle, start, length, buffer); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + } + + public static unsafe void SetDoubleArrayRegion (JniObjectReference array, int start, int length, double* buffer) + { + if (!array.IsValid) + throw new ArgumentException ("Handle must be valid.", "array"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->SetDoubleArrayRegion (__env, array.Handle, start, length, buffer); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + } + + public static unsafe IntPtr GetPrimitiveArrayCritical (JniObjectReference array, bool* isCopy) + { + if (!array.IsValid) + throw new ArgumentException ("Handle must be valid.", "array"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetPrimitiveArrayCritical (__env, array.Handle, isCopy); + return tmp; + } + + public static unsafe void ReleasePrimitiveArrayCritical (JniObjectReference array, IntPtr carray, JniReleaseArrayElementsMode mode) + { + if (!array.IsValid) + throw new ArgumentException ("Handle must be valid.", "array"); + if (carray == IntPtr.Zero) + throw new ArgumentException ("'carray' must not be IntPtr.Zero.", "carray"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->ReleasePrimitiveArrayCritical (__env, array.Handle, carray, ((int) mode)); + } + } + + public static partial class Exceptions { + + internal static unsafe int _Throw (JniObjectReference toThrow) + { + if (!toThrow.IsValid) + throw new ArgumentException ("Handle must be valid.", "toThrow"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->Throw (__env, toThrow.Handle); + return tmp; + } + + internal static unsafe int _ThrowNew (JniObjectReference type, string message) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (message == null) + throw new ArgumentNullException ("message"); + + var _message_ptr = Marshal.StringToCoTaskMemUTF8 (message); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->ThrowNew (__env, type.Handle, _message_ptr); + Marshal.ZeroFreeCoTaskMemUTF8 (_message_ptr); + return tmp; + } + + public static unsafe JniObjectReference ExceptionOccurred () + { + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + return new JniObjectReference (tmp, JniObjectReferenceType.Local); + } + + public static unsafe void ExceptionDescribe () + { + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->ExceptionDescribe (__env); + } + + public static unsafe void ExceptionClear () + { + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->ExceptionClear (__env); + } + + public static unsafe void FatalError (string message) + { + if (message == null) + throw new ArgumentNullException ("message"); + + var _message_ptr = Marshal.StringToCoTaskMemUTF8 (message); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->FatalError (__env, _message_ptr); + Marshal.ZeroFreeCoTaskMemUTF8 (_message_ptr); + } + + public static unsafe bool ExceptionCheck () + { + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->ExceptionCheck (__env); + return (tmp != 0) ? true : false; + } + } + + public static partial class InstanceFields { + + public static unsafe JniFieldInfo GetFieldID (JniObjectReference type, string name, string signature) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (name == null) + throw new ArgumentNullException ("name"); + if (signature == null) + throw new ArgumentNullException ("signature"); + + var _name_ptr = Marshal.StringToCoTaskMemUTF8 (name); + var _signature_ptr = Marshal.StringToCoTaskMemUTF8 (signature); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetFieldID (__env, type.Handle, _name_ptr, _signature_ptr); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + Marshal.ZeroFreeCoTaskMemUTF8 (_name_ptr); + Marshal.ZeroFreeCoTaskMemUTF8 (_signature_ptr); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + if (tmp == IntPtr.Zero) + return null; + return new JniFieldInfo (name, signature, tmp, isStatic: false); + } + + public static unsafe JniObjectReference GetObjectField (JniObjectReference instance, JniFieldInfo field) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (field == null) + throw new ArgumentNullException ("field"); + if (!field.IsValid) + throw new ArgumentException ("Handle value is not valid.", "field"); + System.Diagnostics.Debug.Assert (!field.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetObjectField (__env, instance.Handle, field.ID); + JniEnvironment.LogCreateLocalRef (tmp); + return new JniObjectReference (tmp, JniObjectReferenceType.Local); + } + + public static unsafe bool GetBooleanField (JniObjectReference instance, JniFieldInfo field) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (field == null) + throw new ArgumentNullException ("field"); + if (!field.IsValid) + throw new ArgumentException ("Handle value is not valid.", "field"); + System.Diagnostics.Debug.Assert (!field.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetBooleanField (__env, instance.Handle, field.ID); + return (tmp != 0) ? true : false; + } + + public static unsafe sbyte GetByteField (JniObjectReference instance, JniFieldInfo field) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (field == null) + throw new ArgumentNullException ("field"); + if (!field.IsValid) + throw new ArgumentException ("Handle value is not valid.", "field"); + System.Diagnostics.Debug.Assert (!field.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetByteField (__env, instance.Handle, field.ID); + return tmp; + } + + public static unsafe char GetCharField (JniObjectReference instance, JniFieldInfo field) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (field == null) + throw new ArgumentNullException ("field"); + if (!field.IsValid) + throw new ArgumentException ("Handle value is not valid.", "field"); + System.Diagnostics.Debug.Assert (!field.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetCharField (__env, instance.Handle, field.ID); + return tmp; + } + + public static unsafe short GetShortField (JniObjectReference instance, JniFieldInfo field) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (field == null) + throw new ArgumentNullException ("field"); + if (!field.IsValid) + throw new ArgumentException ("Handle value is not valid.", "field"); + System.Diagnostics.Debug.Assert (!field.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetShortField (__env, instance.Handle, field.ID); + return tmp; + } + + public static unsafe int GetIntField (JniObjectReference instance, JniFieldInfo field) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (field == null) + throw new ArgumentNullException ("field"); + if (!field.IsValid) + throw new ArgumentException ("Handle value is not valid.", "field"); + System.Diagnostics.Debug.Assert (!field.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetIntField (__env, instance.Handle, field.ID); + return tmp; + } + + public static unsafe long GetLongField (JniObjectReference instance, JniFieldInfo field) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (field == null) + throw new ArgumentNullException ("field"); + if (!field.IsValid) + throw new ArgumentException ("Handle value is not valid.", "field"); + System.Diagnostics.Debug.Assert (!field.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetLongField (__env, instance.Handle, field.ID); + return tmp; + } + + public static unsafe float GetFloatField (JniObjectReference instance, JniFieldInfo field) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (field == null) + throw new ArgumentNullException ("field"); + if (!field.IsValid) + throw new ArgumentException ("Handle value is not valid.", "field"); + System.Diagnostics.Debug.Assert (!field.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetFloatField (__env, instance.Handle, field.ID); + return tmp; + } + + public static unsafe double GetDoubleField (JniObjectReference instance, JniFieldInfo field) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (field == null) + throw new ArgumentNullException ("field"); + if (!field.IsValid) + throw new ArgumentException ("Handle value is not valid.", "field"); + System.Diagnostics.Debug.Assert (!field.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetDoubleField (__env, instance.Handle, field.ID); + return tmp; + } + + public static unsafe void SetObjectField (JniObjectReference instance, JniFieldInfo field, JniObjectReference value) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (field == null) + throw new ArgumentNullException ("field"); + if (!field.IsValid) + throw new ArgumentException ("Handle value is not valid.", "field"); + System.Diagnostics.Debug.Assert (!field.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->SetObjectField (__env, instance.Handle, field.ID, value.Handle); + } + + public static unsafe void SetBooleanField (JniObjectReference instance, JniFieldInfo field, bool value) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (field == null) + throw new ArgumentNullException ("field"); + if (!field.IsValid) + throw new ArgumentException ("Handle value is not valid.", "field"); + System.Diagnostics.Debug.Assert (!field.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->SetBooleanField (__env, instance.Handle, field.ID, (value ? (byte) 1 : (byte) 0)); + } + + public static unsafe void SetByteField (JniObjectReference instance, JniFieldInfo field, sbyte value) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (field == null) + throw new ArgumentNullException ("field"); + if (!field.IsValid) + throw new ArgumentException ("Handle value is not valid.", "field"); + System.Diagnostics.Debug.Assert (!field.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->SetByteField (__env, instance.Handle, field.ID, value); + } + + public static unsafe void SetCharField (JniObjectReference instance, JniFieldInfo field, char value) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (field == null) + throw new ArgumentNullException ("field"); + if (!field.IsValid) + throw new ArgumentException ("Handle value is not valid.", "field"); + System.Diagnostics.Debug.Assert (!field.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->SetCharField (__env, instance.Handle, field.ID, value); + } + + public static unsafe void SetShortField (JniObjectReference instance, JniFieldInfo field, short value) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (field == null) + throw new ArgumentNullException ("field"); + if (!field.IsValid) + throw new ArgumentException ("Handle value is not valid.", "field"); + System.Diagnostics.Debug.Assert (!field.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->SetShortField (__env, instance.Handle, field.ID, value); + } + + public static unsafe void SetIntField (JniObjectReference instance, JniFieldInfo field, int value) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (field == null) + throw new ArgumentNullException ("field"); + if (!field.IsValid) + throw new ArgumentException ("Handle value is not valid.", "field"); + System.Diagnostics.Debug.Assert (!field.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->SetIntField (__env, instance.Handle, field.ID, value); + } + + public static unsafe void SetLongField (JniObjectReference instance, JniFieldInfo field, long value) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (field == null) + throw new ArgumentNullException ("field"); + if (!field.IsValid) + throw new ArgumentException ("Handle value is not valid.", "field"); + System.Diagnostics.Debug.Assert (!field.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->SetLongField (__env, instance.Handle, field.ID, value); + } + + public static unsafe void SetFloatField (JniObjectReference instance, JniFieldInfo field, float value) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (field == null) + throw new ArgumentNullException ("field"); + if (!field.IsValid) + throw new ArgumentException ("Handle value is not valid.", "field"); + System.Diagnostics.Debug.Assert (!field.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->SetFloatField (__env, instance.Handle, field.ID, value); + } + + public static unsafe void SetDoubleField (JniObjectReference instance, JniFieldInfo field, double value) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (field == null) + throw new ArgumentNullException ("field"); + if (!field.IsValid) + throw new ArgumentException ("Handle value is not valid.", "field"); + System.Diagnostics.Debug.Assert (!field.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->SetDoubleField (__env, instance.Handle, field.ID, value); + } + } + + public static partial class InstanceMethods { + + public static unsafe JniMethodInfo GetMethodID (JniObjectReference type, string name, string signature) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (name == null) + throw new ArgumentNullException ("name"); + if (signature == null) + throw new ArgumentNullException ("signature"); + + var _name_ptr = Marshal.StringToCoTaskMemUTF8 (name); + var _signature_ptr = Marshal.StringToCoTaskMemUTF8 (signature); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetMethodID (__env, type.Handle, _name_ptr, _signature_ptr); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + Marshal.ZeroFreeCoTaskMemUTF8 (_name_ptr); + Marshal.ZeroFreeCoTaskMemUTF8 (_signature_ptr); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + if (tmp == IntPtr.Zero) + return null; + return new JniMethodInfo (name, signature, tmp, isStatic: false); + } + + public static unsafe JniObjectReference CallObjectMethod (JniObjectReference instance, JniMethodInfo method) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallObjectMethod (__env, instance.Handle, method.ID); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + JniEnvironment.LogCreateLocalRef (tmp); + return new JniObjectReference (tmp, JniObjectReferenceType.Local); + } + + public static unsafe JniObjectReference CallObjectMethod (JniObjectReference instance, JniMethodInfo method, JniArgumentValue* args) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallObjectMethodA (__env, instance.Handle, method.ID, (IntPtr) args); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + JniEnvironment.LogCreateLocalRef (tmp); + return new JniObjectReference (tmp, JniObjectReferenceType.Local); + } + + public static unsafe bool CallBooleanMethod (JniObjectReference instance, JniMethodInfo method) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallBooleanMethod (__env, instance.Handle, method.ID); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return (tmp != 0) ? true : false; + } + + public static unsafe bool CallBooleanMethod (JniObjectReference instance, JniMethodInfo method, JniArgumentValue* args) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallBooleanMethodA (__env, instance.Handle, method.ID, (IntPtr) args); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return (tmp != 0) ? true : false; + } + + public static unsafe sbyte CallByteMethod (JniObjectReference instance, JniMethodInfo method) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallByteMethod (__env, instance.Handle, method.ID); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe sbyte CallByteMethod (JniObjectReference instance, JniMethodInfo method, JniArgumentValue* args) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallByteMethodA (__env, instance.Handle, method.ID, (IntPtr) args); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe char CallCharMethod (JniObjectReference instance, JniMethodInfo method) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallCharMethod (__env, instance.Handle, method.ID); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe char CallCharMethod (JniObjectReference instance, JniMethodInfo method, JniArgumentValue* args) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallCharMethodA (__env, instance.Handle, method.ID, (IntPtr) args); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe short CallShortMethod (JniObjectReference instance, JniMethodInfo method) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallShortMethod (__env, instance.Handle, method.ID); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe short CallShortMethod (JniObjectReference instance, JniMethodInfo method, JniArgumentValue* args) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallShortMethodA (__env, instance.Handle, method.ID, (IntPtr) args); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe int CallIntMethod (JniObjectReference instance, JniMethodInfo method) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallIntMethod (__env, instance.Handle, method.ID); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe int CallIntMethod (JniObjectReference instance, JniMethodInfo method, JniArgumentValue* args) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallIntMethodA (__env, instance.Handle, method.ID, (IntPtr) args); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe long CallLongMethod (JniObjectReference instance, JniMethodInfo method) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallLongMethod (__env, instance.Handle, method.ID); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe long CallLongMethod (JniObjectReference instance, JniMethodInfo method, JniArgumentValue* args) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallLongMethodA (__env, instance.Handle, method.ID, (IntPtr) args); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe float CallFloatMethod (JniObjectReference instance, JniMethodInfo method) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallFloatMethod (__env, instance.Handle, method.ID); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe float CallFloatMethod (JniObjectReference instance, JniMethodInfo method, JniArgumentValue* args) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallFloatMethodA (__env, instance.Handle, method.ID, (IntPtr) args); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe double CallDoubleMethod (JniObjectReference instance, JniMethodInfo method) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallDoubleMethod (__env, instance.Handle, method.ID); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe double CallDoubleMethod (JniObjectReference instance, JniMethodInfo method, JniArgumentValue* args) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallDoubleMethodA (__env, instance.Handle, method.ID, (IntPtr) args); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe void CallVoidMethod (JniObjectReference instance, JniMethodInfo method) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->CallVoidMethod (__env, instance.Handle, method.ID); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + } + + public static unsafe void CallVoidMethod (JniObjectReference instance, JniMethodInfo method, JniArgumentValue* args) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->CallVoidMethodA (__env, instance.Handle, method.ID, (IntPtr) args); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + } + + public static unsafe JniObjectReference CallNonvirtualObjectMethod (JniObjectReference instance, JniObjectReference type, JniMethodInfo method) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallNonvirtualObjectMethod (__env, instance.Handle, type.Handle, method.ID); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + JniEnvironment.LogCreateLocalRef (tmp); + return new JniObjectReference (tmp, JniObjectReferenceType.Local); + } + + public static unsafe JniObjectReference CallNonvirtualObjectMethod (JniObjectReference instance, JniObjectReference type, JniMethodInfo method, JniArgumentValue* args) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallNonvirtualObjectMethodA (__env, instance.Handle, type.Handle, method.ID, (IntPtr) args); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + JniEnvironment.LogCreateLocalRef (tmp); + return new JniObjectReference (tmp, JniObjectReferenceType.Local); + } + + public static unsafe bool CallNonvirtualBooleanMethod (JniObjectReference instance, JniObjectReference type, JniMethodInfo method) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallNonvirtualBooleanMethod (__env, instance.Handle, type.Handle, method.ID); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return (tmp != 0) ? true : false; + } + + public static unsafe bool CallNonvirtualBooleanMethod (JniObjectReference instance, JniObjectReference type, JniMethodInfo method, JniArgumentValue* args) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallNonvirtualBooleanMethodA (__env, instance.Handle, type.Handle, method.ID, (IntPtr) args); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return (tmp != 0) ? true : false; + } + + public static unsafe sbyte CallNonvirtualByteMethod (JniObjectReference instance, JniObjectReference type, JniMethodInfo method) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallNonvirtualByteMethod (__env, instance.Handle, type.Handle, method.ID); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe sbyte CallNonvirtualByteMethod (JniObjectReference instance, JniObjectReference type, JniMethodInfo method, JniArgumentValue* args) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallNonvirtualByteMethodA (__env, instance.Handle, type.Handle, method.ID, (IntPtr) args); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe char CallNonvirtualCharMethod (JniObjectReference instance, JniObjectReference type, JniMethodInfo method) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallNonvirtualCharMethod (__env, instance.Handle, type.Handle, method.ID); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe char CallNonvirtualCharMethod (JniObjectReference instance, JniObjectReference type, JniMethodInfo method, JniArgumentValue* args) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallNonvirtualCharMethodA (__env, instance.Handle, type.Handle, method.ID, (IntPtr) args); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe short CallNonvirtualShortMethod (JniObjectReference instance, JniObjectReference type, JniMethodInfo method) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallNonvirtualShortMethod (__env, instance.Handle, type.Handle, method.ID); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe short CallNonvirtualShortMethod (JniObjectReference instance, JniObjectReference type, JniMethodInfo method, JniArgumentValue* args) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallNonvirtualShortMethodA (__env, instance.Handle, type.Handle, method.ID, (IntPtr) args); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe int CallNonvirtualIntMethod (JniObjectReference instance, JniObjectReference type, JniMethodInfo method) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallNonvirtualIntMethod (__env, instance.Handle, type.Handle, method.ID); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe int CallNonvirtualIntMethod (JniObjectReference instance, JniObjectReference type, JniMethodInfo method, JniArgumentValue* args) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallNonvirtualIntMethodA (__env, instance.Handle, type.Handle, method.ID, (IntPtr) args); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe long CallNonvirtualLongMethod (JniObjectReference instance, JniObjectReference type, JniMethodInfo method) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallNonvirtualLongMethod (__env, instance.Handle, type.Handle, method.ID); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe long CallNonvirtualLongMethod (JniObjectReference instance, JniObjectReference type, JniMethodInfo method, JniArgumentValue* args) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallNonvirtualLongMethodA (__env, instance.Handle, type.Handle, method.ID, (IntPtr) args); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe float CallNonvirtualFloatMethod (JniObjectReference instance, JniObjectReference type, JniMethodInfo method) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallNonvirtualFloatMethod (__env, instance.Handle, type.Handle, method.ID); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe float CallNonvirtualFloatMethod (JniObjectReference instance, JniObjectReference type, JniMethodInfo method, JniArgumentValue* args) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallNonvirtualFloatMethodA (__env, instance.Handle, type.Handle, method.ID, (IntPtr) args); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe double CallNonvirtualDoubleMethod (JniObjectReference instance, JniObjectReference type, JniMethodInfo method) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallNonvirtualDoubleMethod (__env, instance.Handle, type.Handle, method.ID); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe double CallNonvirtualDoubleMethod (JniObjectReference instance, JniObjectReference type, JniMethodInfo method, JniArgumentValue* args) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallNonvirtualDoubleMethodA (__env, instance.Handle, type.Handle, method.ID, (IntPtr) args); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe void CallNonvirtualVoidMethod (JniObjectReference instance, JniObjectReference type, JniMethodInfo method) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->CallNonvirtualVoidMethod (__env, instance.Handle, type.Handle, method.ID); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + } + + public static unsafe void CallNonvirtualVoidMethod (JniObjectReference instance, JniObjectReference type, JniMethodInfo method, JniArgumentValue* args) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->CallNonvirtualVoidMethodA (__env, instance.Handle, type.Handle, method.ID, (IntPtr) args); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + } + } + + public static partial class IO { + + public static unsafe JniObjectReference NewDirectByteBuffer (IntPtr address, long capacity) + { + if (address == IntPtr.Zero) + throw new ArgumentException ("'address' must not be IntPtr.Zero.", "address"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->NewDirectByteBuffer (__env, address, capacity); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + JniEnvironment.LogCreateLocalRef (tmp); + return new JniObjectReference (tmp, JniObjectReferenceType.Local); + } + + public static unsafe IntPtr GetDirectBufferAddress (JniObjectReference buffer) + { + if (!buffer.IsValid) + throw new ArgumentException ("Handle must be valid.", "buffer"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetDirectBufferAddress (__env, buffer.Handle); + return tmp; + } + + public static unsafe long GetDirectBufferCapacity (JniObjectReference buffer) + { + if (!buffer.IsValid) + throw new ArgumentException ("Handle must be valid.", "buffer"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetDirectBufferCapacity (__env, buffer.Handle); + return tmp; + } + } + + public static partial class Monitors { + + internal static unsafe int _MonitorEnter (JniObjectReference instance) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->MonitorEnter (__env, instance.Handle); + return tmp; + } + + internal static unsafe int _MonitorExit (JniObjectReference instance) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->MonitorExit (__env, instance.Handle); + return tmp; + } + } + + public static partial class Object { + + public static unsafe JniObjectReference AllocObject (JniObjectReference type) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->AllocObject (__env, type.Handle); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + JniEnvironment.LogCreateLocalRef (tmp); + return new JniObjectReference (tmp, JniObjectReferenceType.Local); + } + + internal static unsafe JniObjectReference _NewObject (JniObjectReference type, JniMethodInfo method) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->NewObject (__env, type.Handle, method.ID); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + JniEnvironment.LogCreateLocalRef (tmp); + return new JniObjectReference (tmp, JniObjectReferenceType.Local); + } + + internal static unsafe JniObjectReference _NewObject (JniObjectReference type, JniMethodInfo method, JniArgumentValue* args) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->NewObjectA (__env, type.Handle, method.ID, (IntPtr) args); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + JniEnvironment.LogCreateLocalRef (tmp); + return new JniObjectReference (tmp, JniObjectReferenceType.Local); + } + } + + public static partial class References { + + internal static unsafe int _PushLocalFrame (int capacity) + { + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->PushLocalFrame (__env, capacity); + return tmp; + } + + public static unsafe JniObjectReference PopLocalFrame (JniObjectReference result) + { + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->PopLocalFrame (__env, result.Handle); + JniEnvironment.LogCreateLocalRef (tmp); + return new JniObjectReference (tmp, JniObjectReferenceType.Local); + } + + internal static unsafe JniObjectReference NewGlobalRef (JniObjectReference instance) + { + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->NewGlobalRef (__env, instance.Handle); + return new JniObjectReference (tmp, JniObjectReferenceType.Global); + } + + internal static unsafe void DeleteGlobalRef (IntPtr instance) + { + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->DeleteGlobalRef (__env, instance); + } + + internal static unsafe void DeleteLocalRef (IntPtr instance) + { + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->DeleteLocalRef (__env, instance); + } + + internal static unsafe JniObjectReference NewLocalRef (JniObjectReference instance) + { + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->NewLocalRef (__env, instance.Handle); + return new JniObjectReference (tmp, JniObjectReferenceType.Local); + } + + internal static unsafe int _EnsureLocalCapacity (int capacity) + { + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->EnsureLocalCapacity (__env, capacity); + return tmp; + } + + internal static unsafe int _GetJavaVM (out IntPtr vm) + { + IntPtr _vm_ptr = IntPtr.Zero; + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetJavaVM (__env, &_vm_ptr); + vm = _vm_ptr; + return tmp; + } + + internal static unsafe JniObjectReference NewWeakGlobalRef (JniObjectReference instance) + { + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->NewWeakGlobalRef (__env, instance.Handle); + return new JniObjectReference (tmp, JniObjectReferenceType.WeakGlobal); + } + + internal static unsafe void DeleteWeakGlobalRef (IntPtr instance) + { + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->DeleteWeakGlobalRef (__env, instance); + } + + internal static unsafe JniObjectReferenceType GetObjectRefType (JniObjectReference instance) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetObjectRefType (__env, instance.Handle); + return tmp; + } + } + + internal static partial class Reflection { + + public static unsafe JniObjectReference ToReflectedMethod (JniObjectReference type, JniMethodInfo method, bool isStatic) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (!method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->ToReflectedMethod (__env, type.Handle, method.ID, (isStatic ? (byte) 1 : (byte) 0)); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + JniEnvironment.LogCreateLocalRef (tmp); + return new JniObjectReference (tmp, JniObjectReferenceType.Local); + } + + public static unsafe JniObjectReference ToReflectedField (JniObjectReference type, JniFieldInfo field, bool isStatic) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (field == null) + throw new ArgumentNullException ("field"); + if (!field.IsValid) + throw new ArgumentException ("Handle value is not valid.", "field"); + System.Diagnostics.Debug.Assert (!field.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->ToReflectedField (__env, type.Handle, field.ID, (isStatic ? (byte) 1 : (byte) 0)); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + JniEnvironment.LogCreateLocalRef (tmp); + return new JniObjectReference (tmp, JniObjectReferenceType.Local); + } + } + + public static partial class StaticFields { + + public static unsafe JniFieldInfo GetStaticFieldID (JniObjectReference type, string name, string signature) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (name == null) + throw new ArgumentNullException ("name"); + if (signature == null) + throw new ArgumentNullException ("signature"); + + var _name_ptr = Marshal.StringToCoTaskMemUTF8 (name); + var _signature_ptr = Marshal.StringToCoTaskMemUTF8 (signature); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetStaticFieldID (__env, type.Handle, _name_ptr, _signature_ptr); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + Marshal.ZeroFreeCoTaskMemUTF8 (_name_ptr); + Marshal.ZeroFreeCoTaskMemUTF8 (_signature_ptr); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + if (tmp == IntPtr.Zero) + return null; + return new JniFieldInfo (name, signature, tmp, isStatic: true); + } + + public static unsafe JniObjectReference GetStaticObjectField (JniObjectReference type, JniFieldInfo field) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (field == null) + throw new ArgumentNullException ("field"); + if (!field.IsValid) + throw new ArgumentException ("Handle value is not valid.", "field"); + System.Diagnostics.Debug.Assert (field.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetStaticObjectField (__env, type.Handle, field.ID); + JniEnvironment.LogCreateLocalRef (tmp); + return new JniObjectReference (tmp, JniObjectReferenceType.Local); + } + + public static unsafe bool GetStaticBooleanField (JniObjectReference type, JniFieldInfo field) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (field == null) + throw new ArgumentNullException ("field"); + if (!field.IsValid) + throw new ArgumentException ("Handle value is not valid.", "field"); + System.Diagnostics.Debug.Assert (field.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetStaticBooleanField (__env, type.Handle, field.ID); + return (tmp != 0) ? true : false; + } + + public static unsafe sbyte GetStaticByteField (JniObjectReference type, JniFieldInfo field) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (field == null) + throw new ArgumentNullException ("field"); + if (!field.IsValid) + throw new ArgumentException ("Handle value is not valid.", "field"); + System.Diagnostics.Debug.Assert (field.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetStaticByteField (__env, type.Handle, field.ID); + return tmp; + } + + public static unsafe char GetStaticCharField (JniObjectReference type, JniFieldInfo field) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (field == null) + throw new ArgumentNullException ("field"); + if (!field.IsValid) + throw new ArgumentException ("Handle value is not valid.", "field"); + System.Diagnostics.Debug.Assert (field.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetStaticCharField (__env, type.Handle, field.ID); + return tmp; + } + + public static unsafe short GetStaticShortField (JniObjectReference type, JniFieldInfo field) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (field == null) + throw new ArgumentNullException ("field"); + if (!field.IsValid) + throw new ArgumentException ("Handle value is not valid.", "field"); + System.Diagnostics.Debug.Assert (field.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetStaticShortField (__env, type.Handle, field.ID); + return tmp; + } + + public static unsafe int GetStaticIntField (JniObjectReference type, JniFieldInfo field) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (field == null) + throw new ArgumentNullException ("field"); + if (!field.IsValid) + throw new ArgumentException ("Handle value is not valid.", "field"); + System.Diagnostics.Debug.Assert (field.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetStaticIntField (__env, type.Handle, field.ID); + return tmp; + } + + public static unsafe long GetStaticLongField (JniObjectReference type, JniFieldInfo field) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (field == null) + throw new ArgumentNullException ("field"); + if (!field.IsValid) + throw new ArgumentException ("Handle value is not valid.", "field"); + System.Diagnostics.Debug.Assert (field.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetStaticLongField (__env, type.Handle, field.ID); + return tmp; + } + + public static unsafe float GetStaticFloatField (JniObjectReference type, JniFieldInfo field) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (field == null) + throw new ArgumentNullException ("field"); + if (!field.IsValid) + throw new ArgumentException ("Handle value is not valid.", "field"); + System.Diagnostics.Debug.Assert (field.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetStaticFloatField (__env, type.Handle, field.ID); + return tmp; + } + + public static unsafe double GetStaticDoubleField (JniObjectReference type, JniFieldInfo field) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (field == null) + throw new ArgumentNullException ("field"); + if (!field.IsValid) + throw new ArgumentException ("Handle value is not valid.", "field"); + System.Diagnostics.Debug.Assert (field.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetStaticDoubleField (__env, type.Handle, field.ID); + return tmp; + } + + public static unsafe void SetStaticObjectField (JniObjectReference type, JniFieldInfo field, JniObjectReference value) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (field == null) + throw new ArgumentNullException ("field"); + if (!field.IsValid) + throw new ArgumentException ("Handle value is not valid.", "field"); + System.Diagnostics.Debug.Assert (field.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->SetStaticObjectField (__env, type.Handle, field.ID, value.Handle); + } + + public static unsafe void SetStaticBooleanField (JniObjectReference type, JniFieldInfo field, bool value) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (field == null) + throw new ArgumentNullException ("field"); + if (!field.IsValid) + throw new ArgumentException ("Handle value is not valid.", "field"); + System.Diagnostics.Debug.Assert (field.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->SetStaticBooleanField (__env, type.Handle, field.ID, (value ? (byte) 1 : (byte) 0)); + } + + public static unsafe void SetStaticByteField (JniObjectReference type, JniFieldInfo field, sbyte value) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (field == null) + throw new ArgumentNullException ("field"); + if (!field.IsValid) + throw new ArgumentException ("Handle value is not valid.", "field"); + System.Diagnostics.Debug.Assert (field.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->SetStaticByteField (__env, type.Handle, field.ID, value); + } + + public static unsafe void SetStaticCharField (JniObjectReference type, JniFieldInfo field, char value) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (field == null) + throw new ArgumentNullException ("field"); + if (!field.IsValid) + throw new ArgumentException ("Handle value is not valid.", "field"); + System.Diagnostics.Debug.Assert (field.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->SetStaticCharField (__env, type.Handle, field.ID, value); + } + + public static unsafe void SetStaticShortField (JniObjectReference type, JniFieldInfo field, short value) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (field == null) + throw new ArgumentNullException ("field"); + if (!field.IsValid) + throw new ArgumentException ("Handle value is not valid.", "field"); + System.Diagnostics.Debug.Assert (field.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->SetStaticShortField (__env, type.Handle, field.ID, value); + } + + public static unsafe void SetStaticIntField (JniObjectReference type, JniFieldInfo field, int value) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (field == null) + throw new ArgumentNullException ("field"); + if (!field.IsValid) + throw new ArgumentException ("Handle value is not valid.", "field"); + System.Diagnostics.Debug.Assert (field.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->SetStaticIntField (__env, type.Handle, field.ID, value); + } + + public static unsafe void SetStaticLongField (JniObjectReference type, JniFieldInfo field, long value) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (field == null) + throw new ArgumentNullException ("field"); + if (!field.IsValid) + throw new ArgumentException ("Handle value is not valid.", "field"); + System.Diagnostics.Debug.Assert (field.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->SetStaticLongField (__env, type.Handle, field.ID, value); + } + + public static unsafe void SetStaticFloatField (JniObjectReference type, JniFieldInfo field, float value) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (field == null) + throw new ArgumentNullException ("field"); + if (!field.IsValid) + throw new ArgumentException ("Handle value is not valid.", "field"); + System.Diagnostics.Debug.Assert (field.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->SetStaticFloatField (__env, type.Handle, field.ID, value); + } + + public static unsafe void SetStaticDoubleField (JniObjectReference type, JniFieldInfo field, double value) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (field == null) + throw new ArgumentNullException ("field"); + if (!field.IsValid) + throw new ArgumentException ("Handle value is not valid.", "field"); + System.Diagnostics.Debug.Assert (field.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->SetStaticDoubleField (__env, type.Handle, field.ID, value); + } + } + + public static partial class StaticMethods { + + public static unsafe JniMethodInfo GetStaticMethodID (JniObjectReference type, string name, string signature) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (name == null) + throw new ArgumentNullException ("name"); + if (signature == null) + throw new ArgumentNullException ("signature"); + + var _name_ptr = Marshal.StringToCoTaskMemUTF8 (name); + var _signature_ptr = Marshal.StringToCoTaskMemUTF8 (signature); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetStaticMethodID (__env, type.Handle, _name_ptr, _signature_ptr); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + Marshal.ZeroFreeCoTaskMemUTF8 (_name_ptr); + Marshal.ZeroFreeCoTaskMemUTF8 (_signature_ptr); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + if (tmp == IntPtr.Zero) + return null; + return new JniMethodInfo (name, signature, tmp, isStatic: true); + } + + public static unsafe JniObjectReference CallStaticObjectMethod (JniObjectReference type, JniMethodInfo method) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallStaticObjectMethod (__env, type.Handle, method.ID); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + JniEnvironment.LogCreateLocalRef (tmp); + return new JniObjectReference (tmp, JniObjectReferenceType.Local); + } + + public static unsafe JniObjectReference CallStaticObjectMethod (JniObjectReference type, JniMethodInfo method, JniArgumentValue* args) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallStaticObjectMethodA (__env, type.Handle, method.ID, (IntPtr) args); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + JniEnvironment.LogCreateLocalRef (tmp); + return new JniObjectReference (tmp, JniObjectReferenceType.Local); + } + + public static unsafe bool CallStaticBooleanMethod (JniObjectReference type, JniMethodInfo method) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallStaticBooleanMethod (__env, type.Handle, method.ID); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return (tmp != 0) ? true : false; + } + + public static unsafe bool CallStaticBooleanMethod (JniObjectReference type, JniMethodInfo method, JniArgumentValue* args) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallStaticBooleanMethodA (__env, type.Handle, method.ID, (IntPtr) args); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return (tmp != 0) ? true : false; + } + + public static unsafe sbyte CallStaticByteMethod (JniObjectReference type, JniMethodInfo method) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallStaticByteMethod (__env, type.Handle, method.ID); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe sbyte CallStaticByteMethod (JniObjectReference type, JniMethodInfo method, JniArgumentValue* args) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallStaticByteMethodA (__env, type.Handle, method.ID, (IntPtr) args); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe char CallStaticCharMethod (JniObjectReference type, JniMethodInfo method) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallStaticCharMethod (__env, type.Handle, method.ID); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe char CallStaticCharMethod (JniObjectReference type, JniMethodInfo method, JniArgumentValue* args) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallStaticCharMethodA (__env, type.Handle, method.ID, (IntPtr) args); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe short CallStaticShortMethod (JniObjectReference type, JniMethodInfo method) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallStaticShortMethod (__env, type.Handle, method.ID); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe short CallStaticShortMethod (JniObjectReference type, JniMethodInfo method, JniArgumentValue* args) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallStaticShortMethodA (__env, type.Handle, method.ID, (IntPtr) args); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe int CallStaticIntMethod (JniObjectReference type, JniMethodInfo method) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallStaticIntMethod (__env, type.Handle, method.ID); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe int CallStaticIntMethod (JniObjectReference type, JniMethodInfo method, JniArgumentValue* args) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallStaticIntMethodA (__env, type.Handle, method.ID, (IntPtr) args); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe long CallStaticLongMethod (JniObjectReference type, JniMethodInfo method) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallStaticLongMethod (__env, type.Handle, method.ID); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe long CallStaticLongMethod (JniObjectReference type, JniMethodInfo method, JniArgumentValue* args) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallStaticLongMethodA (__env, type.Handle, method.ID, (IntPtr) args); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe float CallStaticFloatMethod (JniObjectReference type, JniMethodInfo method) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallStaticFloatMethod (__env, type.Handle, method.ID); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe float CallStaticFloatMethod (JniObjectReference type, JniMethodInfo method, JniArgumentValue* args) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallStaticFloatMethodA (__env, type.Handle, method.ID, (IntPtr) args); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe double CallStaticDoubleMethod (JniObjectReference type, JniMethodInfo method) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallStaticDoubleMethod (__env, type.Handle, method.ID); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe double CallStaticDoubleMethod (JniObjectReference type, JniMethodInfo method, JniArgumentValue* args) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->CallStaticDoubleMethodA (__env, type.Handle, method.ID, (IntPtr) args); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + public static unsafe void CallStaticVoidMethod (JniObjectReference type, JniMethodInfo method) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->CallStaticVoidMethod (__env, type.Handle, method.ID); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + } + + public static unsafe void CallStaticVoidMethod (JniObjectReference type, JniMethodInfo method, JniArgumentValue* args) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + if (method == null) + throw new ArgumentNullException ("method"); + if (!method.IsValid) + throw new ArgumentException ("Handle value is not valid.", "method"); + System.Diagnostics.Debug.Assert (method.IsStatic); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->CallStaticVoidMethodA (__env, type.Handle, method.ID, (IntPtr) args); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + } + } + + public static partial class Strings { + + public static unsafe JniObjectReference NewString (char* unicodeChars, int length) + { + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->NewString (__env, unicodeChars, length); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + JniEnvironment.LogCreateLocalRef (tmp); + return new JniObjectReference (tmp, JniObjectReferenceType.Local); + } + + public static unsafe int GetStringLength (JniObjectReference stringInstance) + { + if (!stringInstance.IsValid) + throw new ArgumentException ("Handle must be valid.", "stringInstance"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetStringLength (__env, stringInstance.Handle); + return tmp; + } + + public static unsafe char* GetStringChars (JniObjectReference stringInstance, bool* isCopy) + { + if (!stringInstance.IsValid) + throw new ArgumentException ("Handle must be valid.", "stringInstance"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetStringChars (__env, stringInstance.Handle, isCopy); + return tmp; + } + + public static unsafe void ReleaseStringChars (JniObjectReference stringInstance, char* chars) + { + if (!stringInstance.IsValid) + throw new ArgumentException ("Handle must be valid.", "stringInstance"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + (*((JNIEnv**)__env))->ReleaseStringChars (__env, stringInstance.Handle, chars); + } + } + + public static partial class Types { + + public static unsafe JniObjectReference DefineClass (string name, JniObjectReference loader, IntPtr buffer, int bufferLength) + { + if (name == null) + throw new ArgumentNullException ("name"); + if (!loader.IsValid) + throw new ArgumentException ("Handle must be valid.", "loader"); + if (buffer == IntPtr.Zero) + throw new ArgumentException ("'buffer' must not be IntPtr.Zero.", "buffer"); + + var _name_ptr = Marshal.StringToCoTaskMemUTF8 (name); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->DefineClass (__env, _name_ptr, loader.Handle, buffer, bufferLength); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + Marshal.ZeroFreeCoTaskMemUTF8 (_name_ptr); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + JniEnvironment.LogCreateLocalRef (tmp); + return new JniObjectReference (tmp, JniObjectReferenceType.Local); + } + + internal static unsafe JniObjectReference _FindClass (string classname) + { + if (classname == null) + throw new ArgumentNullException ("classname"); + + var _classname_ptr = Marshal.StringToCoTaskMemUTF8 (classname); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->FindClass (__env, _classname_ptr); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + Marshal.ZeroFreeCoTaskMemUTF8 (_classname_ptr); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + JniEnvironment.LogCreateLocalRef (tmp); + return new JniObjectReference (tmp, JniObjectReferenceType.Local); + } + + public static unsafe JniObjectReference GetSuperclass (JniObjectReference type) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetSuperclass (__env, type.Handle); + JniEnvironment.LogCreateLocalRef (tmp); + return new JniObjectReference (tmp, JniObjectReferenceType.Local); + } + + public static unsafe bool IsAssignableFrom (JniObjectReference class1, JniObjectReference class2) + { + if (!class1.IsValid) + throw new ArgumentException ("Handle must be valid.", "class1"); + if (!class2.IsValid) + throw new ArgumentException ("Handle must be valid.", "class2"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->IsAssignableFrom (__env, class1.Handle, class2.Handle); + return (tmp != 0) ? true : false; + } + + public static unsafe bool IsSameObject (JniObjectReference object1, JniObjectReference object2) + { + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->IsSameObject (__env, object1.Handle, object2.Handle); + return (tmp != 0) ? true : false; + } + + public static unsafe JniObjectReference GetObjectClass (JniObjectReference instance) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetObjectClass (__env, instance.Handle); + JniEnvironment.LogCreateLocalRef (tmp); + return new JniObjectReference (tmp, JniObjectReferenceType.Local); + } + + public static unsafe bool IsInstanceOf (JniObjectReference instance, JniObjectReference type) + { + if (!instance.IsValid) + throw new ArgumentException ("Handle must be valid.", "instance"); + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->IsInstanceOf (__env, instance.Handle, type.Handle); + return (tmp != 0) ? true : false; + } + + internal static unsafe int _RegisterNatives (JniObjectReference type, JniNativeMethodRegistration [] methods, int numMethods) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->RegisterNatives (__env, type.Handle, methods, numMethods); + IntPtr thrown = (*((JNIEnv**)__env))->ExceptionOccurred (__env); + + Exception __e = JniEnvironment.GetExceptionForLastThrowable (thrown); + if (__e != null) + ExceptionDispatchInfo.Capture (__e).Throw (); + + return tmp; + } + + internal static unsafe int _UnregisterNatives (JniObjectReference type) + { + if (!type.IsValid) + throw new ArgumentException ("Handle must be valid.", "type"); + + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->UnregisterNatives (__env, type.Handle); + return tmp; + } + } + + internal static partial class Versions { + + internal static unsafe int GetVersion () + { + IntPtr __env = JniEnvironment.EnvironmentPointer; + var tmp = (*((JNIEnv**)__env))->GetVersion (__env); + return tmp; + } + } + } + +} +#endif // FEATURE_JNIENVIRONMENT_JI_FUNCTION_POINTERS