|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.Diagnostics.CodeAnalysis; |
| 4 | +using System.Runtime.ExceptionServices; |
| 5 | +using System.Runtime.InteropServices; |
| 6 | + |
| 7 | +namespace Java.Interop; |
| 8 | + |
| 9 | +partial class JniEnvironment { |
| 10 | + partial class StaticMethods { |
| 11 | + |
| 12 | + public static unsafe JniMethodInfo GetStaticMethodID (JniObjectReference type, ReadOnlySpan<byte> name, ReadOnlySpan<byte> signature) |
| 13 | + { |
| 14 | + if (!type.IsValid) |
| 15 | + throw new ArgumentException ("Handle must be valid.", "type"); |
| 16 | + |
| 17 | + IntPtr env = JniEnvironment.EnvironmentPointer; |
| 18 | + IntPtr method; |
| 19 | + IntPtr thrown; |
| 20 | + fixed (void* name_ptr = &MemoryMarshal.GetReference (name)) |
| 21 | + fixed (void* signature_ptr = &MemoryMarshal.GetReference (signature)) { |
| 22 | + method = JniNativeMethods.GetStaticMethodID (env, type.Handle, (IntPtr) name_ptr, (IntPtr) signature_ptr); |
| 23 | + thrown = JniNativeMethods.ExceptionOccurred (env); |
| 24 | + } |
| 25 | + |
| 26 | + Exception? __e = JniEnvironment.GetExceptionForLastThrowable (thrown); |
| 27 | + if (__e != null) |
| 28 | + ExceptionDispatchInfo.Capture (__e).Throw (); |
| 29 | + |
| 30 | + if (method == IntPtr.Zero) |
| 31 | + throw new InvalidOperationException ("Should not be reached; `GetStaticMethodID` should have thrown!"); |
| 32 | + |
| 33 | +#if DEBUG |
| 34 | + return new JniMethodInfo (name.ToString (), signature.ToString (), method, isStatic: true); |
| 35 | +#else // DEBUG |
| 36 | + return new JniMethodInfo (null!, null!, method, isStatic: true); |
| 37 | +#endif // DEBUG |
| 38 | + } |
| 39 | + |
| 40 | + internal static unsafe bool TryGetStaticMethod ( |
| 41 | + JniObjectReference type, |
| 42 | + ReadOnlySpan<byte> name, |
| 43 | + ReadOnlySpan<byte> signature, |
| 44 | + [NotNullWhen(true)] |
| 45 | + out JniMethodInfo? method) |
| 46 | + { |
| 47 | + method = null; |
| 48 | + |
| 49 | + if (!type.IsValid) |
| 50 | + throw new ArgumentException ("Handle must be valid.", "type"); |
| 51 | + |
| 52 | + IntPtr env = JniEnvironment.EnvironmentPointer; |
| 53 | + IntPtr id; |
| 54 | + IntPtr thrown; |
| 55 | + fixed (void* name_ptr = &MemoryMarshal.GetReference (name)) |
| 56 | + fixed (void* signature_ptr = &MemoryMarshal.GetReference (signature)) { |
| 57 | + id = JniNativeMethods.GetStaticMethodID (env, type.Handle, (IntPtr) name_ptr, (IntPtr) signature_ptr); |
| 58 | + thrown = JniNativeMethods.ExceptionOccurred (env); |
| 59 | + } |
| 60 | + |
| 61 | + if (thrown != IntPtr.Zero) { |
| 62 | + JniNativeMethods.ExceptionClear (env); |
| 63 | + JniEnvironment.References.RawDeleteLocalRef (env, thrown); |
| 64 | + thrown = IntPtr.Zero; |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + Debug.Assert (id != IntPtr.Zero); |
| 69 | + if (id == IntPtr.Zero) { |
| 70 | + return false; |
| 71 | + } |
| 72 | + |
| 73 | +#if DEBUG |
| 74 | + method = new JniMethodInfo (name.ToString (), signature.ToString (), id, isStatic: true); |
| 75 | +#else // DEBUG |
| 76 | + method = new JniMethodInfo (null!, null!, id, isStatic: true); |
| 77 | +#endif // DEBUG |
| 78 | + |
| 79 | + return true; |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments