|
| 1 | +#nullable enable |
| 2 | + |
| 3 | +#if NET |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Diagnostics.CodeAnalysis; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Diagnostics; |
| 9 | +using System.Reflection; |
| 10 | +using System.Runtime.InteropServices; |
| 11 | +using System.Runtime.Versioning; |
| 12 | + |
| 13 | +namespace Java.Interop |
| 14 | +{ |
| 15 | + public struct JniMemberSignature : IEquatable<JniMemberSignature> |
| 16 | + { |
| 17 | + public static readonly JniMemberSignature Empty; |
| 18 | + |
| 19 | + string? memberName; |
| 20 | + string? memberSignature; |
| 21 | + |
| 22 | + public string MemberName => memberName ?? throw new InvalidOperationException (); |
| 23 | + public string MemberSignature => memberSignature ?? throw new InvalidOperationException (); |
| 24 | + |
| 25 | + public JniMemberSignature (string memberName, string memberSignature) |
| 26 | + { |
| 27 | + if (string.IsNullOrEmpty (memberName)) { |
| 28 | + throw new ArgumentNullException (nameof (memberName)); |
| 29 | + } |
| 30 | + if (string.IsNullOrEmpty (memberSignature)) { |
| 31 | + throw new ArgumentNullException (nameof (memberSignature)); |
| 32 | + } |
| 33 | + this.memberName = memberName; |
| 34 | + this.memberSignature = memberSignature; |
| 35 | + } |
| 36 | + |
| 37 | + public static int GetParameterCountFromMethodSignature (string jniMethodSignature) |
| 38 | + { |
| 39 | + if (jniMethodSignature.Length < "()V".Length || jniMethodSignature [0] != '(' ) { |
| 40 | + throw new ArgumentException ( |
| 41 | + $"Member signature `{jniMethodSignature}` is not a method signature. Method signatures must start with `(`.", |
| 42 | + nameof (jniMethodSignature)); |
| 43 | + } |
| 44 | + int count = 0; |
| 45 | + int index = 1; |
| 46 | + while (index < jniMethodSignature.Length && |
| 47 | + jniMethodSignature [index] != ')') { |
| 48 | + ExtractType (jniMethodSignature, ref index); |
| 49 | + count++; |
| 50 | + } |
| 51 | + return count; |
| 52 | + } |
| 53 | + |
| 54 | + internal static (int StartIndex, int Length) ExtractType (string signature, ref int index) |
| 55 | + { |
| 56 | + AssertSignatureIndex (signature, index); |
| 57 | + var i = index++; |
| 58 | + switch (signature [i]) { |
| 59 | + case '[': |
| 60 | + if ((i+1) >= signature.Length) |
| 61 | + throw new InvalidOperationException ($"Missing array type after '[' at index {i} in: `{signature}`"); |
| 62 | + var rest = ExtractType (signature, ref index); |
| 63 | + return (StartIndex: i, Length: index - i); |
| 64 | + case 'B': |
| 65 | + case 'C': |
| 66 | + case 'D': |
| 67 | + case 'F': |
| 68 | + case 'I': |
| 69 | + case 'J': |
| 70 | + case 'S': |
| 71 | + case 'V': |
| 72 | + case 'Z': |
| 73 | + return (StartIndex: i, Length: 1); |
| 74 | + case 'L': |
| 75 | + int depth = 0; |
| 76 | + int e = index; |
| 77 | + while (e < signature.Length) { |
| 78 | + var c = signature [e++]; |
| 79 | + if (depth == 0 && c == ';') |
| 80 | + break; |
| 81 | + } |
| 82 | + if (e > signature.Length) |
| 83 | + throw new InvalidOperationException ($"Missing reference type after `{signature [i]}` at index {i} in `{signature}`!"); |
| 84 | + index = e; |
| 85 | + return (StartIndex: i, Length: (e - i)); |
| 86 | + default: |
| 87 | + throw new InvalidOperationException ($"Unknown JNI Type `{signature [i]}` within: `{signature}`!"); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + internal static void AssertSignatureIndex (string signature, int index) |
| 92 | + { |
| 93 | + if (signature == null) |
| 94 | + throw new ArgumentNullException (nameof (signature)); |
| 95 | + if (signature.Length == 0) |
| 96 | + throw new ArgumentException ("Descriptor cannot be empty string", nameof (signature)); |
| 97 | + if (index >= signature.Length) |
| 98 | + throw new ArgumentException ("index >= descriptor.Length", nameof (index)); |
| 99 | + } |
| 100 | + |
| 101 | + public override int GetHashCode () |
| 102 | + { |
| 103 | + return (memberName?.GetHashCode () ?? 0) ^ |
| 104 | + (memberSignature?.GetHashCode () ?? 0); |
| 105 | + } |
| 106 | + |
| 107 | + public override bool Equals (object? obj) |
| 108 | + { |
| 109 | + var v = obj as JniMemberSignature?; |
| 110 | + if (v.HasValue) |
| 111 | + return Equals (v.Value); |
| 112 | + return false; |
| 113 | + } |
| 114 | + |
| 115 | + public bool Equals (JniMemberSignature other) |
| 116 | + { |
| 117 | + return memberName == other.memberName && |
| 118 | + memberSignature == other.memberSignature; |
| 119 | + } |
| 120 | + |
| 121 | + public override string ToString () |
| 122 | + { |
| 123 | + return $"{nameof (JniMemberSignature)} {{ " + |
| 124 | + $"{nameof (MemberName)} = {(memberName == null ? "null" : "\"" + memberName + "\"")}" + |
| 125 | + $", {nameof (MemberSignature)} = {(memberSignature == null ? "null" : "\"" + memberSignature + "\"")}" + |
| 126 | + $"}}"; |
| 127 | + } |
| 128 | + |
| 129 | + public static bool operator== (JniMemberSignature a, JniMemberSignature b) => a.Equals (b); |
| 130 | + public static bool operator!= (JniMemberSignature a, JniMemberSignature b) => !a.Equals (b); |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +#endif // NET |
0 commit comments