Skip to content

Commit 53d6051

Browse files
authored
[jnimarshalmethod-gen] Fix registration on Windows (#721)
`jnimarshalmethod-gen` didn't call [`MethodBuilder.SetParameters()`][0] when creating the marshal method. While this worked under Mono, the lack of a `MethodBuilder.SetParameters()` invocation caused `jnimarshalmethod-gen` to crash when running under .NET Framework: error JM4006: jnimarshalmethod-gen: Unable to process assembly '.\bin\TestDebug\Java.Interop.Export-Tests.dll' Specified argument was out of the range of valid values. Parameter name: The specified parameter index is not in range. System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: The specified parameter index is not in range. at System.Reflection.Emit.MethodBuilder.DefineParameter(Int32 position, ParameterAttributes attributes, String strParamName) at System.Linq.Expressions.Compiler.LambdaCompiler..ctor(AnalyzedTree tree, LambdaExpression lambda, MethodBuilder method) at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda, MethodBuilder method, DebugInfoGenerator debugInfoGenerator) at Xamarin.Android.Tools.JniMarshalMethodGenerator.App.AddRegisterNativeMembers(TypeBuilder dt, ParameterExpression targetType, List`1 registrationElements) in C:\Users\rodo\git\java.interop\tools\jnimarshalmethod-gen\App.cs:line 564 at Xamarin.Android.Tools.JniMarshalMethodGenerator.App.CreateMarshalMethodAssembly(String path) in C:\Users\rodo\git\java.interop\tools\jnimarshalmethod-gen\App.cs:line 426 at Xamarin.Android.Tools.JniMarshalMethodGenerator.App.ProcessAssemblies(List`1 assemblies) in C:\Users\rodo\git\java.interop\tools\jnimarshalmethod-gen\App.cs:line 201 Add a call to `MethodBuilder.SetParameters()` for Windows support. Additionally, when `jnimarshalmethod-gen -v` (verbose) is used, print out the type(s) for which`__RegisterNativeMembers()` is being emitted. [0]: https://docs.microsoft.com/en-us/dotnet/api/system.reflection.emit.methodbuilder.setparameters?view=netcore-3.1
1 parent 5a834d4 commit 53d6051

File tree

1 file changed

+6
-1
lines changed
  • tools/jnimarshalmethod-gen

1 file changed

+6
-1
lines changed

tools/jnimarshalmethod-gen/App.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,12 @@ static Expression CreateRegistration (string method, string signature, LambdaExp
484484

485485
static void AddRegisterNativeMembers (TypeBuilder dt, ParameterExpression targetType, List<Expression> registrationElements)
486486
{
487-
var args = Expression.Parameter (typeof (JniNativeMethodRegistrationArguments), "args");
487+
if (Verbose) {
488+
Console.Write ("Adding registration method for ");
489+
ColorWriteLine ($"{dt.FullName}", ConsoleColor.Green);
490+
}
488491

492+
var args = Expression.Parameter (typeof (JniNativeMethodRegistrationArguments), "args");
489493
var body = Expression.Block (
490494
new[]{targetType},
491495
Expression.Assign (targetType, Expression.Call (Type_GetType, Expression.Constant (dt.FullName))),
@@ -495,6 +499,7 @@ static void AddRegisterNativeMembers (TypeBuilder dt, ParameterExpression target
495499

496500
var rb = dt.DefineMethod ("__RegisterNativeMembers",
497501
System.Reflection.MethodAttributes.Public | System.Reflection.MethodAttributes.Static);
502+
rb.SetParameters (typeof (JniNativeMethodRegistrationArguments));
498503
rb.SetCustomAttribute (new CustomAttributeBuilder (typeof (JniAddNativeMethodRegistrationAttribute).GetConstructor (Type.EmptyTypes), new object[0]));
499504
#if _DUMP_REGISTER_NATIVE_MEMBERS
500505
Console.WriteLine ($"## Dumping contents of `{dt.FullName}::__RegisterNativeMembers`: ");

0 commit comments

Comments
 (0)