|
| 1 | +using System; |
| 2 | +using System.Text; |
| 3 | + |
| 4 | +using Mono.Cecil; |
| 5 | + |
| 6 | +using Android.Runtime; |
| 7 | +using Java.Interop.Tools.Diagnostics; |
| 8 | +using Java.Interop.Tools.TypeNameMappings; |
| 9 | + |
| 10 | +using MethodAttributes = Mono.Cecil.MethodAttributes; |
| 11 | +using static Java.Interop.Tools.TypeNameMappings.JavaNativeTypeManager; |
| 12 | + |
| 13 | +namespace Java.Interop.Tools.JavaCallableWrappers |
| 14 | +{ |
| 15 | + |
| 16 | + public partial class JavaCallableWrapperGenerator { |
| 17 | + internal class Signature { |
| 18 | + |
| 19 | + public Signature (MethodDefinition method, RegisterAttribute register, IMetadataResolver cache, bool shouldBeDynamicallyRegistered = true) : this (method, register, null, null, cache, shouldBeDynamicallyRegistered) {} |
| 20 | + |
| 21 | + public Signature (MethodDefinition method, RegisterAttribute register, string? managedParameters, string? outerType, IMetadataResolver cache, bool shouldBeDynamicallyRegistered = true) |
| 22 | + : this (register.Name, register.Signature, register.Connector, managedParameters, outerType, null) |
| 23 | + { |
| 24 | + Annotations = JavaCallableWrapperGenerator.GetAnnotationsString ("\t", method.CustomAttributes, cache); |
| 25 | + IsDynamicallyRegistered = shouldBeDynamicallyRegistered; |
| 26 | + } |
| 27 | + |
| 28 | + public Signature (MethodDefinition method, ExportAttribute export, string? managedParameters, IMetadataResolver cache) |
| 29 | + : this (method.Name, GetJniSignature (method, cache), "__export__", null, null, export.SuperArgumentsString) |
| 30 | + { |
| 31 | + IsExport = true; |
| 32 | + IsStatic = method.IsStatic; |
| 33 | + JavaAccess = JavaCallableWrapperGenerator.GetJavaAccess (method.Attributes & MethodAttributes.MemberAccessMask); |
| 34 | + ThrownTypeNames = export.ThrownNames; |
| 35 | + JavaNameOverride = export.Name; |
| 36 | + ManagedParameters = managedParameters; |
| 37 | + Annotations = JavaCallableWrapperGenerator.GetAnnotationsString ("\t", method.CustomAttributes, cache); |
| 38 | + } |
| 39 | + |
| 40 | + public Signature (MethodDefinition method, ExportFieldAttribute exportField, IMetadataResolver cache) |
| 41 | + : this (method.Name, GetJniSignature (method, cache), "__export__", null, null, null) |
| 42 | + { |
| 43 | + if (method.HasParameters) |
| 44 | + Diagnostic.Error (4205, JavaCallableWrapperGenerator.LookupSource (method), Localization.Resources.JavaCallableWrappers_XA4205); |
| 45 | + if (method.ReturnType.MetadataType == MetadataType.Void) |
| 46 | + Diagnostic.Error (4208, JavaCallableWrapperGenerator.LookupSource (method), Localization.Resources.JavaCallableWrappers_XA4208); |
| 47 | + IsExport = true; |
| 48 | + IsStatic = method.IsStatic; |
| 49 | + JavaAccess = JavaCallableWrapperGenerator.GetJavaAccess (method.Attributes & MethodAttributes.MemberAccessMask); |
| 50 | + |
| 51 | + // annotations are processed within JavaFieldInfo, not the initializer method. So we don't generate them here. |
| 52 | + } |
| 53 | + |
| 54 | + public Signature (string name, string? signature, string? connector, string? managedParameters, string? outerType, string? superCall) |
| 55 | + { |
| 56 | + ManagedParameters = managedParameters; |
| 57 | + JniSignature = signature ?? throw new ArgumentNullException ("`connector` cannot be null.", nameof (connector)); |
| 58 | + Method = "n_" + name + ":" + JniSignature + ":" + connector; |
| 59 | + Name = name; |
| 60 | + |
| 61 | + var jnisig = JniSignature; |
| 62 | + int closer = jnisig.IndexOf (')'); |
| 63 | + string ret = jnisig.Substring (closer + 1); |
| 64 | + retval = JavaNativeTypeManager.Parse (ret)?.Type; |
| 65 | + string jniparms = jnisig.Substring (1, closer - 1); |
| 66 | + if (string.IsNullOrEmpty (jniparms) && string.IsNullOrEmpty (superCall)) |
| 67 | + return; |
| 68 | + var parms = new StringBuilder (); |
| 69 | + var scall = new StringBuilder (); |
| 70 | + var acall = new StringBuilder (); |
| 71 | + bool first = true; |
| 72 | + int i = 0; |
| 73 | + foreach (JniTypeName jti in JavaNativeTypeManager.FromSignature (jniparms)) { |
| 74 | + if (outerType != null) { |
| 75 | + acall.Append (outerType).Append (".this"); |
| 76 | + outerType = null; |
| 77 | + continue; |
| 78 | + } |
| 79 | + string? parmType = jti.Type; |
| 80 | + if (!first) { |
| 81 | + parms.Append (", "); |
| 82 | + scall.Append (", "); |
| 83 | + acall.Append (", "); |
| 84 | + } |
| 85 | + first = false; |
| 86 | + parms.Append (parmType).Append (" p").Append (i); |
| 87 | + scall.Append ("p").Append (i); |
| 88 | + acall.Append ("p").Append (i); |
| 89 | + ++i; |
| 90 | + } |
| 91 | + this.parms = parms.ToString (); |
| 92 | + this.call = superCall != null ? superCall : scall.ToString (); |
| 93 | + this.ActivateCall = acall.ToString (); |
| 94 | + } |
| 95 | + |
| 96 | + string? call; |
| 97 | + public string? SuperCall { |
| 98 | + get { return call; } |
| 99 | + } |
| 100 | + |
| 101 | + public string? ActivateCall {get; private set;} |
| 102 | + |
| 103 | + public readonly string Name; |
| 104 | + public readonly string? JavaNameOverride; |
| 105 | + public string JavaName { |
| 106 | + get { return JavaNameOverride ?? Name; } |
| 107 | + } |
| 108 | + |
| 109 | + string? parms; |
| 110 | + public string? Params { |
| 111 | + get { return parms; } |
| 112 | + } |
| 113 | + |
| 114 | + string? retval; |
| 115 | + public string? Retval { |
| 116 | + get { return retval; } |
| 117 | + } |
| 118 | + |
| 119 | + public string? ThrowsDeclaration { |
| 120 | + get { return ThrownTypeNames?.Length > 0 ? " throws " + String.Join (", ", ThrownTypeNames) : null; } |
| 121 | + } |
| 122 | + |
| 123 | + public readonly string? JavaAccess; |
| 124 | + public readonly string? ManagedParameters; |
| 125 | + public readonly string JniSignature; |
| 126 | + public readonly string Method; |
| 127 | + public readonly bool IsExport; |
| 128 | + public readonly bool IsStatic; |
| 129 | + public readonly bool IsDynamicallyRegistered = true; |
| 130 | + public readonly string []? ThrownTypeNames; |
| 131 | + public readonly string? Annotations; |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments