Skip to content

Can't call Java desugared static interface methods in android libraries before API-24 #4574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Dolfik1 opened this issue Apr 17, 2020 · 4 comments
Assignees
Labels
Area: Bindings Issues in Java Library Binding projects.

Comments

@Dolfik1
Copy link

Dolfik1 commented Apr 17, 2020

Steps to Reproduce

  1. Generate bindings to any library with static interface method
  2. Set Minimum Android version to API Level 23 or lower
  3. Call static interface method

WebRtcTest.zip

Expected Behavior

Method called successfully

Actual Behavior

Java.Lang.NoSuchMethodError: 'no static method "Lorg/webrtc/EglBase;.create()Lorg/webrtc/EglBase;"'

Other

There are fix for Gradle: react-native-webrtc/react-native-webrtc#720 (comment)

Version Information

Microsoft Visual Studio Community 2019
Version 16.5.2
VisualStudio.16.Release/16.5.2+29926.136
Microsoft .NET Framework
Version 4.8.03752

Installed Version: Community

ASP.NET and Web Tools 2019 16.5.236.49856
ASP.NET and Web Tools 2019

ASP.NET Web Frameworks and Tools 2019 16.5.236.49856
For additional information, visit https://www.asp.net/

Azure App Service Tools v3.0.0 16.5.236.49856
Azure App Service Tools v3.0.0

Azure Functions and Web Jobs Tools 16.5.236.49856
Azure Functions and Web Jobs Tools

C# Tools 3.5.0-beta4-20153-05+20b9af913f1b8ce0a62f72bea9e75e4aa3cf6b0e
C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Common Azure Tools 1.10
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

Extensibility Message Bus 1.2.0 (d16-2@8b56e20)
Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration.

IntelliCode Extension 1.0
IntelliCode Visual Studio Extension Detailed Info

Microsoft Azure Tools 2.9
Microsoft Azure Tools for Microsoft Visual Studio 2019 - v2.9.30207.1

Microsoft Continuous Delivery Tools for Visual Studio 0.4
Simplifying the configuration of Azure DevOps pipelines from within the Visual Studio IDE.

Microsoft JVM Debugger 1.0
Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

Microsoft Library Manager 2.1.25+gdacdb9b7a1
Install client-side libraries easily to any web project

Microsoft MI-Based Debugger 1.0
Provides support for connecting Visual Studio to MI compatible debuggers

Microsoft Visual Studio Tools for Containers 1.1
Develop, run, validate your ASP.NET Core applications in the target environment. F5 your application directly into a container with debugging, or CTRL + F5 to edit & refresh your app without having to rebuild the container.

Mono Debugging for Visual Studio 16.5.514 (c4f36a9)
Support for debugging Mono processes with Visual Studio.

NuGet Package Manager 5.5.0
NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/

ProjectServicesPackage Extension 1.0
ProjectServicesPackage Visual Studio Extension Detailed Info

SQL Server Data Tools 16.0.62003.05170
Microsoft SQL Server Data Tools

ToolWindowHostedEditor 1.0
Example of hosting the editor embedded into a tool window.

TypeScript Tools 16.0.20225.2001
TypeScript Tools for Microsoft Visual Studio

Visual Basic Tools 3.5.0-beta4-20153-05+20b9af913f1b8ce0a62f72bea9e75e4aa3cf6b0e
Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Visual F# Tools 10.8.0.0 for F# 4.7 16.5.0-beta.20104.8+7c4de19faf36647c1ef700e655a52350840c6f03
Microsoft Visual F# Tools 10.8.0.0 for F# 4.7

Visual Studio Code Debug Adapter Host Package 1.0
Interop layer for hosting Visual Studio Code debug adapters in Visual Studio

Visual Studio Container Tools Extensions (Preview) 1.0
View, manage, and diagnose containers within Visual Studio.

Visual Studio Tools for Containers 1.0
Visual Studio Tools for Containers

VisualStudio.DeviceLog 1.0
Information about my package

VisualStudio.Foo 1.0
Information about my package

VisualStudio.Mac 1.0
Mac Extension for Visual Studio

Xamarin 16.5.000.528 (d16-5@2b54082)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin Designer 16.5.0.470 (remotes/origin/d16-5@681de3fd6)
Visual Studio extension to enable Xamarin Designer tools in Visual Studio.

Xamarin Templates 16.5.49 (0904f41)
Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms.

Xamarin.Android SDK 10.2.0.100 (d16-5/988c811)
Xamarin.Android Reference Assemblies and MSBuild support.
Mono: c0c5c78
Java.Interop: xamarin/java.interop/d16-5@fc18c54
ProGuard: xamarin/proguard@905836d
SQLite: xamarin/sqlite@46204c4
Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-5@9f4ed4b

Xamarin.iOS and Xamarin.Mac SDK 13.16.0.11 (aa73e41)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.

@Dolfik1 Dolfik1 added the Area: App Runtime Issues in `libmonodroid.so`. label Apr 17, 2020
@radekdoulik radekdoulik added this to the Under Consideration milestone Apr 17, 2020
@jonpryor
Copy link
Contributor

The problem is caused by "desugaring", which "moves things around." In particular, if you run dexdump classes.dex on the classes.dex contained within the .apk, the only create method with a "type" of ()Lorg/webrtc/EglBase; is declared in Lorg/webrtc/EglBase$-CC;:

$ $HOME/android-toolchain/sdk/build-tools/29.0.2/dexdump classes.dex
…
Class #1481            -
  Class descriptor  : 'Lorg/webrtc/EglBase$-CC;'
  Access flags      : 0x1011 (PUBLIC FINAL SYNTHETIC)
  Superclass        : 'Ljava/lang/Object;'
  Interfaces        -
  Static fields     -
  Instance fields   -
  Direct methods    -
    …
    #1              : (in Lorg/webrtc/EglBase$-CC;)
      name          : 'create'
      type          : '()Lorg/webrtc/EglBase;'
      access        : 0x0009 (PUBLIC STATIC)
      code          -
      registers     : 2
      ins           : 0
      outs          : 2
      insns size    : 8 16-bit code units
      catches       : (none)
      positions     : 
        0x0000 line=170
      locals        : 

EglBase$-CC is not a class present within the original classes.jar. Equally important, our code is looking for the create method on org/webrtc/EglBase, not org/webrtc/EglBase$-CC;, and while org/webrtc/EglBase exists in classes.dex, it does not contain the static create method.

This is why the Java.Lang.NoSuchMethodError is thrown.

Disabling desugar allow the app to run, but requires -- as you note in step (2) -- that the Minimum Android version to API Level be 24 or higher. 26 in this case, actually, to avoid a different build error:

D8 : error : Invoke-customs are only supported starting with Android O (--min-api 26)

I'm not entirely sure how to address this on our end. We could skip binding static interface methods/etc. unless building against Mono.Android.dll v7.0, which would avoid the NoSuchMethodException… by removing the method entirely. (This isn't a "fix.") I'm also not sure it would actually work, because all Mono.Android.dlls have an assembly version of 0.0.0.0, so we'd have to "somehow" ensure that the v7.0 bit makes it into the NuGet package? (I don't understand how that process works.)

The only other fix I can think of is to IL-rewrite the binding assembly to replace appropriate instances of Lorg/webrtc/EglBase; with Lorg/webrtc/EglBase$-CC;, which could be done but could be "brittle".

@Dolfik1
Copy link
Author

Dolfik1 commented Apr 17, 2020

@jonpryor is it possible fix that via transforms without IL rewriting?

I tried to use this code, but it doesn't worked for me:

<add-node path="/api/package[@name='org.webrtc']">
  <interface abstract="true" deprecated="not deprecated" final="false" name="EglBaseCustom" static="false" visibility="public" jni-signature="Lorg/webrtc/EglBase;">
    <method abstract="false" deprecated="not deprecated" final="false" name="create" jni-signature="()Lorg/webrtc/EglBase$-CC;" 
		  bridge="false" native="false" return="org.webrtc.EglBase" jni-return="Lorg/webrtc/EglBase;" static="true" synchronized="false" synthetic="false" visibility="public">
    </method>
  </interface>
</add-node>```

@jpobst jpobst assigned jonpryor and unassigned jpobst Apr 20, 2020
@Dolfik1
Copy link
Author

Dolfik1 commented Jun 19, 2020

I found workaround for this issue:

namespace Org.Webrtc
{
	[Register("org/webrtc/EglBase$-CC", DoNotGenerateAcw = true)]
	public abstract class EglBaseStatic : Java.Lang.Object
	{
		internal EglBaseStatic()
		{
		}

		// Metadata.xml XPath method reference: path="/api/package[@name='org.webrtc']/interface[@name='EglBase']/method[@name='create' and count(parameter)=0]"
		[Register("create", "()Lorg/webrtc/EglBase;", "")]
		public static unsafe global::Org.Webrtc.IEglBase Create()
		{
			const string __id = "create.()Lorg/webrtc/EglBase;";
			try
			{
				var __rm = _members.StaticMethods.InvokeObjectMethod(__id, null);
				return global::Java.Lang.Object.GetObject<global::Org.Webrtc.IEglBase>(__rm.Handle, JniHandleOwnership.TransferLocalRef);
			}
			finally
			{
			}
		}

		static readonly JniPeerMembers _members = new XAPeerMembers("org/webrtc/EglBase$-CC", typeof(EglBaseStatic));
	}
}

But this workaround works only in Debug configuration. I got this exception in Release configuration:

Java.Lang.IncompatibleClassChangeError: no static method "Lorg/webrtc/EglBase$-CC;.create()Lorg/webrtc/EglBase;"
      at Java.Interop.JniEnvironment+StaticMethods.GetStaticMethodID (Java.Interop.JniObjectReference type, System.String name, System.String signature) [0x0005b] in <3f76ba23ba894c92a0082c32427fb8a5>:0 
      at Java.Interop.JniType.GetStaticMethod (System.String name, System.String signature) [0x0000c] in <3f76ba23ba894c92a0082c32427fb8a5>:0 
      at Java.Interop.JniPeerMembers+JniStaticMethods.GetMethodInfo (System.String encodedMember) [0x00036] in <3f76ba23ba894c92a0082c32427fb8a5>:0 
      at Java.Interop.JniPeerMembers+JniStaticMethods.InvokeObjectMethod (System.String encodedMember, Java.Interop.JniArgumentValue* parameters) [0x00000] in <3f76ba23ba894c92a0082c32427fb8a5>:0 
      at Org.Webrtc.EglBaseStatic.Create () [0x0000a] in <e9145eeab3c54ba095bbc83c7d9b5f7a>:0 

@jonpryor what do you think about this?

jonpryor added a commit to jonpryor/java.interop that referenced this issue Dec 27, 2021
Context: dotnet#867

There are two scenarios for which a "generalized" type & member
remapping solution would be useful:

 1. Desugaring
 2. Microsoft Intune MAM

Note: this new support is only present when targeting .NET 6+,
and will not (currently) be present in Classic Xamarin.Android.

~~ Desugaring ~~

Context: dotnet/android#4574 (comment)
Context: dotnet/android#6142 (comment)

The [Android Runtime][0] is responsible for running the Dalvik
bytecode within e.g. `classes.dex`.  The Android runtime and Dalvik
bytecode formats have apparently changed over time in order to
support newer Java language features, such as support for static
interface methods:

	package org.webrtc;

	public /* partial */ interface EGLBase {
	    public static EGLBase create() { /* … */}
	}

Support for static interface methods was added in Java 8 and in
Android v8.0 (API-26).  If you want to use static interface methods
on an Android device running API-25 or earlier, you must [desugar][1]
the Java Bytecode.  The result of [desugaring][2] the above
`org.webrtc.EGLBase` type is that the static methods are moved to a
*new* type, with a `$-CC` suffix, "as if" the Java code were instead:

	package org.webrtc;

	public /* partial */ interface EGLBase {
	    // …
	}

	public final /* partial */ class EGLBase$-CC {
	    public static EGLBase create { /* … */ }
	}

While this works for pure Java code, this *does not work* with
Xamarin.Android, because our bindings currently expect (require) that
the types & members our binding process detects don't "move":

	// C# Binding for EGLBase
	namespace Org.Webrtc {
	    public partial interface IEGLBase {
	        private static readonly JniPeerMembers _members = new JniPeerMembers ("org/webrtc/EGLBase", typeof (IEGLBase));
	        public static IEGLBase Create()
	        {
	            const string __id = "create.()Lorg/webrtc/EglBase;"
	            var __rm = _members.StaticMethods.InvokeObjectMethod (__id, null);
	            return Java.Lang.Object.GetObject<IEGLBase>(__rm.Handle, JniHandleOwnership.TransferLocalRef);
	        }
	    }
	}

The `new JniPeerMembers(…)` invocation will use `JNIEnv::FindClass()`
to lookup the `org/webrtc/EGLBase` type, and `IEGLBase.Create()` will
use `JNIEnv::GetStaticMethodID()` and `JNIEnv::CallStaticObjectMethod()`
to lookup and invoke the `EGLBase.create()` method.

However, when desugaring is used, *there is no* `EGLBase.create()`
method.  Consequently, in a "desugared" environment,
`Java.Lang.NoSuchMethodError` will be thrown when `IEGLBase.Create()`
is invoked, as `EGLBase.create()` doesn't exist; it "should" instead
be looking for `EGLBase$-CC.create()`!

Introduce partial support for this scenario by adding the new method:

	namespace Java.Interop {
	    public partial class JniRuntime {
	        public partial class JniTypeManager {
	            public IReadOnlyList<string>? GetStaticMethodFallbackTypes (string jniSimpleReference) =>
	                GetStaticMethodFallbackTypesCore (jniSimpleReference);
	            protected virtual IReadOnlyList<string>? GetStaticMethodFallbackTypesCore (string jniSimpleReference) => null;
	        }
	    }
	}

`JniPeerMembers.JniStaticMethods.GetMethodInfo()` will attempt to
lookup the static method, "as normal".  If the method cannot be
found, then `JniRuntime.JniTypeManager.GetStaticMethodFallbackTypes()`
will be called, and we'll attempt to resolve the static method on each
type returned by `GetStaticMethodFallbackTypes()`.
If `GetStaticMethodFallbackTypes()` returns `null` or no fallback type
provides the method, then a `NoSuchMethodError` will be thrown.

TODO: Update xamarin-android to override
`GetStaticMethodFallbackTypes()`, to return
`$"{jniSimpleReference}$-CC"`.

~~ Microsoft Intune MAM ~~

Context: https://www.nuget.org/packages/Microsoft.Intune.MAM.Remapper.Tasks/
Context: https://docs.microsoft.com/en-us/mem/intune/developer/app-sdk-xamarin#remapper

The [Microsoft Intune App SDK Xamarin Bindings][3] is in a
similar-yet-different position: it involves Java & Dalvik Bytecode
manipulation for various security purposes, and in order to make that
work reasonably from Xamarin.Android, they *also* rewrite
Xamarin.Android IL so that it's consistent with the manipulated
Dalvik bytecode.

See `readme.md` and `content/MonoAndroid10/remapping-config.json`
within the [`Microsoft.Intune.MAM.Remapper.Tasks NuGet package`][4]
for some additional details/tidbits such as:

> This task operates on assemblies to replace the base type classes
> which Microsoft Intune requires to implement its SDK (for example,
> Intune requires using a `MAMActivity` in place of `Activity` and
> methods such as `OnMAMCreate` instead of `OnCreate`).

	"ClassRewrites": [
	  {
	    "Class": {
	      "From": "android.app.Activity",
	      "To": "com.microsoft.intune.mam.client.app.MAMActivity"
	    },
	    "Methods": [
	      {
	        "MakeStatic": false,
	        "OriginalName": "onCreate",
	        "NewName": "onMAMCreate",
	        "OriginalParams": [
	          "android.os.Bundle"
	        ]
	      }
	    ]
	  },
	  {
	    "Class": {
	      "From": "android.content.pm.PackageManager",
	      "To": "com.microsoft.intune.mam.client.content.pm.MAMPackageManagement"
	    },
	    "Methods": [
	      {
	        "MakeStatic": true,
	        "OriginalName": "checkPermission",
	      }
	    ]
	  }
	]
	"GlobalMethodCalls": [
	  {
	    "Class": {
	      "From": "android.app.PendingIntent",
	      "To": "com.microsoft.intune.mam.client.app.MAMPendingIntent"
	    },
	    "Methods": [
	      {
	        "MakeStatic": false,
	        "OriginalName": "getActivities"
	      },
	    ]
	  }
	]

While what the InTune team has *works*, it suffers from a number of
"workflow" problems, e.g. incremental builds are problematic (they
might try to rewrite assemblies which were already rewritten), IL
rewriting is *always* "fun", and if we change IL binding patterns,
they'll need to support previous "binding styles" and newer styles.

Introduce partial support for this scenario by adding the following
members to `Java.Interop.JniRuntime.JniTypeManager`:

	namespace Java.Interop {
	    public partial class JniRuntime {

	        public struct ReplacementMethodInfo {
	            public  string? SourceJniType                   {get; set;}
	            public  string? SourceJniMethodName             {get; set;}
	            public  string? SourceJniMethodSignature        {get; set;}
	            public  string? TargetJniType                   {get; set;}
	            public  string? TargetJniMethodName             {get; set;}
	            public  string? TargetJniMethodSignature        {get; set;}
	            public  int?    TargetJniMethodParameterCount   {get; set;}
	            public  bool    TargetJniMethodIsStatic         {get; set;}
	        }

	        public partial class JniTypeManager {

	            public string? GetReplacementType (string jniSimpleReference) =>
	                GetReplacementTypeCore (jniSimpleReference);
	            protected virtual string? GetReplacementTypeCore (string jniSimpleReference) => null;

	            public ReplacementMethodInfo? GetReplacementMethodInfo (string jniSimpleReference, string jniMethodName, string jniMethodSignature) =>
	                GetReplacementMethodInfoCore (jniSimpleReference, jniMethodName,jniMethodSignature);
	            protected virtual ReplacementMethodInfo? GetReplacementMethodInfoCore (string jniSimpleReference, string jniMethodName, string jniMethodSignature) => null;
	        }
	    }
	}

These new methods are used by `JniPeerMembers`.

Consider "normal" usage of `JniPeerMembers`, within a binding:

	partial class Activity {
	    static readonly JniPeerMembers _members = new XAPeerMembers (jniPeerTypeName:"android/app/Activity", managedPeerType:typeof (Activity));
	}

`JniRuntime.JniTypeManager.GetReplacementType()` allows "replacing"
the `jniPeerTypeName` value with a "replacement" JNI type name.
The "replacement" type will be used for all field and method lookups.
This allows supporting the `ClassRewrites[0].Class.From` /
`ClassRewrites[0].Class.To` semantics.

`JniRuntime.JniTypeManager.GetReplacementMethodInfo()` is the key
integration for all other "replacement" semantics.  Given the
source type, source method name, and source JNI signature, it is
responsible for providing an *overriding* target type, target method
name, and target JNI signature.

For `ClassRewrites[0].Methods[0]`, this allows "renaming"
`Activity.onCreate()` to `MAMActivity.onMAMCreate()`:

	var r = JniEnvironment.Runtime.TypeManager.GetReplacementMethodInfo (
	    "android/app/Activity",
	    "onCreate",
	    "(Landroid/os/Bundle;)V"
	);
	// is equivalent to:
	var r = new JniRuntime.ReplacementMethodInfo {
	    SourceJniType                   = "android/app/Activity",   // from input parameter
	    SourceJniMethodName             = "onCreate",               // from input parameter
	    SourceJniMethodSignature        = "(Landroid/os/Bundle;)V", // from input parameter

	    TargetJniType                   = "com/microsoft/intune/mam/client/app/MAMActivity",
	    TargetJniMethodName             = "onMAMCreate",
	    SourceJniMethodSignature        = null,                     // "use original value"
	    TargetJniMethodParameterCount   = null,                     // unchanged
	    TargetJniMethodIsStatic         = false,
	}

`ClassRewrites[1].Methods[0]` is "weird", in that it has
`MakeStatic: true`.  The semantics for when `MakeStatic: true` exists
is that the "original" method is an *instance* method, and the target
method is a *`static`* method, *and* the `this` instance now becomes
a *parameter* to the method.  This is likewise supported via
`JniRuntime.JniTypeManager.GetReplacementMethodInfo()`, and is
identified by:

 1. `ReplacementMethodInfo.TargetJniMethodParameterCount` being a
    non-`null` value, and

 2. `ReplacementMethodInfo.TargetJniMethodIsStatic` is true.

Thus, `ClassRewrites[1].Methods[0]` is akin to:

	var r = JniEnvironment.Runtime.TypeManager.GetReplacementMethodInfo (
	    "android/content/pm/PackageManager",
	    "checkPermission",
	    "(Ljava/lang/String;Ljava/lang/String;)I"
	);
	// is equivalent to:
	var r = new JniRuntime.ReplacementMethodInfo {
	    SourceJniType                   = "android/content/pm/PackageManager",              // from input parameter
	    SourceJniMethodName             = "checkPermission",                                // from input parameter
	    SourceJniMethodSignature        = "(Ljava/lang/String;Ljava/lang/String;)I",        // from input parameter

	    TargetJniType                   = "com/microsoft/intune/mam/client/content/pm/MAMPackageManagement",
	    TargetJniMethodName             = "CheckPermission",
	    SourceJniMethodSignature        = "(Landroid/content/pm/PackageManager;Ljava/lang/String;Ljava/lang/String;)I",
	    TargetJniMethodParameterCount   = 3,
	    TargetJniMethodIsStatic         = true,
	}

(Note that the subclass is responsible for providing this data.)

`ReplacementMethodInfo.TargetJniMethodParameterCount` must be the
number of parameters that the target method accepts.  This is needed
so that `JniPeerMembers.JniInstanceMethods.TryInvoke*StaticRedirect()`
can appropriately reallocate the `JniArgumentValue*` array, so that
the `this` parameter can be added to the beginning of the parameters.

~~ Overhead? ~~

All these extra invocations into `JniRuntime.JniTypeManager` imply
additional overhead to invoke a Java method.  However, this is all
done during the *first* time a method is looked up, after which the
`JniMethodInfo` instance is *cached* for a given
(type, method, signature) tuple.

To demonstrate overheads, `samples/Hello` has been updated to accept
a new `-t` value; when provided, it invokes the
`java.lang.Object.hashCode()` method 1 million times and calculates
the average invocation overhead:

	% dotnet run --project samples/Hello -- -t
	Object.hashCode: 1000000 invocations. Total=00:00:00.5196758; Average=0.0005196758ms

If we replace the .NET 6 `samples/Hello/bin/Debug/Java.Interop.dll`
assembly with e.g. `bin/Debug/Java.Interop.dll`, we can compare to
performance *without* these new changes, as the changes are only in
the .NET 6 build:

	% \cp bin/Debug/Java.Interop{.dll,.pdb} samples/Hello/bin/Debug
	% dotnet samples/Hello/bin/Debug/Hello.dll -t
	Object.hashCode: 1000000 invocations. Total=00:00:00.5386676; Average=0.0005386676ms

There is a fair bit of variation in `dotnet Hello.dll -t` output,
but they're all roughly similar.  There doesn't appear to be
significant overhead for this particular test.

[0]: https://en.wikipedia.org/wiki/Android_Runtime
[1]: https://developer.android.com/studio/write/java8-support
[2]: https://developer.android.com/studio/write/java8-support-table
[3]: https://docs.microsoft.com/en-us/mem/intune/developer/app-sdk-xamarin
[4]: https://www.nuget.org/packages/Microsoft.Intune.MAM.Remapper.Tasks/
jonpryor added a commit to jonpryor/java.interop that referenced this issue Jan 4, 2022
Context: dotnet#867

There are two scenarios for which a "generalized" type & member
remapping solution would be useful:

 1. Desugaring
 2. Microsoft Intune MAM

Note: this new support is only present when targeting .NET 6+,
and will not (currently) be present in Classic Xamarin.Android.

~~ Desugaring ~~

Context: dotnet/android#4574 (comment)
Context: dotnet/android#6142 (comment)

The [Android Runtime][0] is responsible for running the Dalvik
bytecode within e.g. `classes.dex`.  The Android runtime and Dalvik
bytecode formats have apparently changed over time in order to
support newer Java language features, such as support for static
interface methods:

	package org.webrtc;

	public /* partial */ interface EGLBase {
	    public static EGLBase create() { /* … */}
	}

Support for static interface methods was added in Java 8 and in
Android v8.0 (API-26).  If you want to use static interface methods
on an Android device running API-25 or earlier, you must [desugar][1]
the Java Bytecode.  The result of [desugaring][2] the above
`org.webrtc.EGLBase` type is that the static methods are moved to a
*new* type, with a `$-CC` suffix, "as if" the Java code were instead:

	package org.webrtc;

	public /* partial */ interface EGLBase {
	    // …
	}

	public final /* partial */ class EGLBase$-CC {
	    public static EGLBase create { /* … */ }
	}

While this works for pure Java code, this *does not work* with
Xamarin.Android, because our bindings currently expect (require) that
the types & members our binding process detects don't "move":

	// C# Binding for EGLBase
	namespace Org.Webrtc {
	    public partial interface IEGLBase {
	        private static readonly JniPeerMembers _members = new JniPeerMembers ("org/webrtc/EGLBase", typeof (IEGLBase));
	        public static IEGLBase Create()
	        {
	            const string __id = "create.()Lorg/webrtc/EglBase;"
	            var __rm = _members.StaticMethods.InvokeObjectMethod (__id, null);
	            return Java.Lang.Object.GetObject<IEGLBase>(__rm.Handle, JniHandleOwnership.TransferLocalRef);
	        }
	    }
	}

The `new JniPeerMembers(…)` invocation will use `JNIEnv::FindClass()`
to lookup the `org/webrtc/EGLBase` type, and `IEGLBase.Create()` will
use `JNIEnv::GetStaticMethodID()` and `JNIEnv::CallStaticObjectMethod()`
to lookup and invoke the `EGLBase.create()` method.

However, when desugaring is used, *there is no* `EGLBase.create()`
method.  Consequently, in a "desugared" environment,
`Java.Lang.NoSuchMethodError` will be thrown when `IEGLBase.Create()`
is invoked, as `EGLBase.create()` doesn't exist; it "should" instead
be looking for `EGLBase$-CC.create()`!

Introduce partial support for this scenario by adding the new method:

	namespace Java.Interop {
	    public partial class JniRuntime {
	        public partial class JniTypeManager {
	            public IReadOnlyList<string>? GetStaticMethodFallbackTypes (string jniSimpleReference) =>
	                GetStaticMethodFallbackTypesCore (jniSimpleReference);
	            protected virtual IReadOnlyList<string>? GetStaticMethodFallbackTypesCore (string jniSimpleReference) => null;
	        }
	    }
	}

`JniPeerMembers.JniStaticMethods.GetMethodInfo()` will attempt to
lookup the static method, "as normal".  If the method cannot be
found, then `JniRuntime.JniTypeManager.GetStaticMethodFallbackTypes()`
will be called, and we'll attempt to resolve the static method on each
type returned by `GetStaticMethodFallbackTypes()`.
If `GetStaticMethodFallbackTypes()` returns `null` or no fallback type
provides the method, then a `NoSuchMethodError` will be thrown.

TODO: Update xamarin-android to override
`GetStaticMethodFallbackTypes()`, to return
`$"{jniSimpleReference}$-CC"`.

~~ Microsoft Intune MAM ~~

Context: https://www.nuget.org/packages/Microsoft.Intune.MAM.Remapper.Tasks/
Context: https://docs.microsoft.com/en-us/mem/intune/developer/app-sdk-xamarin#remapper

The [Microsoft Intune App SDK Xamarin Bindings][3] is in a
similar-yet-different position: it involves Java & Dalvik Bytecode
manipulation for various security purposes, and in order to make that
work reasonably from Xamarin.Android, they *also* rewrite
Xamarin.Android IL so that it's consistent with the manipulated
Dalvik bytecode.

See `readme.md` and `content/MonoAndroid10/remapping-config.json`
within the [`Microsoft.Intune.MAM.Remapper.Tasks NuGet package`][4]
for some additional details/tidbits such as:

> This task operates on assemblies to replace the base type classes
> which Microsoft Intune requires to implement its SDK (for example,
> Intune requires using a `MAMActivity` in place of `Activity` and
> methods such as `OnMAMCreate` instead of `OnCreate`).

`remapping-config.json` contains JSON fragments such as:

	"ClassRewrites": [
	  {
	    "Class": {
	      "From": "android.app.Activity",
	      "To": "com.microsoft.intune.mam.client.app.MAMActivity"
	    },
	    "Methods": [
	      {
	        "MakeStatic": false,
	        "OriginalName": "onCreate",
	        "NewName": "onMAMCreate",
	        "OriginalParams": [
	          "android.os.Bundle"
	        ]
	      }
	    ]
	  },
	  {
	    "Class": {
	      "From": "android.content.pm.PackageManager",
	      "To": "com.microsoft.intune.mam.client.content.pm.MAMPackageManagement"
	    },
	    "Methods": [
	      {
	        "MakeStatic": true,
	        "OriginalName": "checkPermission",
	      }
	    ]
	  }
	]
	"GlobalMethodCalls": [
	  {
	    "Class": {
	      "From": "android.app.PendingIntent",
	      "To": "com.microsoft.intune.mam.client.app.MAMPendingIntent"
	    },
	    "Methods": [
	      {
	        "MakeStatic": false,
	        "OriginalName": "getActivities"
	      },
	    ]
	  }
	]

While what the InTune team has *works*, it suffers from a number of
"workflow" problems, e.g. incremental builds are problematic (they
might try to rewrite assemblies which were already rewritten), IL
rewriting is *always* "fun", and if we change IL binding patterns,
they'll need to support previous "binding styles" and newer styles.

Introduce partial support for this scenario by adding the following
members to `Java.Interop.JniRuntime.JniTypeManager`:

	namespace Java.Interop {
	    public partial class JniRuntime {

	        public struct ReplacementMethodInfo {
	            public  string? SourceJniType                   {get; set;}
	            public  string? SourceJniMethodName             {get; set;}
	            public  string? SourceJniMethodSignature        {get; set;}
	            public  string? TargetJniType                   {get; set;}
	            public  string? TargetJniMethodName             {get; set;}
	            public  string? TargetJniMethodSignature        {get; set;}
	            public  int?    TargetJniMethodParameterCount   {get; set;}
	            public  bool    TargetJniMethodInstanceToStatic {get; set;}
	        }

	        public partial class JniTypeManager {

	            public string? GetReplacementType (string jniSimpleReference) =>
	                GetReplacementTypeCore (jniSimpleReference);
	            protected virtual string? GetReplacementTypeCore (string jniSimpleReference) => null;

	            public ReplacementMethodInfo? GetReplacementMethodInfo (string jniSimpleReference, string jniMethodName, string jniMethodSignature) =>
	                GetReplacementMethodInfoCore (jniSimpleReference, jniMethodName,jniMethodSignature);
	            protected virtual ReplacementMethodInfo? GetReplacementMethodInfoCore (string jniSimpleReference, string jniMethodName, string jniMethodSignature) => null;
	        }
	    }
	}

These new methods are used by `JniPeerMembers`.

Consider "normal" usage of `JniPeerMembers`, within a binding:

	partial class Activity {
	    static readonly JniPeerMembers _members = new XAPeerMembers (jniPeerTypeName:"android/app/Activity", managedPeerType:typeof (Activity));
	}

`JniRuntime.JniTypeManager.GetReplacementType()` allows "replacing"
the `jniPeerTypeName` value with a "replacement" JNI type name.
The "replacement" type will be used for all field and method lookups.
This allows supporting the `ClassRewrites[0].Class.From` /
`ClassRewrites[0].Class.To` semantics.

`JniRuntime.JniTypeManager.GetReplacementMethodInfo()` is the key
integration for all other "replacement" semantics.  Given the
source type, source method name, and source JNI signature, it is
responsible for providing an *overriding* target type, target method
name, and target JNI signature.

For `ClassRewrites[0].Methods[0]`, this allows "renaming"
`Activity.onCreate()` to `MAMActivity.onMAMCreate()`:

	var r = JniEnvironment.Runtime.TypeManager.GetReplacementMethodInfo (
	    "android/app/Activity",
	    "onCreate",
	    "(Landroid/os/Bundle;)V"
	);
	// is equivalent to:
	var r = new JniRuntime.ReplacementMethodInfo {
	    SourceJniType                   = "android/app/Activity",   // from input parameter
	    SourceJniMethodName             = "onCreate",               // from input parameter
	    SourceJniMethodSignature        = "(Landroid/os/Bundle;)V", // from input parameter

	    TargetJniType                   = "com/microsoft/intune/mam/client/app/MAMActivity",
	    TargetJniMethodName             = "onMAMCreate",
	    SourceJniMethodSignature        = null,                     // "use original value"
	    TargetJniMethodParameterCount   = null,                     // unchanged
	    TargetJniMethodInstanceToStatic = false,
	}

`ClassRewrites[1].Methods[0]` is "weird", in that it has
`MakeStatic: true`.  The semantics for when `MakeStatic: true` exists
is that the "original" method is an *instance* method, and the target
method is a *`static`* method, *and* the `this` instance now becomes
a *parameter* to the method.  This is likewise supported via
`JniRuntime.JniTypeManager.GetReplacementMethodInfo()`, and is
identified by:

 1. `ReplacementMethodInfo.TargetJniMethodParameterCount` being a
    non-`null` value, and

 2. `ReplacementMethodInfo.TargetJniMethodInstanceToStatic` is true.

Thus, `ClassRewrites[1].Methods[0]` is akin to:

	var r = JniEnvironment.Runtime.TypeManager.GetReplacementMethodInfo (
	    "android/content/pm/PackageManager",
	    "checkPermission",
	    "(Ljava/lang/String;Ljava/lang/String;)I"
	);
	// is equivalent to:
	var r = new JniRuntime.ReplacementMethodInfo {
	    SourceJniType                   = "android/content/pm/PackageManager",              // from input parameter
	    SourceJniMethodName             = "checkPermission",                                // from input parameter
	    SourceJniMethodSignature        = "(Ljava/lang/String;Ljava/lang/String;)I",        // from input parameter

	    TargetJniType                   = "com/microsoft/intune/mam/client/content/pm/MAMPackageManagement",
	    TargetJniMethodName             = "checkPermission",
	    SourceJniMethodSignature        = "(Landroid/content/pm/PackageManager;Ljava/lang/String;Ljava/lang/String;)I",
	    TargetJniMethodParameterCount   = 3,
	    TargetJniMethodInstanceToStatic = true,
	}

(Note that the subclass is responsible for providing this data.)

`ReplacementMethodInfo.TargetJniMethodParameterCount` must be the
number of parameters that the target method accepts.  This is needed
so that `JniPeerMembers.JniInstanceMethods.TryInvoke*StaticRedirect()`
can appropriately reallocate the `JniArgumentValue*` array, so that
the `this` parameter can be added to the beginning of the parameters.

~~ Overhead? ~~

All these extra invocations into `JniRuntime.JniTypeManager` imply
additional overhead to invoke a Java method.  However, this is all
done during the *first* time a method is looked up, after which the
`JniMethodInfo` instance is *cached* for a given
(type, method, signature) tuple.

To demonstrate overheads, `samples/Hello` has been updated to accept
a new `-t` value; when provided, it invokes the
`java.lang.Object.hashCode()` method 1 million times and calculates
the average invocation overhead:

	% dotnet run --project samples/Hello -- -t
	Object.hashCode: 1000000 invocations. Total=00:00:00.5196758; Average=0.0005196758ms

If we replace the .NET 6 `samples/Hello/bin/Debug/Java.Interop.dll`
assembly with e.g. `bin/Debug/Java.Interop.dll`, we can compare to
performance *without* these new changes, as the changes are only in
the .NET 6 build:

	% \cp bin/Debug/Java.Interop{.dll,.pdb} samples/Hello/bin/Debug
	% dotnet samples/Hello/bin/Debug/Hello.dll -t
	Object.hashCode: 1000000 invocations. Total=00:00:00.5386676; Average=0.0005386676ms

There is a fair bit of variation in `dotnet Hello.dll -t` output,
but they're all roughly similar.  There doesn't appear to be
significant overhead for this particular test.

[0]: https://en.wikipedia.org/wiki/Android_Runtime
[1]: https://developer.android.com/studio/write/java8-support
[2]: https://developer.android.com/studio/write/java8-support-table
[3]: https://docs.microsoft.com/en-us/mem/intune/developer/app-sdk-xamarin
[4]: https://www.nuget.org/packages/Microsoft.Intune.MAM.Remapper.Tasks/
jonpryor added a commit to jonpryor/java.interop that referenced this issue Jan 4, 2022
Context: dotnet#867

There are two scenarios for which a "generalized" type & member
remapping solution would be useful:

 1. Desugaring
 2. Microsoft Intune MAM

Note: this new support is only present when targeting .NET 6+,
and will not (currently) be present in Classic Xamarin.Android.

~~ Desugaring ~~

Context: dotnet/android#4574 (comment)
Context: dotnet/android#6142 (comment)

The [Android Runtime][0] is responsible for running the Dalvik
bytecode within e.g. `classes.dex`.  The Android runtime and Dalvik
bytecode formats have apparently changed over time in order to
support newer Java language features, such as support for static
interface methods:

	package org.webrtc;

	public /* partial */ interface EGLBase {
	    public static EGLBase create() { /* … */}
	}

Support for static interface methods was added in Java 8 and in
Android v8.0 (API-26).  If you want to use static interface methods
on an Android device running API-25 or earlier, you must [desugar][1]
the Java Bytecode.  The result of [desugaring][2] the above
`org.webrtc.EGLBase` type is that the static methods are moved to a
*new* type, with a `$-CC` suffix, "as if" the Java code were instead:

	package org.webrtc;

	public /* partial */ interface EGLBase {
	    // …
	}

	public final /* partial */ class EGLBase$-CC {
	    public static EGLBase create { /* … */ }
	}

While this works for pure Java code, this *does not work* with
Xamarin.Android, because our bindings currently expect (require) that
the types & members our binding process detects don't "move":

	// C# Binding for EGLBase
	namespace Org.Webrtc {
	    public partial interface IEGLBase {
	        private static readonly JniPeerMembers _members = new JniPeerMembers ("org/webrtc/EGLBase", typeof (IEGLBase));
	        public static IEGLBase Create()
	        {
	            const string __id = "create.()Lorg/webrtc/EglBase;"
	            var __rm = _members.StaticMethods.InvokeObjectMethod (__id, null);
	            return Java.Lang.Object.GetObject<IEGLBase>(__rm.Handle, JniHandleOwnership.TransferLocalRef);
	        }
	    }
	}

The `new JniPeerMembers(…)` invocation will use `JNIEnv::FindClass()`
to lookup the `org/webrtc/EGLBase` type, and `IEGLBase.Create()` will
use `JNIEnv::GetStaticMethodID()` and `JNIEnv::CallStaticObjectMethod()`
to lookup and invoke the `EGLBase.create()` method.

However, when desugaring is used, *there is no* `EGLBase.create()`
method.  Consequently, in a "desugared" environment,
`Java.Lang.NoSuchMethodError` will be thrown when `IEGLBase.Create()`
is invoked, as `EGLBase.create()` doesn't exist; it "should" instead
be looking for `EGLBase$-CC.create()`!

Introduce partial support for this scenario by adding the new method:

	namespace Java.Interop {
	    public partial class JniRuntime {
	        public partial class JniTypeManager {
	            public IReadOnlyList<string>? GetStaticMethodFallbackTypes (string jniSimpleReference) =>
	                GetStaticMethodFallbackTypesCore (jniSimpleReference);
	            protected virtual IReadOnlyList<string>? GetStaticMethodFallbackTypesCore (string jniSimpleReference) => null;
	        }
	    }
	}

`JniPeerMembers.JniStaticMethods.GetMethodInfo()` will attempt to
lookup the static method, "as normal".  If the method cannot be
found, then `JniRuntime.JniTypeManager.GetStaticMethodFallbackTypes()`
will be called, and we'll attempt to resolve the static method on each
type returned by `GetStaticMethodFallbackTypes()`.
If `GetStaticMethodFallbackTypes()` returns `null` or no fallback type
provides the method, then a `NoSuchMethodError` will be thrown.

TODO: Update xamarin-android to override
`GetStaticMethodFallbackTypes()`, to return
`$"{jniSimpleReference}$-CC"`.

~~ Microsoft Intune MAM ~~

Context: https://www.nuget.org/packages/Microsoft.Intune.MAM.Remapper.Tasks/
Context: https://docs.microsoft.com/en-us/mem/intune/developer/app-sdk-xamarin#remapper

The [Microsoft Intune App SDK Xamarin Bindings][3] is in a
similar-yet-different position: it involves Java & Dalvik Bytecode
manipulation for various security purposes, and in order to make that
work reasonably from Xamarin.Android, they *also* rewrite
Xamarin.Android IL so that it's consistent with the manipulated
Dalvik bytecode.

See `readme.md` and `content/MonoAndroid10/remapping-config.json`
within the [`Microsoft.Intune.MAM.Remapper.Tasks NuGet package`][4]
for some additional details/tidbits such as:

> This task operates on assemblies to replace the base type classes
> which Microsoft Intune requires to implement its SDK (for example,
> Intune requires using a `MAMActivity` in place of `Activity` and
> methods such as `OnMAMCreate` instead of `OnCreate`).

`remapping-config.json` contains JSON fragments such as:

	"ClassRewrites": [
	  {
	    "Class": {
	      "From": "android.app.Activity",
	      "To": "com.microsoft.intune.mam.client.app.MAMActivity"
	    },
	    "Methods": [
	      {
	        "MakeStatic": false,
	        "OriginalName": "onCreate",
	        "NewName": "onMAMCreate",
	        "OriginalParams": [
	          "android.os.Bundle"
	        ]
	      }
	    ]
	  },
	  {
	    "Class": {
	      "From": "android.content.pm.PackageManager",
	      "To": "com.microsoft.intune.mam.client.content.pm.MAMPackageManagement"
	    },
	    "Methods": [
	      {
	        "MakeStatic": true,
	        "OriginalName": "checkPermission",
	      }
	    ]
	  }
	]
	"GlobalMethodCalls": [
	  {
	    "Class": {
	      "From": "android.app.PendingIntent",
	      "To": "com.microsoft.intune.mam.client.app.MAMPendingIntent"
	    },
	    "Methods": [
	      {
	        "MakeStatic": false,
	        "OriginalName": "getActivities"
	      },
	    ]
	  }
	]

While what the InTune team has *works*, it suffers from a number of
"workflow" problems, e.g. incremental builds are problematic (they
might try to rewrite assemblies which were already rewritten), IL
rewriting is *always* "fun", and if we change IL binding patterns,
they'll need to support previous "binding styles" and newer styles.

Introduce partial support for this scenario by adding the following
members to `Java.Interop.JniRuntime.JniTypeManager`:

	namespace Java.Interop {
	    public partial class JniRuntime {

	        public struct ReplacementMethodInfo {
	            public  string? SourceJniType                   {get; set;}
	            public  string? SourceJniMethodName             {get; set;}
	            public  string? SourceJniMethodSignature        {get; set;}
	            public  string? TargetJniType                   {get; set;}
	            public  string? TargetJniMethodName             {get; set;}
	            public  string? TargetJniMethodSignature        {get; set;}
	            public  int?    TargetJniMethodParameterCount   {get; set;}
	            public  bool    TargetJniMethodInstanceToStatic {get; set;}
	        }

	        public partial class JniTypeManager {

	            public string? GetReplacementType (string jniSimpleReference) =>
	                GetReplacementTypeCore (jniSimpleReference);
	            protected virtual string? GetReplacementTypeCore (string jniSimpleReference) => null;

	            public ReplacementMethodInfo? GetReplacementMethodInfo (string jniSimpleReference, string jniMethodName, string jniMethodSignature) =>
	                GetReplacementMethodInfoCore (jniSimpleReference, jniMethodName,jniMethodSignature);
	            protected virtual ReplacementMethodInfo? GetReplacementMethodInfoCore (string jniSimpleReference, string jniMethodName, string jniMethodSignature) => null;
	        }
	    }
	}

These new methods are used by `JniPeerMembers`.

Consider "normal" usage of `JniPeerMembers`, within a binding:

	partial class Activity {
	    static readonly JniPeerMembers _members = new XAPeerMembers (jniPeerTypeName:"android/app/Activity", managedPeerType:typeof (Activity));
	}

`JniRuntime.JniTypeManager.GetReplacementType()` allows "replacing"
the `jniPeerTypeName` value with a "replacement" JNI type name.
The "replacement" type will be used for all field and method lookups.
This allows supporting the `ClassRewrites[0].Class.From` /
`ClassRewrites[0].Class.To` semantics.

`JniRuntime.JniTypeManager.GetReplacementMethodInfo()` is the key
integration for all other "replacement" semantics.  Given the
source type, source method name, and source JNI signature, it is
responsible for providing an *overriding* target type, target method
name, and target JNI signature.

For `ClassRewrites[0].Methods[0]`, this allows "renaming"
`Activity.onCreate()` to `MAMActivity.onMAMCreate()`:

	var r = JniEnvironment.Runtime.TypeManager.GetReplacementMethodInfo (
	    "android/app/Activity",
	    "onCreate",
	    "(Landroid/os/Bundle;)V"
	);
	// is equivalent to:
	var r = new JniRuntime.ReplacementMethodInfo {
	    SourceJniType                   = "android/app/Activity",   // from input parameter
	    SourceJniMethodName             = "onCreate",               // from input parameter
	    SourceJniMethodSignature        = "(Landroid/os/Bundle;)V", // from input parameter

	    TargetJniType                   = "com/microsoft/intune/mam/client/app/MAMActivity",
	    TargetJniMethodName             = "onMAMCreate",
	    SourceJniMethodSignature        = null,                     // "use original value"
	    TargetJniMethodParameterCount   = null,                     // unchanged
	    TargetJniMethodInstanceToStatic = false,
	}

`ClassRewrites[1].Methods[0]` is "weird", in that it has
`MakeStatic: true`.  The semantics for when `MakeStatic: true` exists
is that the "original" method is an *instance* method, and the target
method is a *`static`* method, *and* the `this` instance now becomes
a *parameter* to the method.  This is likewise supported via
`JniRuntime.JniTypeManager.GetReplacementMethodInfo()`, and is
identified by:

 1. `ReplacementMethodInfo.TargetJniMethodParameterCount` being a
    non-`null` value, and

 2. `ReplacementMethodInfo.TargetJniMethodInstanceToStatic` is true.

Thus, `ClassRewrites[1].Methods[0]` is akin to:

	var r = JniEnvironment.Runtime.TypeManager.GetReplacementMethodInfo (
	    "android/content/pm/PackageManager",
	    "checkPermission",
	    "(Ljava/lang/String;Ljava/lang/String;)I"
	);
	// is equivalent to:
	var r = new JniRuntime.ReplacementMethodInfo {
	    SourceJniType                   = "android/content/pm/PackageManager",              // from input parameter
	    SourceJniMethodName             = "checkPermission",                                // from input parameter
	    SourceJniMethodSignature        = "(Ljava/lang/String;Ljava/lang/String;)I",        // from input parameter

	    TargetJniType                   = "com/microsoft/intune/mam/client/content/pm/MAMPackageManagement",
	    TargetJniMethodName             = "checkPermission",
	    SourceJniMethodSignature        = "(Landroid/content/pm/PackageManager;Ljava/lang/String;Ljava/lang/String;)I",
	    TargetJniMethodParameterCount   = 3,
	    TargetJniMethodInstanceToStatic = true,
	}

(Note that the subclass is responsible for providing this data.)

`ReplacementMethodInfo.TargetJniMethodParameterCount` must be the
number of parameters that the target method accepts.  This is needed
so that `JniPeerMembers.JniInstanceMethods.TryInvoke*StaticRedirect()`
can appropriately reallocate the `JniArgumentValue*` array, so that
the `this` parameter can be added to the beginning of the parameters.

~~ Overhead? ~~

All these extra invocations into `JniRuntime.JniTypeManager` imply
additional overhead to invoke a Java method.  However, this is all
done during the *first* time a method is looked up, after which the
`JniMethodInfo` instance is *cached* for a given
(type, method, signature) tuple.

To demonstrate overheads, `samples/Hello` has been updated to accept
a new `-t` value; when provided, it invokes the
`java.lang.Object.hashCode()` method 1 million times and calculates
the average invocation overhead:

	% dotnet run --project samples/Hello -- -t
	Object.hashCode: 1000000 invocations. Total=00:00:00.5196758; Average=0.0005196758ms

If we replace the .NET 6 `samples/Hello/bin/Debug/Java.Interop.dll`
assembly with e.g. `bin/Debug/Java.Interop.dll`, we can compare to
performance *without* these new changes, as the changes are only in
the .NET 6 build:

	% \cp bin/Debug/Java.Interop{.dll,.pdb} samples/Hello/bin/Debug
	% dotnet samples/Hello/bin/Debug/Hello.dll -t
	Object.hashCode: 1000000 invocations. Total=00:00:00.5386676; Average=0.0005386676ms

There is a fair bit of variation in `dotnet Hello.dll -t` output,
but they're all roughly similar.  There doesn't appear to be
significant overhead for this particular test.

[0]: https://en.wikipedia.org/wiki/Android_Runtime
[1]: https://developer.android.com/studio/write/java8-support
[2]: https://developer.android.com/studio/write/java8-support-table
[3]: https://docs.microsoft.com/en-us/mem/intune/developer/app-sdk-xamarin
[4]: https://www.nuget.org/packages/Microsoft.Intune.MAM.Remapper.Tasks/
jonpryor added a commit to jonpryor/java.interop that referenced this issue Jan 4, 2022
Context: dotnet#867

There are two scenarios for which a "generalized" type & member
remapping solution would be useful:

 1. Desugaring
 2. Microsoft Intune MAM

Note: this new support is only present when targeting .NET 6+,
and will not (currently) be present in Classic Xamarin.Android.

~~ Desugaring ~~

Context: dotnet/android#4574 (comment)
Context: dotnet/android#6142 (comment)

The [Android Runtime][0] is responsible for running the Dalvik
bytecode within e.g. `classes.dex`.  The Android runtime and Dalvik
bytecode formats have apparently changed over time in order to
support newer Java language features, such as support for static
interface methods:

	package org.webrtc;

	public /* partial */ interface EGLBase {
	    public static EGLBase create() { /* … */}
	}

Support for static interface methods was added in Java 8 and in
Android v8.0 (API-26).  If you want to use static interface methods
on an Android device running API-25 or earlier, you must [desugar][1]
the Java Bytecode.  The result of [desugaring][2] the above
`org.webrtc.EGLBase` type is that the static methods are moved to a
*new* type, with a `$-CC` suffix, "as if" the Java code were instead:

	package org.webrtc;

	public /* partial */ interface EGLBase {
	    // …
	}

	public final /* partial */ class EGLBase$-CC {
	    public static EGLBase create { /* … */ }
	}

While this works for pure Java code, this *does not work* with
Xamarin.Android, because our bindings currently expect (require) that
the types & members our binding process detects don't "move":

	// C# Binding for EGLBase
	namespace Org.Webrtc {
	    public partial interface IEGLBase {
	        private static readonly JniPeerMembers _members = new JniPeerMembers ("org/webrtc/EGLBase", typeof (IEGLBase));
	        public static IEGLBase Create()
	        {
	            const string __id = "create.()Lorg/webrtc/EglBase;"
	            var __rm = _members.StaticMethods.InvokeObjectMethod (__id, null);
	            return Java.Lang.Object.GetObject<IEGLBase>(__rm.Handle, JniHandleOwnership.TransferLocalRef);
	        }
	    }
	}

The `new JniPeerMembers(…)` invocation will use `JNIEnv::FindClass()`
to lookup the `org/webrtc/EGLBase` type, and `IEGLBase.Create()` will
use `JNIEnv::GetStaticMethodID()` and `JNIEnv::CallStaticObjectMethod()`
to lookup and invoke the `EGLBase.create()` method.

However, when desugaring is used, *there is no* `EGLBase.create()`
method.  Consequently, in a "desugared" environment,
`Java.Lang.NoSuchMethodError` will be thrown when `IEGLBase.Create()`
is invoked, as `EGLBase.create()` doesn't exist; it "should" instead
be looking for `EGLBase$-CC.create()`!

Introduce partial support for this scenario by adding the new method:

	namespace Java.Interop {
	    public partial class JniRuntime {
	        public partial class JniTypeManager {
	            public IReadOnlyList<string>? GetStaticMethodFallbackTypes (string jniSimpleReference) =>
	                GetStaticMethodFallbackTypesCore (jniSimpleReference);
	            protected virtual IReadOnlyList<string>? GetStaticMethodFallbackTypesCore (string jniSimpleReference) => null;
	        }
	    }
	}

`JniPeerMembers.JniStaticMethods.GetMethodInfo()` will attempt to
lookup the static method, "as normal".  If the method cannot be
found, then `JniRuntime.JniTypeManager.GetStaticMethodFallbackTypes()`
will be called, and we'll attempt to resolve the static method on each
type returned by `GetStaticMethodFallbackTypes()`.
If `GetStaticMethodFallbackTypes()` returns `null` or no fallback type
provides the method, then a `NoSuchMethodError` will be thrown.

TODO: Update xamarin-android to override
`GetStaticMethodFallbackTypes()`, to return
`$"{jniSimpleReference}$-CC"`.

~~ Microsoft Intune MAM ~~

Context: https://www.nuget.org/packages/Microsoft.Intune.MAM.Remapper.Tasks/
Context: https://docs.microsoft.com/en-us/mem/intune/developer/app-sdk-xamarin#remapper

The [Microsoft Intune App SDK Xamarin Bindings][3] is in a
similar-yet-different position: it involves Java & Dalvik Bytecode
manipulation for various security purposes, and in order to make that
work reasonably from Xamarin.Android, they *also* rewrite
Xamarin.Android IL so that it's consistent with the manipulated
Dalvik bytecode.

See `readme.md` and `content/MonoAndroid10/remapping-config.json`
within the [`Microsoft.Intune.MAM.Remapper.Tasks NuGet package`][4]
for some additional details/tidbits such as:

> This task operates on assemblies to replace the base type classes
> which Microsoft Intune requires to implement its SDK (for example,
> Intune requires using a `MAMActivity` in place of `Activity` and
> methods such as `OnMAMCreate` instead of `OnCreate`).

`remapping-config.json` contains JSON fragments such as:

	"ClassRewrites": [
	  {
	    "Class": {
	      "From": "android.app.Activity",
	      "To": "com.microsoft.intune.mam.client.app.MAMActivity"
	    },
	    "Methods": [
	      {
	        "MakeStatic": false,
	        "OriginalName": "onCreate",
	        "NewName": "onMAMCreate",
	        "OriginalParams": [
	          "android.os.Bundle"
	        ]
	      }
	    ]
	  },
	  {
	    "Class": {
	      "From": "android.content.pm.PackageManager",
	      "To": "com.microsoft.intune.mam.client.content.pm.MAMPackageManagement"
	    },
	    "Methods": [
	      {
	        "MakeStatic": true,
	        "OriginalName": "checkPermission",
	      }
	    ]
	  }
	]
	"GlobalMethodCalls": [
	  {
	    "Class": {
	      "From": "android.app.PendingIntent",
	      "To": "com.microsoft.intune.mam.client.app.MAMPendingIntent"
	    },
	    "Methods": [
	      {
	        "MakeStatic": false,
	        "OriginalName": "getActivities"
	      },
	    ]
	  }
	]

While what the InTune team has *works*, it suffers from a number of
"workflow" problems, e.g. incremental builds are problematic (they
might try to rewrite assemblies which were already rewritten), IL
rewriting is *always* "fun", and if we change IL binding patterns,
they'll need to support previous "binding styles" and newer styles.

Introduce partial support for this scenario by adding the following
members to `Java.Interop.JniRuntime.JniTypeManager`:

	namespace Java.Interop {
	    public partial class JniRuntime {

	        public struct ReplacementMethodInfo {
	            public  string? SourceJniType                   {get; set;}
	            public  string? SourceJniMethodName             {get; set;}
	            public  string? SourceJniMethodSignature        {get; set;}
	            public  string? TargetJniType                   {get; set;}
	            public  string? TargetJniMethodName             {get; set;}
	            public  string? TargetJniMethodSignature        {get; set;}
	            public  int?    TargetJniMethodParameterCount   {get; set;}
	            public  bool    TargetJniMethodInstanceToStatic {get; set;}
	        }

	        public partial class JniTypeManager {

	            public string? GetReplacementType (string jniSimpleReference) =>
	                GetReplacementTypeCore (jniSimpleReference);
	            protected virtual string? GetReplacementTypeCore (string jniSimpleReference) => null;

	            public ReplacementMethodInfo? GetReplacementMethodInfo (string jniSimpleReference, string jniMethodName, string jniMethodSignature) =>
	                GetReplacementMethodInfoCore (jniSimpleReference, jniMethodName,jniMethodSignature);
	            protected virtual ReplacementMethodInfo? GetReplacementMethodInfoCore (string jniSimpleReference, string jniMethodName, string jniMethodSignature) => null;
	        }
	    }
	}

These new methods are used by `JniPeerMembers`.

Consider "normal" usage of `JniPeerMembers`, within a binding:

	partial class Activity {
	    static readonly JniPeerMembers _members = new XAPeerMembers (jniPeerTypeName:"android/app/Activity", managedPeerType:typeof (Activity));
	}

`JniRuntime.JniTypeManager.GetReplacementType()` allows "replacing"
the `jniPeerTypeName` value with a "replacement" JNI type name.
The "replacement" type will be used for all field and method lookups.
This allows supporting the `ClassRewrites[0].Class.From` /
`ClassRewrites[0].Class.To` semantics.

`JniRuntime.JniTypeManager.GetReplacementMethodInfo()` is the key
integration for all other "replacement" semantics.  Given the
source type, source method name, and source JNI signature, it is
responsible for providing an *overriding* target type, target method
name, and target JNI signature.

For `ClassRewrites[0].Methods[0]`, this allows "renaming"
`Activity.onCreate()` to `MAMActivity.onMAMCreate()`:

	var r = JniEnvironment.Runtime.TypeManager.GetReplacementMethodInfo (
	    "android/app/Activity",
	    "onCreate",
	    "(Landroid/os/Bundle;)V"
	);
	// is equivalent to:
	var r = new JniRuntime.ReplacementMethodInfo {
	    SourceJniType                   = "android/app/Activity",   // from input parameter
	    SourceJniMethodName             = "onCreate",               // from input parameter
	    SourceJniMethodSignature        = "(Landroid/os/Bundle;)V", // from input parameter

	    TargetJniType                   = "com/microsoft/intune/mam/client/app/MAMActivity",
	    TargetJniMethodName             = "onMAMCreate",
	    SourceJniMethodSignature        = null,                     // "use original value"
	    TargetJniMethodParameterCount   = null,                     // unchanged
	    TargetJniMethodInstanceToStatic = false,
	}

`ClassRewrites[1].Methods[0]` is "weird", in that it has
`MakeStatic: true`.  The semantics for when `MakeStatic: true` exists
is that the "original" method is an *instance* method, and the target
method is a *`static`* method, *and* the `this` instance now becomes
a *parameter* to the method.  This is likewise supported via
`JniRuntime.JniTypeManager.GetReplacementMethodInfo()`, and is
identified by:

 1. `ReplacementMethodInfo.TargetJniMethodParameterCount` being a
    non-`null` value, and

 2. `ReplacementMethodInfo.TargetJniMethodInstanceToStatic` is true.

Thus, `ClassRewrites[1].Methods[0]` is akin to:

	var r = JniEnvironment.Runtime.TypeManager.GetReplacementMethodInfo (
	    "android/content/pm/PackageManager",
	    "checkPermission",
	    "(Ljava/lang/String;Ljava/lang/String;)I"
	);
	// is equivalent to:
	var r = new JniRuntime.ReplacementMethodInfo {
	    SourceJniType                   = "android/content/pm/PackageManager",              // from input parameter
	    SourceJniMethodName             = "checkPermission",                                // from input parameter
	    SourceJniMethodSignature        = "(Ljava/lang/String;Ljava/lang/String;)I",        // from input parameter

	    TargetJniType                   = "com/microsoft/intune/mam/client/content/pm/MAMPackageManagement",
	    TargetJniMethodName             = "checkPermission",
	    SourceJniMethodSignature        = "(Landroid/content/pm/PackageManager;Ljava/lang/String;Ljava/lang/String;)I",
	    TargetJniMethodParameterCount   = 3,
	    TargetJniMethodInstanceToStatic = true,
	}

(Note that the subclass is responsible for providing this data.)

`ReplacementMethodInfo.TargetJniMethodParameterCount` must be the
number of parameters that the target method accepts.  This is needed
so that `JniPeerMembers.JniInstanceMethods.TryInvoke*StaticRedirect()`
can appropriately reallocate the `JniArgumentValue*` array, so that
the `this` parameter can be added to the beginning of the parameters.

~~ Overhead? ~~

All these extra invocations into `JniRuntime.JniTypeManager` imply
additional overhead to invoke a Java method.  However, this is all
done during the *first* time a method is looked up, after which the
`JniMethodInfo` instance is *cached* for a given
(type, method, signature) tuple.

To demonstrate overheads, `samples/Hello` has been updated to accept
a new `-t` value; when provided, it invokes the
`java.lang.Object.hashCode()` method 1 million times and calculates
the average invocation overhead:

	% dotnet run --project samples/Hello -- -t
	Object.hashCode: 1000000 invocations. Total=00:00:00.5196758; Average=0.0005196758ms

If we replace the .NET 6 `samples/Hello/bin/Debug/Java.Interop.dll`
assembly with e.g. `bin/Debug/Java.Interop.dll`, we can compare to
performance *without* these new changes, as the changes are only in
the .NET 6 build:

	% \cp bin/Debug/Java.Interop{.dll,.pdb} samples/Hello/bin/Debug
	% dotnet samples/Hello/bin/Debug/Hello.dll -t
	Object.hashCode: 1000000 invocations. Total=00:00:00.5386676; Average=0.0005386676ms

There is a fair bit of variation in `dotnet Hello.dll -t` output,
but they're all roughly similar.  There doesn't appear to be
significant overhead for this particular test.

[0]: https://en.wikipedia.org/wiki/Android_Runtime
[1]: https://developer.android.com/studio/write/java8-support
[2]: https://developer.android.com/studio/write/java8-support-table
[3]: https://docs.microsoft.com/en-us/mem/intune/developer/app-sdk-xamarin
[4]: https://www.nuget.org/packages/Microsoft.Intune.MAM.Remapper.Tasks/
jonpryor added a commit to jonpryor/java.interop that referenced this issue Feb 2, 2022
Context: dotnet#867

There are two scenarios for which a "generalized" type & member
remapping solution would be useful:

 1. Desugaring
 2. Microsoft Intune MAM

Note: this new support is only present when targeting .NET 6+,
and will not (currently) be present in Classic Xamarin.Android.

~~ Desugaring ~~

Context: dotnet/android#4574 (comment)
Context: dotnet/android#6142 (comment)

The [Android Runtime][0] is responsible for running the Dalvik
bytecode within e.g. `classes.dex`.  The Android runtime and Dalvik
bytecode formats have apparently changed over time in order to
support newer Java language features, such as support for static
interface methods:

	package org.webrtc;

	public /* partial */ interface EGLBase {
	    public static EGLBase create() { /* … */}
	}

Support for static interface methods was added in Java 8 and in
Android v8.0 (API-26).  If you want to use static interface methods
on an Android device running API-25 or earlier, you must [desugar][1]
the Java Bytecode.  The result of [desugaring][2] the above
`org.webrtc.EGLBase` type is that the static methods are moved to a
*new* type, with a `$-CC` suffix, "as if" the Java code were instead:

	package org.webrtc;

	public /* partial */ interface EGLBase {
	    // …
	}

	public final /* partial */ class EGLBase$-CC {
	    public static EGLBase create { /* … */ }
	}

While this works for pure Java code, this *does not work* with
Xamarin.Android, because our bindings currently expect (require) that
the types & members our binding process detects don't "move":

	// C# Binding for EGLBase
	namespace Org.Webrtc {
	    public partial interface IEGLBase {
	        private static readonly JniPeerMembers _members = new JniPeerMembers ("org/webrtc/EGLBase", typeof (IEGLBase));
	        public static IEGLBase Create()
	        {
	            const string __id = "create.()Lorg/webrtc/EglBase;"
	            var __rm = _members.StaticMethods.InvokeObjectMethod (__id, null);
	            return Java.Lang.Object.GetObject<IEGLBase>(__rm.Handle, JniHandleOwnership.TransferLocalRef);
	        }
	    }
	}

The `new JniPeerMembers(…)` invocation will use `JNIEnv::FindClass()`
to lookup the `org/webrtc/EGLBase` type, and `IEGLBase.Create()` will
use `JNIEnv::GetStaticMethodID()` and `JNIEnv::CallStaticObjectMethod()`
to lookup and invoke the `EGLBase.create()` method.

However, when desugaring is used, *there is no* `EGLBase.create()`
method.  Consequently, in a "desugared" environment,
`Java.Lang.NoSuchMethodError` will be thrown when `IEGLBase.Create()`
is invoked, as `EGLBase.create()` doesn't exist; it "should" instead
be looking for `EGLBase$-CC.create()`!

Introduce partial support for this scenario by adding the new method:

	namespace Java.Interop {
	    public partial class JniRuntime {
	        public partial class JniTypeManager {
	            public IReadOnlyList<string>? GetStaticMethodFallbackTypes (string jniSimpleReference) =>
	                GetStaticMethodFallbackTypesCore (jniSimpleReference);
	            protected virtual IReadOnlyList<string>? GetStaticMethodFallbackTypesCore (string jniSimpleReference) => null;
	        }
	    }
	}

`JniPeerMembers.JniStaticMethods.GetMethodInfo()` will attempt to
lookup the static method, "as normal".  If the method cannot be
found, then `JniRuntime.JniTypeManager.GetStaticMethodFallbackTypes()`
will be called, and we'll attempt to resolve the static method on each
type returned by `GetStaticMethodFallbackTypes()`.
If `GetStaticMethodFallbackTypes()` returns `null` or no fallback type
provides the method, then a `NoSuchMethodError` will be thrown.

TODO: Update xamarin-android to override
`GetStaticMethodFallbackTypes()`, to return
`$"{jniSimpleReference}$-CC"`.

~~ Microsoft Intune MAM ~~

Context: https://www.nuget.org/packages/Microsoft.Intune.MAM.Remapper.Tasks/
Context: https://docs.microsoft.com/en-us/mem/intune/developer/app-sdk-xamarin#remapper

The [Microsoft Intune App SDK Xamarin Bindings][3] is in a
similar-yet-different position: it involves Java & Dalvik Bytecode
manipulation for various security purposes, and in order to make that
work reasonably from Xamarin.Android, they *also* rewrite
Xamarin.Android IL so that it's consistent with the manipulated
Dalvik bytecode.

See `readme.md` and `content/MonoAndroid10/remapping-config.json`
within the [`Microsoft.Intune.MAM.Remapper.Tasks NuGet package`][4]
for some additional details/tidbits such as:

> This task operates on assemblies to replace the base type classes
> which Microsoft Intune requires to implement its SDK (for example,
> Intune requires using a `MAMActivity` in place of `Activity` and
> methods such as `OnMAMCreate` instead of `OnCreate`).

`remapping-config.json` contains JSON fragments such as:

	"ClassRewrites": [
	  {
	    "Class": {
	      "From": "android.app.Activity",
	      "To": "com.microsoft.intune.mam.client.app.MAMActivity"
	    },
	    "Methods": [
	      {
	        "MakeStatic": false,
	        "OriginalName": "onCreate",
	        "NewName": "onMAMCreate",
	        "OriginalParams": [
	          "android.os.Bundle"
	        ]
	      }
	    ]
	  },
	  {
	    "Class": {
	      "From": "android.content.pm.PackageManager",
	      "To": "com.microsoft.intune.mam.client.content.pm.MAMPackageManagement"
	    },
	    "Methods": [
	      {
	        "MakeStatic": true,
	        "OriginalName": "checkPermission",
	      }
	    ]
	  }
	]
	"GlobalMethodCalls": [
	  {
	    "Class": {
	      "From": "android.app.PendingIntent",
	      "To": "com.microsoft.intune.mam.client.app.MAMPendingIntent"
	    },
	    "Methods": [
	      {
	        "MakeStatic": false,
	        "OriginalName": "getActivities"
	      },
	    ]
	  }
	]

While what the InTune team has *works*, it suffers from a number of
"workflow" problems, e.g. incremental builds are problematic (they
might try to rewrite assemblies which were already rewritten), IL
rewriting is *always* "fun", and if we change IL binding patterns,
they'll need to support previous "binding styles" and newer styles.

Introduce partial support for this scenario by adding the following
members to `Java.Interop.JniRuntime.JniTypeManager`:

	namespace Java.Interop {
	    public partial class JniRuntime {

	        public struct ReplacementMethodInfo {
	            public  string? SourceJniType                   {get; set;}
	            public  string? SourceJniMethodName             {get; set;}
	            public  string? SourceJniMethodSignature        {get; set;}
	            public  string? TargetJniType                   {get; set;}
	            public  string? TargetJniMethodName             {get; set;}
	            public  string? TargetJniMethodSignature        {get; set;}
	            public  int?    TargetJniMethodParameterCount   {get; set;}
	            public  bool    TargetJniMethodInstanceToStatic {get; set;}
	        }

	        public partial class JniTypeManager {

	            public string? GetReplacementType (string jniSimpleReference) =>
	                GetReplacementTypeCore (jniSimpleReference);
	            protected virtual string? GetReplacementTypeCore (string jniSimpleReference) => null;

	            public ReplacementMethodInfo? GetReplacementMethodInfo (string jniSimpleReference, string jniMethodName, string jniMethodSignature) =>
	                GetReplacementMethodInfoCore (jniSimpleReference, jniMethodName,jniMethodSignature);
	            protected virtual ReplacementMethodInfo? GetReplacementMethodInfoCore (string jniSimpleReference, string jniMethodName, string jniMethodSignature) => null;
	        }
	    }
	}

These new methods are used by `JniPeerMembers`.

Consider "normal" usage of `JniPeerMembers`, within a binding:

	partial class Activity {
	    static readonly JniPeerMembers _members = new XAPeerMembers (jniPeerTypeName:"android/app/Activity", managedPeerType:typeof (Activity));
	}

`JniRuntime.JniTypeManager.GetReplacementType()` allows "replacing"
the `jniPeerTypeName` value with a "replacement" JNI type name.
The "replacement" type will be used for all field and method lookups.
This allows supporting the `ClassRewrites[0].Class.From` /
`ClassRewrites[0].Class.To` semantics.

`JniRuntime.JniTypeManager.GetReplacementMethodInfo()` is the key
integration for all other "replacement" semantics.  Given the
source type, source method name, and source JNI signature, it is
responsible for providing an *overriding* target type, target method
name, and target JNI signature.

For `ClassRewrites[0].Methods[0]`, this allows "renaming"
`Activity.onCreate()` to `MAMActivity.onMAMCreate()`:

	var r = JniEnvironment.Runtime.TypeManager.GetReplacementMethodInfo (
	    "android/app/Activity",
	    "onCreate",
	    "(Landroid/os/Bundle;)V"
	);
	// is equivalent to:
	var r = new JniRuntime.ReplacementMethodInfo {
	    SourceJniType                   = "android/app/Activity",   // from input parameter
	    SourceJniMethodName             = "onCreate",               // from input parameter
	    SourceJniMethodSignature        = "(Landroid/os/Bundle;)V", // from input parameter

	    TargetJniType                   = "com/microsoft/intune/mam/client/app/MAMActivity",
	    TargetJniMethodName             = "onMAMCreate",
	    SourceJniMethodSignature        = null,                     // "use original value"
	    TargetJniMethodParameterCount   = null,                     // unchanged
	    TargetJniMethodInstanceToStatic = false,
	}

`ClassRewrites[1].Methods[0]` is "weird", in that it has
`MakeStatic: true`.  The semantics for when `MakeStatic: true` exists
is that the "original" method is an *instance* method, and the target
method is a *`static`* method, *and* the `this` instance now becomes
a *parameter* to the method.  This is likewise supported via
`JniRuntime.JniTypeManager.GetReplacementMethodInfo()`, and is
identified by:

 1. `ReplacementMethodInfo.TargetJniMethodParameterCount` being a
    non-`null` value, and

 2. `ReplacementMethodInfo.TargetJniMethodInstanceToStatic` is true.

Thus, `ClassRewrites[1].Methods[0]` is akin to:

	var r = JniEnvironment.Runtime.TypeManager.GetReplacementMethodInfo (
	    "android/content/pm/PackageManager",
	    "checkPermission",
	    "(Ljava/lang/String;Ljava/lang/String;)I"
	);
	// is equivalent to:
	var r = new JniRuntime.ReplacementMethodInfo {
	    SourceJniType                   = "android/content/pm/PackageManager",              // from input parameter
	    SourceJniMethodName             = "checkPermission",                                // from input parameter
	    SourceJniMethodSignature        = "(Ljava/lang/String;Ljava/lang/String;)I",        // from input parameter

	    TargetJniType                   = "com/microsoft/intune/mam/client/content/pm/MAMPackageManagement",
	    TargetJniMethodName             = "checkPermission",
	    SourceJniMethodSignature        = "(Landroid/content/pm/PackageManager;Ljava/lang/String;Ljava/lang/String;)I",
	    TargetJniMethodParameterCount   = 3,
	    TargetJniMethodInstanceToStatic = true,
	}

(Note that the subclass is responsible for providing this data.)

`ReplacementMethodInfo.TargetJniMethodParameterCount` must be the
number of parameters that the target method accepts.  This is needed
so that `JniPeerMembers.JniInstanceMethods.TryInvoke*StaticRedirect()`
can appropriately reallocate the `JniArgumentValue*` array, so that
the `this` parameter can be added to the beginning of the parameters.

~~ Overhead? ~~

All these extra invocations into `JniRuntime.JniTypeManager` imply
additional overhead to invoke a Java method.  However, this is all
done during the *first* time a method is looked up, after which the
`JniMethodInfo` instance is *cached* for a given
(type, method, signature) tuple.

To demonstrate overheads, `samples/Hello` has been updated to accept
a new `-t` value; when provided, it invokes the
`java.lang.Object.hashCode()` method 1 million times and calculates
the average invocation overhead:

	% dotnet run --project samples/Hello -- -t
	Object.hashCode: 1000000 invocations. Total=00:00:00.5196758; Average=0.0005196758ms

If we replace the .NET 6 `samples/Hello/bin/Debug/Java.Interop.dll`
assembly with e.g. `bin/Debug/Java.Interop.dll`, we can compare to
performance *without* these new changes, as the changes are only in
the .NET 6 build:

	% \cp bin/Debug/Java.Interop{.dll,.pdb} samples/Hello/bin/Debug
	% dotnet samples/Hello/bin/Debug/Hello.dll -t
	Object.hashCode: 1000000 invocations. Total=00:00:00.5386676; Average=0.0005386676ms

There is a fair bit of variation in `dotnet Hello.dll -t` output,
but they're all roughly similar.  There doesn't appear to be
significant overhead for this particular test.

[0]: https://en.wikipedia.org/wiki/Android_Runtime
[1]: https://developer.android.com/studio/write/java8-support
[2]: https://developer.android.com/studio/write/java8-support-table
[3]: https://docs.microsoft.com/en-us/mem/intune/developer/app-sdk-xamarin
[4]: https://www.nuget.org/packages/Microsoft.Intune.MAM.Remapper.Tasks/
jonpryor added a commit to jonpryor/java.interop that referenced this issue May 4, 2022
Context: dotnet#867

There are two scenarios for which a "generalized" type & member
remapping solution would be useful:

 1. Desugaring
 2. Microsoft Intune MAM

Note: this new support is only present when targeting .NET 6+,
and will not (currently) be present in Classic Xamarin.Android.

~~ Desugaring ~~

Context: dotnet/android#4574 (comment)
Context: dotnet/android#6142 (comment)

The [Android Runtime][0] is responsible for running the Dalvik
bytecode within e.g. `classes.dex`.  The Android runtime and Dalvik
bytecode formats have apparently changed over time in order to
support newer Java language features, such as support for static
interface methods:

	package org.webrtc;

	public /* partial */ interface EGLBase {
	    public static EGLBase create() { /* … */}
	}

Support for static interface methods was added in Java 8 and in
Android v8.0 (API-26).  If you want to use static interface methods
on an Android device running API-25 or earlier, you must [desugar][1]
the Java Bytecode.  The result of [desugaring][2] the above
`org.webrtc.EGLBase` type is that the static methods are moved to a
*new* type, with a `$-CC` suffix, "as if" the Java code were instead:

	package org.webrtc;

	public /* partial */ interface EGLBase {
	    // …
	}

	public final /* partial */ class EGLBase$-CC {
	    public static EGLBase create { /* … */ }
	}

While this works for pure Java code, this *does not work* with
Xamarin.Android, because our bindings currently expect (require) that
the types & members our binding process detects don't "move":

	// C# Binding for EGLBase
	namespace Org.Webrtc {
	    public partial interface IEGLBase {
	        private static readonly JniPeerMembers _members = new JniPeerMembers ("org/webrtc/EGLBase", typeof (IEGLBase));
	        public static IEGLBase Create()
	        {
	            const string __id = "create.()Lorg/webrtc/EglBase;"
	            var __rm = _members.StaticMethods.InvokeObjectMethod (__id, null);
	            return Java.Lang.Object.GetObject<IEGLBase>(__rm.Handle, JniHandleOwnership.TransferLocalRef);
	        }
	    }
	}

The `new JniPeerMembers(…)` invocation will use `JNIEnv::FindClass()`
to lookup the `org/webrtc/EGLBase` type, and `IEGLBase.Create()` will
use `JNIEnv::GetStaticMethodID()` and `JNIEnv::CallStaticObjectMethod()`
to lookup and invoke the `EGLBase.create()` method.

However, when desugaring is used, *there is no* `EGLBase.create()`
method.  Consequently, in a "desugared" environment,
`Java.Lang.NoSuchMethodError` will be thrown when `IEGLBase.Create()`
is invoked, as `EGLBase.create()` doesn't exist; it "should" instead
be looking for `EGLBase$-CC.create()`!

Introduce partial support for this scenario by adding the new method:

	namespace Java.Interop {
	    public partial class JniRuntime {
	        public partial class JniTypeManager {
	            public IReadOnlyList<string>? GetStaticMethodFallbackTypes (string jniSimpleReference) =>
	                GetStaticMethodFallbackTypesCore (jniSimpleReference);
	            protected virtual IReadOnlyList<string>? GetStaticMethodFallbackTypesCore (string jniSimpleReference) => null;
	        }
	    }
	}

`JniPeerMembers.JniStaticMethods.GetMethodInfo()` will attempt to
lookup the static method, "as normal".  If the method cannot be
found, then `JniRuntime.JniTypeManager.GetStaticMethodFallbackTypes()`
will be called, and we'll attempt to resolve the static method on each
type returned by `GetStaticMethodFallbackTypes()`.
If `GetStaticMethodFallbackTypes()` returns `null` or no fallback type
provides the method, then a `NoSuchMethodError` will be thrown.

TODO: Update xamarin-android to override
`GetStaticMethodFallbackTypes()`, to return
`$"{jniSimpleReference}$-CC"`.

~~ Microsoft Intune MAM ~~

Context: https://www.nuget.org/packages/Microsoft.Intune.MAM.Remapper.Tasks/
Context: https://docs.microsoft.com/en-us/mem/intune/developer/app-sdk-xamarin#remapper

The [Microsoft Intune App SDK Xamarin Bindings][3] is in a
similar-yet-different position: it involves Java & Dalvik Bytecode
manipulation for various security purposes, and in order to make that
work reasonably from Xamarin.Android, they *also* rewrite
Xamarin.Android IL so that it's consistent with the manipulated
Dalvik bytecode.

See `readme.md` and `content/MonoAndroid10/remapping-config.json`
within the [`Microsoft.Intune.MAM.Remapper.Tasks NuGet package`][4]
for some additional details/tidbits such as:

> This task operates on assemblies to replace the base type classes
> which Microsoft Intune requires to implement its SDK (for example,
> Intune requires using a `MAMActivity` in place of `Activity` and
> methods such as `OnMAMCreate` instead of `OnCreate`).

`remapping-config.json` contains JSON fragments such as:

	"ClassRewrites": [
	  {
	    "Class": {
	      "From": "android.app.Activity",
	      "To": "com.microsoft.intune.mam.client.app.MAMActivity"
	    },
	    "Methods": [
	      {
	        "MakeStatic": false,
	        "OriginalName": "onCreate",
	        "NewName": "onMAMCreate",
	        "OriginalParams": [
	          "android.os.Bundle"
	        ]
	      }
	    ]
	  },
	  {
	    "Class": {
	      "From": "android.content.pm.PackageManager",
	      "To": "com.microsoft.intune.mam.client.content.pm.MAMPackageManagement"
	    },
	    "Methods": [
	      {
	        "MakeStatic": true,
	        "OriginalName": "checkPermission",
	      }
	    ]
	  }
	]
	"GlobalMethodCalls": [
	  {
	    "Class": {
	      "From": "android.app.PendingIntent",
	      "To": "com.microsoft.intune.mam.client.app.MAMPendingIntent"
	    },
	    "Methods": [
	      {
	        "MakeStatic": false,
	        "OriginalName": "getActivities"
	      },
	    ]
	  }
	]

While what the InTune team has *works*, it suffers from a number of
"workflow" problems, e.g. incremental builds are problematic (they
might try to rewrite assemblies which were already rewritten), IL
rewriting is *always* "fun", and if we change IL binding patterns,
they'll need to support previous "binding styles" and newer styles.

Introduce partial support for this scenario by adding the following
members to `Java.Interop.JniRuntime.JniTypeManager`:

	namespace Java.Interop {
	    public partial class JniRuntime {

	        public struct ReplacementMethodInfo {
	            public  string? SourceJniType                   {get; set;}
	            public  string? SourceJniMethodName             {get; set;}
	            public  string? SourceJniMethodSignature        {get; set;}
	            public  string? TargetJniType                   {get; set;}
	            public  string? TargetJniMethodName             {get; set;}
	            public  string? TargetJniMethodSignature        {get; set;}
	            public  int?    TargetJniMethodParameterCount   {get; set;}
	            public  bool    TargetJniMethodInstanceToStatic {get; set;}
	        }

	        public partial class JniTypeManager {

	            public string? GetReplacementType (string jniSimpleReference) =>
	                GetReplacementTypeCore (jniSimpleReference);
	            protected virtual string? GetReplacementTypeCore (string jniSimpleReference) => null;

	            public ReplacementMethodInfo? GetReplacementMethodInfo (string jniSimpleReference, string jniMethodName, string jniMethodSignature) =>
	                GetReplacementMethodInfoCore (jniSimpleReference, jniMethodName,jniMethodSignature);
	            protected virtual ReplacementMethodInfo? GetReplacementMethodInfoCore (string jniSimpleReference, string jniMethodName, string jniMethodSignature) => null;
	        }
	    }
	}

These new methods are used by `JniPeerMembers`.

Consider "normal" usage of `JniPeerMembers`, within a binding:

	partial class Activity {
	    static readonly JniPeerMembers _members = new XAPeerMembers (jniPeerTypeName:"android/app/Activity", managedPeerType:typeof (Activity));
	}

`JniRuntime.JniTypeManager.GetReplacementType()` allows "replacing"
the `jniPeerTypeName` value with a "replacement" JNI type name.
The "replacement" type will be used for all field and method lookups.
This allows supporting the `ClassRewrites[0].Class.From` /
`ClassRewrites[0].Class.To` semantics.

`JniRuntime.JniTypeManager.GetReplacementMethodInfo()` is the key
integration for all other "replacement" semantics.  Given the
source type, source method name, and source JNI signature, it is
responsible for providing an *overriding* target type, target method
name, and target JNI signature.

For `ClassRewrites[0].Methods[0]`, this allows "renaming"
`Activity.onCreate()` to `MAMActivity.onMAMCreate()`:

	var r = JniEnvironment.Runtime.TypeManager.GetReplacementMethodInfo (
	    "android/app/Activity",
	    "onCreate",
	    "(Landroid/os/Bundle;)V"
	);
	// is equivalent to:
	var r = new JniRuntime.ReplacementMethodInfo {
	    SourceJniType                   = "android/app/Activity",   // from input parameter
	    SourceJniMethodName             = "onCreate",               // from input parameter
	    SourceJniMethodSignature        = "(Landroid/os/Bundle;)V", // from input parameter

	    TargetJniType                   = "com/microsoft/intune/mam/client/app/MAMActivity",
	    TargetJniMethodName             = "onMAMCreate",
	    SourceJniMethodSignature        = null,                     // "use original value"
	    TargetJniMethodParameterCount   = null,                     // unchanged
	    TargetJniMethodInstanceToStatic = false,
	}

`ClassRewrites[1].Methods[0]` is "weird", in that it has
`MakeStatic: true`.  The semantics for when `MakeStatic: true` exists
is that the "original" method is an *instance* method, and the target
method is a *`static`* method, *and* the `this` instance now becomes
a *parameter* to the method.  This is likewise supported via
`JniRuntime.JniTypeManager.GetReplacementMethodInfo()`, and is
identified by:

 1. `ReplacementMethodInfo.TargetJniMethodParameterCount` being a
    non-`null` value, and

 2. `ReplacementMethodInfo.TargetJniMethodInstanceToStatic` is true.

Thus, `ClassRewrites[1].Methods[0]` is akin to:

	var r = JniEnvironment.Runtime.TypeManager.GetReplacementMethodInfo (
	    "android/content/pm/PackageManager",
	    "checkPermission",
	    "(Ljava/lang/String;Ljava/lang/String;)I"
	);
	// is equivalent to:
	var r = new JniRuntime.ReplacementMethodInfo {
	    SourceJniType                   = "android/content/pm/PackageManager",              // from input parameter
	    SourceJniMethodName             = "checkPermission",                                // from input parameter
	    SourceJniMethodSignature        = "(Ljava/lang/String;Ljava/lang/String;)I",        // from input parameter

	    TargetJniType                   = "com/microsoft/intune/mam/client/content/pm/MAMPackageManagement",
	    TargetJniMethodName             = "checkPermission",
	    SourceJniMethodSignature        = "(Landroid/content/pm/PackageManager;Ljava/lang/String;Ljava/lang/String;)I",
	    TargetJniMethodParameterCount   = 3,
	    TargetJniMethodInstanceToStatic = true,
	}

(Note that the subclass is responsible for providing this data.)

`ReplacementMethodInfo.TargetJniMethodParameterCount` must be the
number of parameters that the target method accepts.  This is needed
so that `JniPeerMembers.JniInstanceMethods.TryInvoke*StaticRedirect()`
can appropriately reallocate the `JniArgumentValue*` array, so that
the `this` parameter can be added to the beginning of the parameters.

~~ Overhead? ~~

All these extra invocations into `JniRuntime.JniTypeManager` imply
additional overhead to invoke a Java method.  However, this is all
done during the *first* time a method is looked up, after which the
`JniMethodInfo` instance is *cached* for a given
(type, method, signature) tuple.

To demonstrate overheads, `samples/Hello` has been updated to accept
a new `-t` value; when provided, it invokes the
`java.lang.Object.hashCode()` method 1 million times and calculates
the average invocation overhead:

	% dotnet run --project samples/Hello -- -t
	Object.hashCode: 1000000 invocations. Total=00:00:00.5196758; Average=0.0005196758ms

If we replace the .NET 6 `samples/Hello/bin/Debug/Java.Interop.dll`
assembly with e.g. `bin/Debug/Java.Interop.dll`, we can compare to
performance *without* these new changes, as the changes are only in
the .NET 6 build:

	% \cp bin/Debug/Java.Interop{.dll,.pdb} samples/Hello/bin/Debug
	% dotnet samples/Hello/bin/Debug/Hello.dll -t
	Object.hashCode: 1000000 invocations. Total=00:00:00.5386676; Average=0.0005386676ms

There is a fair bit of variation in `dotnet Hello.dll -t` output,
but they're all roughly similar.  There doesn't appear to be
significant overhead for this particular test.

[0]: https://en.wikipedia.org/wiki/Android_Runtime
[1]: https://developer.android.com/studio/write/java8-support
[2]: https://developer.android.com/studio/write/java8-support-table
[3]: https://docs.microsoft.com/en-us/mem/intune/developer/app-sdk-xamarin
[4]: https://www.nuget.org/packages/Microsoft.Intune.MAM.Remapper.Tasks/
jonpryor added a commit to jonpryor/java.interop that referenced this issue May 4, 2022
Context: dotnet#867

There are two scenarios for which a "generalized" type & member
remapping solution would be useful:

 1. Desugaring
 2. Microsoft Intune MAM

Note: this new support is only present when targeting .NET 6+,
and will not (currently) be present in Classic Xamarin.Android.

~~ Desugaring ~~

Context: dotnet/android#4574 (comment)
Context: dotnet/android#6142 (comment)

The [Android Runtime][0] is responsible for running the Dalvik
bytecode within e.g. `classes.dex`.  The Android runtime and Dalvik
bytecode formats have apparently changed over time in order to
support newer Java language features, such as support for static
interface methods:

	package org.webrtc;

	public /* partial */ interface EGLBase {
	    public static EGLBase create() { /* … */}
	}

Support for static interface methods was added in Java 8 and in
Android v8.0 (API-26).  If you want to use static interface methods
on an Android device running API-25 or earlier, you must [desugar][1]
the Java Bytecode.  The result of [desugaring][2] the above
`org.webrtc.EGLBase` type is that the static methods are moved to a
*new* type, with a `$-CC` suffix, "as if" the Java code were instead:

	package org.webrtc;

	public /* partial */ interface EGLBase {
	    // …
	}

	public final /* partial */ class EGLBase$-CC {
	    public static EGLBase create { /* … */ }
	}

While this works for pure Java code, this *does not work* with
Xamarin.Android, because our bindings currently expect (require) that
the types & members our binding process detects don't "move":

	// C# Binding for EGLBase
	namespace Org.Webrtc {
	    public partial interface IEGLBase {
	        private static readonly JniPeerMembers _members = new JniPeerMembers ("org/webrtc/EGLBase", typeof (IEGLBase));
	        public static IEGLBase Create()
	        {
	            const string __id = "create.()Lorg/webrtc/EglBase;"
	            var __rm = _members.StaticMethods.InvokeObjectMethod (__id, null);
	            return Java.Lang.Object.GetObject<IEGLBase>(__rm.Handle, JniHandleOwnership.TransferLocalRef);
	        }
	    }
	}

The `new JniPeerMembers(…)` invocation will use `JNIEnv::FindClass()`
to lookup the `org/webrtc/EGLBase` type, and `IEGLBase.Create()` will
use `JNIEnv::GetStaticMethodID()` and `JNIEnv::CallStaticObjectMethod()`
to lookup and invoke the `EGLBase.create()` method.

However, when desugaring is used, *there is no* `EGLBase.create()`
method.  Consequently, in a "desugared" environment,
`Java.Lang.NoSuchMethodError` will be thrown when `IEGLBase.Create()`
is invoked, as `EGLBase.create()` doesn't exist; it "should" instead
be looking for `EGLBase$-CC.create()`!

Introduce partial support for this scenario by adding the new method:

	namespace Java.Interop {
	    public partial class JniRuntime {
	        public partial class JniTypeManager {
	            public IReadOnlyList<string>? GetStaticMethodFallbackTypes (string jniSimpleReference) =>
	                GetStaticMethodFallbackTypesCore (jniSimpleReference);
	            protected virtual IReadOnlyList<string>? GetStaticMethodFallbackTypesCore (string jniSimpleReference) => null;
	        }
	    }
	}

`JniPeerMembers.JniStaticMethods.GetMethodInfo()` will attempt to
lookup the static method, "as normal".  If the method cannot be
found, then `JniRuntime.JniTypeManager.GetStaticMethodFallbackTypes()`
will be called, and we'll attempt to resolve the static method on each
type returned by `GetStaticMethodFallbackTypes()`.
If `GetStaticMethodFallbackTypes()` returns `null` or no fallback type
provides the method, then a `NoSuchMethodError` will be thrown.

TODO: Update xamarin-android to override
`GetStaticMethodFallbackTypes()`, to return
`$"{jniSimpleReference}$-CC"`.

~~ Microsoft Intune MAM ~~

Context: https://www.nuget.org/packages/Microsoft.Intune.MAM.Remapper.Tasks/
Context: https://docs.microsoft.com/en-us/mem/intune/developer/app-sdk-xamarin#remapper

The [Microsoft Intune App SDK Xamarin Bindings][3] is in a
similar-yet-different position: it involves Java & Dalvik Bytecode
manipulation for various security purposes, and in order to make that
work reasonably from Xamarin.Android, they *also* rewrite
Xamarin.Android IL so that it's consistent with the manipulated
Dalvik bytecode.

See `readme.md` and `content/MonoAndroid10/remapping-config.json`
within the [`Microsoft.Intune.MAM.Remapper.Tasks NuGet package`][4]
for some additional details/tidbits such as:

> This task operates on assemblies to replace the base type classes
> which Microsoft Intune requires to implement its SDK (for example,
> Intune requires using a `MAMActivity` in place of `Activity` and
> methods such as `OnMAMCreate` instead of `OnCreate`).

`remapping-config.json` contains JSON fragments such as:

	"ClassRewrites": [
	  {
	    "Class": {
	      "From": "android.app.Activity",
	      "To": "com.microsoft.intune.mam.client.app.MAMActivity"
	    },
	    "Methods": [
	      {
	        "MakeStatic": false,
	        "OriginalName": "onCreate",
	        "NewName": "onMAMCreate",
	        "OriginalParams": [
	          "android.os.Bundle"
	        ]
	      }
	    ]
	  },
	  {
	    "Class": {
	      "From": "android.content.pm.PackageManager",
	      "To": "com.microsoft.intune.mam.client.content.pm.MAMPackageManagement"
	    },
	    "Methods": [
	      {
	        "MakeStatic": true,
	        "OriginalName": "checkPermission",
	      }
	    ]
	  }
	]
	"GlobalMethodCalls": [
	  {
	    "Class": {
	      "From": "android.app.PendingIntent",
	      "To": "com.microsoft.intune.mam.client.app.MAMPendingIntent"
	    },
	    "Methods": [
	      {
	        "MakeStatic": false,
	        "OriginalName": "getActivities"
	      },
	    ]
	  }
	]

While what the InTune team has *works*, it suffers from a number of
"workflow" problems, e.g. incremental builds are problematic (they
might try to rewrite assemblies which were already rewritten), IL
rewriting is *always* "fun", and if we change IL binding patterns,
they'll need to support previous "binding styles" and newer styles.

Introduce partial support for this scenario by adding the following
members to `Java.Interop.JniRuntime.JniTypeManager`:

	namespace Java.Interop {
	    public partial class JniRuntime {

	        public struct ReplacementMethodInfo {
	            public  string? SourceJniType                   {get; set;}
	            public  string? SourceJniMethodName             {get; set;}
	            public  string? SourceJniMethodSignature        {get; set;}
	            public  string? TargetJniType                   {get; set;}
	            public  string? TargetJniMethodName             {get; set;}
	            public  string? TargetJniMethodSignature        {get; set;}
	            public  int?    TargetJniMethodParameterCount   {get; set;}
	            public  bool    TargetJniMethodInstanceToStatic {get; set;}
	        }

	        public partial class JniTypeManager {

	            public string? GetReplacementType (string jniSimpleReference) =>
	                GetReplacementTypeCore (jniSimpleReference);
	            protected virtual string? GetReplacementTypeCore (string jniSimpleReference) => null;

	            public ReplacementMethodInfo? GetReplacementMethodInfo (string jniSimpleReference, string jniMethodName, string jniMethodSignature) =>
	                GetReplacementMethodInfoCore (jniSimpleReference, jniMethodName,jniMethodSignature);
	            protected virtual ReplacementMethodInfo? GetReplacementMethodInfoCore (string jniSimpleReference, string jniMethodName, string jniMethodSignature) => null;
	        }
	    }
	}

These new methods are used by `JniPeerMembers`.

Consider "normal" usage of `JniPeerMembers`, within a binding:

	partial class Activity {
	    static readonly JniPeerMembers _members = new XAPeerMembers (jniPeerTypeName:"android/app/Activity", managedPeerType:typeof (Activity));
	}

`JniRuntime.JniTypeManager.GetReplacementType()` allows "replacing"
the `jniPeerTypeName` value with a "replacement" JNI type name.
The "replacement" type will be used for all field and method lookups.
This allows supporting the `ClassRewrites[0].Class.From` /
`ClassRewrites[0].Class.To` semantics.

`JniRuntime.JniTypeManager.GetReplacementMethodInfo()` is the key
integration for all other "replacement" semantics.  Given the
source type, source method name, and source JNI signature, it is
responsible for providing an *overriding* target type, target method
name, and target JNI signature.

For `ClassRewrites[0].Methods[0]`, this allows "renaming"
`Activity.onCreate()` to `MAMActivity.onMAMCreate()`:

	var r = JniEnvironment.Runtime.TypeManager.GetReplacementMethodInfo (
	    "android/app/Activity",
	    "onCreate",
	    "(Landroid/os/Bundle;)V"
	);
	// is equivalent to:
	var r = new JniRuntime.ReplacementMethodInfo {
	    SourceJniType                   = "android/app/Activity",   // from input parameter
	    SourceJniMethodName             = "onCreate",               // from input parameter
	    SourceJniMethodSignature        = "(Landroid/os/Bundle;)V", // from input parameter

	    TargetJniType                   = "com/microsoft/intune/mam/client/app/MAMActivity",
	    TargetJniMethodName             = "onMAMCreate",
	    SourceJniMethodSignature        = null,                     // "use original value"
	    TargetJniMethodParameterCount   = null,                     // unchanged
	    TargetJniMethodInstanceToStatic = false,
	}

`ClassRewrites[1].Methods[0]` is "weird", in that it has
`MakeStatic: true`.  The semantics for when `MakeStatic: true` exists
is that the "original" method is an *instance* method, and the target
method is a *`static`* method, *and* the `this` instance now becomes
a *parameter* to the method.  This is likewise supported via
`JniRuntime.JniTypeManager.GetReplacementMethodInfo()`, and is
identified by:

 1. `ReplacementMethodInfo.TargetJniMethodParameterCount` being a
    non-`null` value, and

 2. `ReplacementMethodInfo.TargetJniMethodInstanceToStatic` is true.

Thus, `ClassRewrites[1].Methods[0]` is akin to:

	var r = JniEnvironment.Runtime.TypeManager.GetReplacementMethodInfo (
	    "android/content/pm/PackageManager",
	    "checkPermission",
	    "(Ljava/lang/String;Ljava/lang/String;)I"
	);
	// is equivalent to:
	var r = new JniRuntime.ReplacementMethodInfo {
	    SourceJniType                   = "android/content/pm/PackageManager",              // from input parameter
	    SourceJniMethodName             = "checkPermission",                                // from input parameter
	    SourceJniMethodSignature        = "(Ljava/lang/String;Ljava/lang/String;)I",        // from input parameter

	    TargetJniType                   = "com/microsoft/intune/mam/client/content/pm/MAMPackageManagement",
	    TargetJniMethodName             = "checkPermission",
	    SourceJniMethodSignature        = "(Landroid/content/pm/PackageManager;Ljava/lang/String;Ljava/lang/String;)I",
	    TargetJniMethodParameterCount   = 3,
	    TargetJniMethodInstanceToStatic = true,
	}

(Note that the subclass is responsible for providing this data.)

`ReplacementMethodInfo.TargetJniMethodParameterCount` must be the
number of parameters that the target method accepts.  This is needed
so that `JniPeerMembers.JniInstanceMethods.TryInvoke*StaticRedirect()`
can appropriately reallocate the `JniArgumentValue*` array, so that
the `this` parameter can be added to the beginning of the parameters.

~~ Overhead? ~~

All these extra invocations into `JniRuntime.JniTypeManager` imply
additional overhead to invoke a Java method.  However, this is all
done during the *first* time a method is looked up, after which the
`JniMethodInfo` instance is *cached* for a given
(type, method, signature) tuple.

To demonstrate overheads, `samples/Hello` has been updated to accept
a new `-t` value; when provided, it invokes the
`java.lang.Object.hashCode()` method 1 million times and calculates
the average invocation overhead:

	% dotnet run --project samples/Hello -- -t
	Object.hashCode: 1000000 invocations. Total=00:00:00.5196758; Average=0.0005196758ms

If we replace the .NET 6 `samples/Hello/bin/Debug/Java.Interop.dll`
assembly with e.g. `bin/Debug/Java.Interop.dll`, we can compare to
performance *without* these new changes, as the changes are only in
the .NET 6 build:

	% \cp bin/Debug/Java.Interop{.dll,.pdb} samples/Hello/bin/Debug
	% dotnet samples/Hello/bin/Debug/Hello.dll -t
	Object.hashCode: 1000000 invocations. Total=00:00:00.5386676; Average=0.0005386676ms

There is a fair bit of variation in `dotnet Hello.dll -t` output,
but they're all roughly similar.  There doesn't appear to be
significant overhead for this particular test.

[0]: https://en.wikipedia.org/wiki/Android_Runtime
[1]: https://developer.android.com/studio/write/java8-support
[2]: https://developer.android.com/studio/write/java8-support-table
[3]: https://docs.microsoft.com/en-us/mem/intune/developer/app-sdk-xamarin
[4]: https://www.nuget.org/packages/Microsoft.Intune.MAM.Remapper.Tasks/
jonpryor added a commit to jonpryor/java.interop that referenced this issue May 5, 2022
Context: dotnet#867

There are two scenarios for which a "generalized" type & member
remapping solution would be useful:

 1. Desugaring
 2. Microsoft Intune MAM

Note: this new support is only present when targeting .NET 6+,
and will not (currently) be present in Classic Xamarin.Android.

~~ Desugaring ~~

Context: dotnet/android#4574 (comment)
Context: dotnet/android#6142 (comment)

The [Android Runtime][0] is responsible for running the Dalvik
bytecode within e.g. `classes.dex`.  The Android runtime and Dalvik
bytecode formats have apparently changed over time in order to
support newer Java language features, such as support for static
interface methods:

	package org.webrtc;

	public /* partial */ interface EGLBase {
	    public static EGLBase create() { /* … */}
	}

Support for static interface methods was added in Java 8 and in
Android v8.0 (API-26).  If you want to use static interface methods
on an Android device running API-25 or earlier, you must [desugar][1]
the Java Bytecode.  The result of [desugaring][2] the above
`org.webrtc.EGLBase` type is that the static methods are moved to a
*new* type, with a `$-CC` suffix, "as if" the Java code were instead:

	package org.webrtc;

	public /* partial */ interface EGLBase {
	    // …
	}

	public final /* partial */ class EGLBase$-CC {
	    public static EGLBase create { /* … */ }
	}

While this works for pure Java code, this *does not work* with
Xamarin.Android, because our bindings currently expect (require) that
the types & members our binding process detects don't "move":

	// C# Binding for EGLBase
	namespace Org.Webrtc {
	    public partial interface IEGLBase {
	        private static readonly JniPeerMembers _members = new JniPeerMembers ("org/webrtc/EGLBase", typeof (IEGLBase));
	        public static IEGLBase Create()
	        {
	            const string __id = "create.()Lorg/webrtc/EglBase;"
	            var __rm = _members.StaticMethods.InvokeObjectMethod (__id, null);
	            return Java.Lang.Object.GetObject<IEGLBase>(__rm.Handle, JniHandleOwnership.TransferLocalRef);
	        }
	    }
	}

The `new JniPeerMembers(…)` invocation will use `JNIEnv::FindClass()`
to lookup the `org/webrtc/EGLBase` type, and `IEGLBase.Create()` will
use `JNIEnv::GetStaticMethodID()` and `JNIEnv::CallStaticObjectMethod()`
to lookup and invoke the `EGLBase.create()` method.

However, when desugaring is used, *there is no* `EGLBase.create()`
method.  Consequently, in a "desugared" environment,
`Java.Lang.NoSuchMethodError` will be thrown when `IEGLBase.Create()`
is invoked, as `EGLBase.create()` doesn't exist; it "should" instead
be looking for `EGLBase$-CC.create()`!

Introduce partial support for this scenario by adding the new method:

	namespace Java.Interop {
	    public partial class JniRuntime {
	        public partial class JniTypeManager {
	            public IReadOnlyList<string>? GetStaticMethodFallbackTypes (string jniSimpleReference) =>
	                GetStaticMethodFallbackTypesCore (jniSimpleReference);
	            protected virtual IReadOnlyList<string>? GetStaticMethodFallbackTypesCore (string jniSimpleReference) => null;
	        }
	    }
	}

`JniPeerMembers.JniStaticMethods.GetMethodInfo()` will attempt to
lookup the static method, "as normal".  If the method cannot be
found, then `JniRuntime.JniTypeManager.GetStaticMethodFallbackTypes()`
will be called, and we'll attempt to resolve the static method on each
type returned by `GetStaticMethodFallbackTypes()`.
If `GetStaticMethodFallbackTypes()` returns `null` or no fallback type
provides the method, then a `NoSuchMethodError` will be thrown.

TODO: Update xamarin-android to override
`GetStaticMethodFallbackTypes()`, to return
`$"{jniSimpleReference}$-CC"`.

~~ Microsoft Intune MAM ~~

Context: https://www.nuget.org/packages/Microsoft.Intune.MAM.Remapper.Tasks/
Context: https://docs.microsoft.com/en-us/mem/intune/developer/app-sdk-xamarin#remapper

The [Microsoft Intune App SDK Xamarin Bindings][3] is in a
similar-yet-different position: it involves Java & Dalvik Bytecode
manipulation for various security purposes, and in order to make that
work reasonably from Xamarin.Android, they *also* rewrite
Xamarin.Android IL so that it's consistent with the manipulated
Dalvik bytecode.

See `readme.md` and `content/MonoAndroid10/remapping-config.json`
within the [`Microsoft.Intune.MAM.Remapper.Tasks NuGet package`][4]
for some additional details/tidbits such as:

> This task operates on assemblies to replace the base type classes
> which Microsoft Intune requires to implement its SDK (for example,
> Intune requires using a `MAMActivity` in place of `Activity` and
> methods such as `OnMAMCreate` instead of `OnCreate`).

`remapping-config.json` contains JSON fragments such as:

	"ClassRewrites": [
	  {
	    "Class": {
	      "From": "android.app.Activity",
	      "To": "com.microsoft.intune.mam.client.app.MAMActivity"
	    },
	    "Methods": [
	      {
	        "MakeStatic": false,
	        "OriginalName": "onCreate",
	        "NewName": "onMAMCreate",
	        "OriginalParams": [
	          "android.os.Bundle"
	        ]
	      }
	    ]
	  },
	  {
	    "Class": {
	      "From": "android.content.pm.PackageManager",
	      "To": "com.microsoft.intune.mam.client.content.pm.MAMPackageManagement"
	    },
	    "Methods": [
	      {
	        "MakeStatic": true,
	        "OriginalName": "checkPermission",
	      }
	    ]
	  }
	]
	"GlobalMethodCalls": [
	  {
	    "Class": {
	      "From": "android.app.PendingIntent",
	      "To": "com.microsoft.intune.mam.client.app.MAMPendingIntent"
	    },
	    "Methods": [
	      {
	        "MakeStatic": false,
	        "OriginalName": "getActivities"
	      },
	    ]
	  }
	]

While what the InTune team has *works*, it suffers from a number of
"workflow" problems, e.g. incremental builds are problematic (they
might try to rewrite assemblies which were already rewritten), IL
rewriting is *always* "fun", and if we change IL binding patterns,
they'll need to support previous "binding styles" and newer styles.

Introduce partial support for this scenario by adding the following
members to `Java.Interop.JniRuntime.JniTypeManager`:

	namespace Java.Interop {
	    public partial class JniRuntime {

	        public struct ReplacementMethodInfo {
	            public  string? SourceJniType                   {get; set;}
	            public  string? SourceJniMethodName             {get; set;}
	            public  string? SourceJniMethodSignature        {get; set;}
	            public  string? TargetJniType                   {get; set;}
	            public  string? TargetJniMethodName             {get; set;}
	            public  string? TargetJniMethodSignature        {get; set;}
	            public  int?    TargetJniMethodParameterCount   {get; set;}
	            public  bool    TargetJniMethodInstanceToStatic {get; set;}
	        }

	        public partial class JniTypeManager {

	            public string? GetReplacementType (string jniSimpleReference) =>
	                GetReplacementTypeCore (jniSimpleReference);
	            protected virtual string? GetReplacementTypeCore (string jniSimpleReference) => null;

	            public ReplacementMethodInfo? GetReplacementMethodInfo (string jniSimpleReference, string jniMethodName, string jniMethodSignature) =>
	                GetReplacementMethodInfoCore (jniSimpleReference, jniMethodName,jniMethodSignature);
	            protected virtual ReplacementMethodInfo? GetReplacementMethodInfoCore (string jniSimpleReference, string jniMethodName, string jniMethodSignature) => null;
	        }
	    }
	}

These new methods are used by `JniPeerMembers`.

Consider "normal" usage of `JniPeerMembers`, within a binding:

	partial class Activity {
	    static readonly JniPeerMembers _members = new XAPeerMembers (jniPeerTypeName:"android/app/Activity", managedPeerType:typeof (Activity));
	}

`JniRuntime.JniTypeManager.GetReplacementType()` allows "replacing"
the `jniPeerTypeName` value with a "replacement" JNI type name.
The "replacement" type will be used for all field and method lookups.
This allows supporting the `ClassRewrites[0].Class.From` /
`ClassRewrites[0].Class.To` semantics.

`JniRuntime.JniTypeManager.GetReplacementMethodInfo()` is the key
integration for all other "replacement" semantics.  Given the
source type, source method name, and source JNI signature, it is
responsible for providing an *overriding* target type, target method
name, and target JNI signature.

For `ClassRewrites[0].Methods[0]`, this allows "renaming"
`Activity.onCreate()` to `MAMActivity.onMAMCreate()`:

	var r = JniEnvironment.Runtime.TypeManager.GetReplacementMethodInfo (
	    "android/app/Activity",
	    "onCreate",
	    "(Landroid/os/Bundle;)V"
	);
	// is equivalent to:
	var r = new JniRuntime.ReplacementMethodInfo {
	    SourceJniType                   = "android/app/Activity",   // from input parameter
	    SourceJniMethodName             = "onCreate",               // from input parameter
	    SourceJniMethodSignature        = "(Landroid/os/Bundle;)V", // from input parameter

	    TargetJniType                   = "com/microsoft/intune/mam/client/app/MAMActivity",
	    TargetJniMethodName             = "onMAMCreate",
	    SourceJniMethodSignature        = null,                     // "use original value"
	    TargetJniMethodParameterCount   = null,                     // unchanged
	    TargetJniMethodInstanceToStatic = false,
	}

`ClassRewrites[1].Methods[0]` is "weird", in that it has
`MakeStatic: true`.  The semantics for when `MakeStatic: true` exists
is that the "original" method is an *instance* method, and the target
method is a *`static`* method, *and* the `this` instance now becomes
a *parameter* to the method.  This is likewise supported via
`JniRuntime.JniTypeManager.GetReplacementMethodInfo()`, and is
identified by:

 1. `ReplacementMethodInfo.TargetJniMethodParameterCount` being a
    non-`null` value, and

 2. `ReplacementMethodInfo.TargetJniMethodInstanceToStatic` is true.

Thus, `ClassRewrites[1].Methods[0]` is akin to:

	var r = JniEnvironment.Runtime.TypeManager.GetReplacementMethodInfo (
	    "android/content/pm/PackageManager",
	    "checkPermission",
	    "(Ljava/lang/String;Ljava/lang/String;)I"
	);
	// is equivalent to:
	var r = new JniRuntime.ReplacementMethodInfo {
	    SourceJniType                   = "android/content/pm/PackageManager",              // from input parameter
	    SourceJniMethodName             = "checkPermission",                                // from input parameter
	    SourceJniMethodSignature        = "(Ljava/lang/String;Ljava/lang/String;)I",        // from input parameter

	    TargetJniType                   = "com/microsoft/intune/mam/client/content/pm/MAMPackageManagement",
	    TargetJniMethodName             = "checkPermission",
	    SourceJniMethodSignature        = "(Landroid/content/pm/PackageManager;Ljava/lang/String;Ljava/lang/String;)I",
	    TargetJniMethodParameterCount   = 3,
	    TargetJniMethodInstanceToStatic = true,
	}

(Note that the subclass is responsible for providing this data.)

`ReplacementMethodInfo.TargetJniMethodParameterCount` must be the
number of parameters that the target method accepts.  This is needed
so that `JniPeerMembers.JniInstanceMethods.TryInvoke*StaticRedirect()`
can appropriately reallocate the `JniArgumentValue*` array, so that
the `this` parameter can be added to the beginning of the parameters.

~~ Overhead? ~~

All these extra invocations into `JniRuntime.JniTypeManager` imply
additional overhead to invoke a Java method.  However, this is all
done during the *first* time a method is looked up, after which the
`JniMethodInfo` instance is *cached* for a given
(type, method, signature) tuple.

To demonstrate overheads, `samples/Hello` has been updated to accept
a new `-t` value; when provided, it invokes the
`java.lang.Object.hashCode()` method 1 million times and calculates
the average invocation overhead:

	% dotnet run --project samples/Hello -- -t
	Object.hashCode: 1000000 invocations. Total=00:00:00.5196758; Average=0.0005196758ms

If we replace the .NET 6 `samples/Hello/bin/Debug/Java.Interop.dll`
assembly with e.g. `bin/Debug/Java.Interop.dll`, we can compare to
performance *without* these new changes, as the changes are only in
the .NET 6 build:

	% \cp bin/Debug/Java.Interop{.dll,.pdb} samples/Hello/bin/Debug
	% dotnet samples/Hello/bin/Debug/Hello.dll -t
	Object.hashCode: 1000000 invocations. Total=00:00:00.5386676; Average=0.0005386676ms

There is a fair bit of variation in `dotnet Hello.dll -t` output,
but they're all roughly similar.  There doesn't appear to be
significant overhead for this particular test.

[0]: https://en.wikipedia.org/wiki/Android_Runtime
[1]: https://developer.android.com/studio/write/java8-support
[2]: https://developer.android.com/studio/write/java8-support-table
[3]: https://docs.microsoft.com/en-us/mem/intune/developer/app-sdk-xamarin
[4]: https://www.nuget.org/packages/Microsoft.Intune.MAM.Remapper.Tasks/
jonpryor added a commit to jonpryor/java.interop that referenced this issue May 5, 2022
Context: dotnet#867

There are two scenarios for which a "generalized" type & member
remapping solution would be useful:

 1. Desugaring
 2. Microsoft Intune MAM

Note: this new support is only present when targeting .NET 6+,
and will not (currently) be present in Classic Xamarin.Android.

~~ Desugaring ~~

Context: dotnet/android#4574 (comment)
Context: dotnet/android#6142 (comment)

The [Android Runtime][0] is responsible for running the Dalvik
bytecode within e.g. `classes.dex`.  The Android runtime and Dalvik
bytecode formats have apparently changed over time in order to
support newer Java language features, such as support for static
interface methods:

	package org.webrtc;

	public /* partial */ interface EGLBase {
	    public static EGLBase create() { /* … */}
	}

Support for static interface methods was added in Java 8 and in
Android v8.0 (API-26).  If you want to use static interface methods
on an Android device running API-25 or earlier, you must [desugar][1]
the Java Bytecode.  The result of [desugaring][2] the above
`org.webrtc.EGLBase` type is that the static methods are moved to a
*new* type, with a `$-CC` suffix, "as if" the Java code were instead:

	package org.webrtc;

	public /* partial */ interface EGLBase {
	    // …
	}

	public final /* partial */ class EGLBase$-CC {
	    public static EGLBase create { /* … */ }
	}

While this works for pure Java code, this *does not work* with
Xamarin.Android, because our bindings currently expect (require) that
the types & members our binding process detects don't "move":

	// C# Binding for EGLBase
	namespace Org.Webrtc {
	    public partial interface IEGLBase {
	        private static readonly JniPeerMembers _members = new JniPeerMembers ("org/webrtc/EGLBase", typeof (IEGLBase));
	        public static IEGLBase Create()
	        {
	            const string __id = "create.()Lorg/webrtc/EglBase;"
	            var __rm = _members.StaticMethods.InvokeObjectMethod (__id, null);
	            return Java.Lang.Object.GetObject<IEGLBase>(__rm.Handle, JniHandleOwnership.TransferLocalRef);
	        }
	    }
	}

The `new JniPeerMembers(…)` invocation will use `JNIEnv::FindClass()`
to lookup the `org/webrtc/EGLBase` type, and `IEGLBase.Create()` will
use `JNIEnv::GetStaticMethodID()` and `JNIEnv::CallStaticObjectMethod()`
to lookup and invoke the `EGLBase.create()` method.

However, when desugaring is used, *there is no* `EGLBase.create()`
method.  Consequently, in a "desugared" environment,
`Java.Lang.NoSuchMethodError` will be thrown when `IEGLBase.Create()`
is invoked, as `EGLBase.create()` doesn't exist; it "should" instead
be looking for `EGLBase$-CC.create()`!

Introduce partial support for this scenario by adding the new method:

	namespace Java.Interop {
	    public partial class JniRuntime {
	        public partial class JniTypeManager {
	            public IReadOnlyList<string>? GetStaticMethodFallbackTypes (string jniSimpleReference) =>
	                GetStaticMethodFallbackTypesCore (jniSimpleReference);
	            protected virtual IReadOnlyList<string>? GetStaticMethodFallbackTypesCore (string jniSimpleReference) => null;
	        }
	    }
	}

`JniPeerMembers.JniStaticMethods.GetMethodInfo()` will attempt to
lookup the static method, "as normal".  If the method cannot be
found, then `JniRuntime.JniTypeManager.GetStaticMethodFallbackTypes()`
will be called, and we'll attempt to resolve the static method on each
type returned by `GetStaticMethodFallbackTypes()`.
If `GetStaticMethodFallbackTypes()` returns `null` or no fallback type
provides the method, then a `NoSuchMethodError` will be thrown.

TODO: Update xamarin-android to override
`GetStaticMethodFallbackTypes()`, to return
`$"{jniSimpleReference}$-CC"`.

~~ Microsoft Intune MAM ~~

Context: https://www.nuget.org/packages/Microsoft.Intune.MAM.Remapper.Tasks/
Context: https://docs.microsoft.com/en-us/mem/intune/developer/app-sdk-xamarin#remapper

The [Microsoft Intune App SDK Xamarin Bindings][3] is in a
similar-yet-different position: it involves Java & Dalvik Bytecode
manipulation for various security purposes, and in order to make that
work reasonably from Xamarin.Android, they *also* rewrite
Xamarin.Android IL so that it's consistent with the manipulated
Dalvik bytecode.

See `readme.md` and `content/MonoAndroid10/remapping-config.json`
within the [`Microsoft.Intune.MAM.Remapper.Tasks NuGet package`][4]
for some additional details/tidbits such as:

> This task operates on assemblies to replace the base type classes
> which Microsoft Intune requires to implement its SDK (for example,
> Intune requires using a `MAMActivity` in place of `Activity` and
> methods such as `OnMAMCreate` instead of `OnCreate`).

`remapping-config.json` contains JSON fragments such as:

	"ClassRewrites": [
	  {
	    "Class": {
	      "From": "android.app.Activity",
	      "To": "com.microsoft.intune.mam.client.app.MAMActivity"
	    },
	    "Methods": [
	      {
	        "MakeStatic": false,
	        "OriginalName": "onCreate",
	        "NewName": "onMAMCreate",
	        "OriginalParams": [
	          "android.os.Bundle"
	        ]
	      }
	    ]
	  },
	  {
	    "Class": {
	      "From": "android.content.pm.PackageManager",
	      "To": "com.microsoft.intune.mam.client.content.pm.MAMPackageManagement"
	    },
	    "Methods": [
	      {
	        "MakeStatic": true,
	        "OriginalName": "checkPermission",
	      }
	    ]
	  }
	]
	"GlobalMethodCalls": [
	  {
	    "Class": {
	      "From": "android.app.PendingIntent",
	      "To": "com.microsoft.intune.mam.client.app.MAMPendingIntent"
	    },
	    "Methods": [
	      {
	        "MakeStatic": false,
	        "OriginalName": "getActivities"
	      },
	    ]
	  }
	]

While what the InTune team has *works*, it suffers from a number of
"workflow" problems, e.g. incremental builds are problematic (they
might try to rewrite assemblies which were already rewritten), IL
rewriting is *always* "fun", and if we change IL binding patterns,
they'll need to support previous "binding styles" and newer styles.

Introduce partial support for this scenario by adding the following
members to `Java.Interop.JniRuntime.JniTypeManager`:

	namespace Java.Interop {
	    public partial class JniRuntime {

	        public struct ReplacementMethodInfo {
	            public  string? SourceJniType                   {get; set;}
	            public  string? SourceJniMethodName             {get; set;}
	            public  string? SourceJniMethodSignature        {get; set;}
	            public  string? TargetJniType                   {get; set;}
	            public  string? TargetJniMethodName             {get; set;}
	            public  string? TargetJniMethodSignature        {get; set;}
	            public  int?    TargetJniMethodParameterCount   {get; set;}
	            public  bool    TargetJniMethodInstanceToStatic {get; set;}
	        }

	        public partial class JniTypeManager {

	            public string? GetReplacementType (string jniSimpleReference) =>
	                GetReplacementTypeCore (jniSimpleReference);
	            protected virtual string? GetReplacementTypeCore (string jniSimpleReference) => null;

	            public ReplacementMethodInfo? GetReplacementMethodInfo (string jniSimpleReference, string jniMethodName, string jniMethodSignature) =>
	                GetReplacementMethodInfoCore (jniSimpleReference, jniMethodName,jniMethodSignature);
	            protected virtual ReplacementMethodInfo? GetReplacementMethodInfoCore (string jniSimpleReference, string jniMethodName, string jniMethodSignature) => null;
	        }
	    }
	}

These new methods are used by `JniPeerMembers`.

Consider "normal" usage of `JniPeerMembers`, within a binding:

	partial class Activity {
	    static readonly JniPeerMembers _members = new XAPeerMembers (jniPeerTypeName:"android/app/Activity", managedPeerType:typeof (Activity));
	}

`JniRuntime.JniTypeManager.GetReplacementType()` allows "replacing"
the `jniPeerTypeName` value with a "replacement" JNI type name.
The "replacement" type will be used for all field and method lookups.
This allows supporting the `ClassRewrites[0].Class.From` /
`ClassRewrites[0].Class.To` semantics.

`JniRuntime.JniTypeManager.GetReplacementMethodInfo()` is the key
integration for all other "replacement" semantics.  Given the
source type, source method name, and source JNI signature, it is
responsible for providing an *overriding* target type, target method
name, and target JNI signature.

For `ClassRewrites[0].Methods[0]`, this allows "renaming"
`Activity.onCreate()` to `MAMActivity.onMAMCreate()`:

	var r = JniEnvironment.Runtime.TypeManager.GetReplacementMethodInfo (
	    "android/app/Activity",
	    "onCreate",
	    "(Landroid/os/Bundle;)V"
	);
	// is equivalent to:
	var r = new JniRuntime.ReplacementMethodInfo {
	    SourceJniType                   = "android/app/Activity",   // from input parameter
	    SourceJniMethodName             = "onCreate",               // from input parameter
	    SourceJniMethodSignature        = "(Landroid/os/Bundle;)V", // from input parameter

	    TargetJniType                   = "com/microsoft/intune/mam/client/app/MAMActivity",
	    TargetJniMethodName             = "onMAMCreate",
	    SourceJniMethodSignature        = null,                     // "use original value"
	    TargetJniMethodParameterCount   = null,                     // unchanged
	    TargetJniMethodInstanceToStatic = false,
	}

`ClassRewrites[1].Methods[0]` is "weird", in that it has
`MakeStatic: true`.  The semantics for when `MakeStatic: true` exists
is that the "original" method is an *instance* method, and the target
method is a *`static`* method, *and* the `this` instance now becomes
a *parameter* to the method.  This is likewise supported via
`JniRuntime.JniTypeManager.GetReplacementMethodInfo()`, and is
identified by:

 1. `ReplacementMethodInfo.TargetJniMethodParameterCount` being a
    non-`null` value, and

 2. `ReplacementMethodInfo.TargetJniMethodInstanceToStatic` is true.

Thus, `ClassRewrites[1].Methods[0]` is akin to:

	var r = JniEnvironment.Runtime.TypeManager.GetReplacementMethodInfo (
	    "android/content/pm/PackageManager",
	    "checkPermission",
	    "(Ljava/lang/String;Ljava/lang/String;)I"
	);
	// is equivalent to:
	var r = new JniRuntime.ReplacementMethodInfo {
	    SourceJniType                   = "android/content/pm/PackageManager",              // from input parameter
	    SourceJniMethodName             = "checkPermission",                                // from input parameter
	    SourceJniMethodSignature        = "(Ljava/lang/String;Ljava/lang/String;)I",        // from input parameter

	    TargetJniType                   = "com/microsoft/intune/mam/client/content/pm/MAMPackageManagement",
	    TargetJniMethodName             = "checkPermission",
	    SourceJniMethodSignature        = "(Landroid/content/pm/PackageManager;Ljava/lang/String;Ljava/lang/String;)I",
	    TargetJniMethodParameterCount   = 3,
	    TargetJniMethodInstanceToStatic = true,
	}

(Note that the subclass is responsible for providing this data.)

`ReplacementMethodInfo.TargetJniMethodParameterCount` must be the
number of parameters that the target method accepts.  This is needed
so that `JniPeerMembers.JniInstanceMethods.TryInvoke*StaticRedirect()`
can appropriately reallocate the `JniArgumentValue*` array, so that
the `this` parameter can be added to the beginning of the parameters.

~~ Overhead? ~~

All these extra invocations into `JniRuntime.JniTypeManager` imply
additional overhead to invoke a Java method.  However, this is all
done during the *first* time a method is looked up, after which the
`JniMethodInfo` instance is *cached* for a given
(type, method, signature) tuple.

To demonstrate overheads, `samples/Hello` has been updated to accept
a new `-t` value; when provided, it invokes the
`java.lang.Object.hashCode()` method 1 million times and calculates
the average invocation overhead:

	% dotnet run --project samples/Hello -- -t
	Object.hashCode: 1000000 invocations. Total=00:00:00.5196758; Average=0.0005196758ms

If we replace the .NET 6 `samples/Hello/bin/Debug/Java.Interop.dll`
assembly with e.g. `bin/Debug/Java.Interop.dll`, we can compare to
performance *without* these new changes, as the changes are only in
the .NET 6 build:

	% \cp bin/Debug/Java.Interop{.dll,.pdb} samples/Hello/bin/Debug
	% dotnet samples/Hello/bin/Debug/Hello.dll -t
	Object.hashCode: 1000000 invocations. Total=00:00:00.5386676; Average=0.0005386676ms

There is a fair bit of variation in `dotnet Hello.dll -t` output,
but they're all roughly similar.  There doesn't appear to be
significant overhead for this particular test.

[0]: https://en.wikipedia.org/wiki/Android_Runtime
[1]: https://developer.android.com/studio/write/java8-support
[2]: https://developer.android.com/studio/write/java8-support-table
[3]: https://docs.microsoft.com/en-us/mem/intune/developer/app-sdk-xamarin
[4]: https://www.nuget.org/packages/Microsoft.Intune.MAM.Remapper.Tasks/
jonpryor added a commit to jonpryor/java.interop that referenced this issue May 6, 2022
Context: dotnet#867

There are two scenarios for which a "generalized" type & member
remapping solution would be useful:

 1. Desugaring
 2. Microsoft Intune MAM

Note: this new support is only present when targeting .NET 6+,
and will not (currently) be present in Classic Xamarin.Android.

~~ Desugaring ~~

Context: dotnet/android#4574 (comment)
Context: dotnet/android#6142 (comment)

The [Android Runtime][0] is responsible for running the Dalvik
bytecode within e.g. `classes.dex`.  The Android runtime and Dalvik
bytecode formats have apparently changed over time in order to
support newer Java language features, such as support for static
interface methods:

	package org.webrtc;

	public /* partial */ interface EGLBase {
	    public static EGLBase create() { /* … */}
	}

Support for static interface methods was added in Java 8 and in
Android v8.0 (API-26).  If you want to use static interface methods
on an Android device running API-25 or earlier, you must [desugar][1]
the Java Bytecode.  The result of [desugaring][2] the above
`org.webrtc.EGLBase` type is that the static methods are moved to a
*new* type, with a `$-CC` suffix, "as if" the Java code were instead:

	package org.webrtc;

	public /* partial */ interface EGLBase {
	    // …
	}

	public final /* partial */ class EGLBase$-CC {
	    public static EGLBase create { /* … */ }
	}

While this works for pure Java code, this *does not work* with
Xamarin.Android, because our bindings currently expect (require) that
the types & members our binding process detects don't "move":

	// C# Binding for EGLBase
	namespace Org.Webrtc {
	    public partial interface IEGLBase {
	        private static readonly JniPeerMembers _members = new JniPeerMembers ("org/webrtc/EGLBase", typeof (IEGLBase));
	        public static IEGLBase Create()
	        {
	            const string __id = "create.()Lorg/webrtc/EglBase;"
	            var __rm = _members.StaticMethods.InvokeObjectMethod (__id, null);
	            return Java.Lang.Object.GetObject<IEGLBase>(__rm.Handle, JniHandleOwnership.TransferLocalRef);
	        }
	    }
	}

The `new JniPeerMembers(…)` invocation will use `JNIEnv::FindClass()`
to lookup the `org/webrtc/EGLBase` type, and `IEGLBase.Create()` will
use `JNIEnv::GetStaticMethodID()` and `JNIEnv::CallStaticObjectMethod()`
to lookup and invoke the `EGLBase.create()` method.

However, when desugaring is used, *there is no* `EGLBase.create()`
method.  Consequently, in a "desugared" environment,
`Java.Lang.NoSuchMethodError` will be thrown when `IEGLBase.Create()`
is invoked, as `EGLBase.create()` doesn't exist; it "should" instead
be looking for `EGLBase$-CC.create()`!

Introduce partial support for this scenario by adding the new method:

	namespace Java.Interop {
	    public partial class JniRuntime {
	        public partial class JniTypeManager {
	            public IReadOnlyList<string>? GetStaticMethodFallbackTypes (string jniSimpleReference) =>
	                GetStaticMethodFallbackTypesCore (jniSimpleReference);
	            protected virtual IReadOnlyList<string>? GetStaticMethodFallbackTypesCore (string jniSimpleReference) => null;
	        }
	    }
	}

`JniPeerMembers.JniStaticMethods.GetMethodInfo()` will attempt to
lookup the static method, "as normal".  If the method cannot be
found, then `JniRuntime.JniTypeManager.GetStaticMethodFallbackTypes()`
will be called, and we'll attempt to resolve the static method on each
type returned by `GetStaticMethodFallbackTypes()`.
If `GetStaticMethodFallbackTypes()` returns `null` or no fallback type
provides the method, then a `NoSuchMethodError` will be thrown.

TODO: Update xamarin-android to override
`GetStaticMethodFallbackTypes()`, to return
`$"{jniSimpleReference}$-CC"`.

~~ Microsoft Intune MAM ~~

Context: https://www.nuget.org/packages/Microsoft.Intune.MAM.Remapper.Tasks/
Context: https://docs.microsoft.com/en-us/mem/intune/developer/app-sdk-xamarin#remapper

The [Microsoft Intune App SDK Xamarin Bindings][3] is in a
similar-yet-different position: it involves Java & Dalvik Bytecode
manipulation for various security purposes, and in order to make that
work reasonably from Xamarin.Android, they *also* rewrite
Xamarin.Android IL so that it's consistent with the manipulated
Dalvik bytecode.

See `readme.md` and `content/MonoAndroid10/remapping-config.json`
within the [`Microsoft.Intune.MAM.Remapper.Tasks NuGet package`][4]
for some additional details/tidbits such as:

> This task operates on assemblies to replace the base type classes
> which Microsoft Intune requires to implement its SDK (for example,
> Intune requires using a `MAMActivity` in place of `Activity` and
> methods such as `OnMAMCreate` instead of `OnCreate`).

`remapping-config.json` contains JSON fragments such as:

	"ClassRewrites": [
	  {
	    "Class": {
	      "From": "android.app.Activity",
	      "To": "com.microsoft.intune.mam.client.app.MAMActivity"
	    },
	    "Methods": [
	      {
	        "MakeStatic": false,
	        "OriginalName": "onCreate",
	        "NewName": "onMAMCreate",
	        "OriginalParams": [
	          "android.os.Bundle"
	        ]
	      }
	    ]
	  },
	  {
	    "Class": {
	      "From": "android.content.pm.PackageManager",
	      "To": "com.microsoft.intune.mam.client.content.pm.MAMPackageManagement"
	    },
	    "Methods": [
	      {
	        "MakeStatic": true,
	        "OriginalName": "checkPermission",
	      }
	    ]
	  }
	]
	"GlobalMethodCalls": [
	  {
	    "Class": {
	      "From": "android.app.PendingIntent",
	      "To": "com.microsoft.intune.mam.client.app.MAMPendingIntent"
	    },
	    "Methods": [
	      {
	        "MakeStatic": false,
	        "OriginalName": "getActivities"
	      },
	    ]
	  }
	]

While what the InTune team has *works*, it suffers from a number of
"workflow" problems, e.g. incremental builds are problematic (they
might try to rewrite assemblies which were already rewritten), IL
rewriting is *always* "fun", and if we change IL binding patterns,
they'll need to support previous "binding styles" and newer styles.

Introduce partial support for this scenario by adding the following
members to `Java.Interop.JniRuntime.JniTypeManager`:

	namespace Java.Interop {
	    public partial class JniRuntime {

	        public struct ReplacementMethodInfo {
	            public  string? SourceJniType                   {get; set;}
	            public  string? SourceJniMethodName             {get; set;}
	            public  string? SourceJniMethodSignature        {get; set;}
	            public  string? TargetJniType                   {get; set;}
	            public  string? TargetJniMethodName             {get; set;}
	            public  string? TargetJniMethodSignature        {get; set;}
	            public  int?    TargetJniMethodParameterCount   {get; set;}
	            public  bool    TargetJniMethodInstanceToStatic {get; set;}
	        }

	        public partial class JniTypeManager {

	            public string? GetReplacementType (string jniSimpleReference) =>
	                GetReplacementTypeCore (jniSimpleReference);
	            protected virtual string? GetReplacementTypeCore (string jniSimpleReference) => null;

	            public ReplacementMethodInfo? GetReplacementMethodInfo (string jniSimpleReference, string jniMethodName, string jniMethodSignature) =>
	                GetReplacementMethodInfoCore (jniSimpleReference, jniMethodName,jniMethodSignature);
	            protected virtual ReplacementMethodInfo? GetReplacementMethodInfoCore (string jniSimpleReference, string jniMethodName, string jniMethodSignature) => null;
	        }
	    }
	}

These new methods are used by `JniPeerMembers`.

Consider "normal" usage of `JniPeerMembers`, within a binding:

	partial class Activity {
	    static readonly JniPeerMembers _members = new XAPeerMembers (jniPeerTypeName:"android/app/Activity", managedPeerType:typeof (Activity));
	}

`JniRuntime.JniTypeManager.GetReplacementType()` allows "replacing"
the `jniPeerTypeName` value with a "replacement" JNI type name.
The "replacement" type will be used for all field and method lookups.
This allows supporting the `ClassRewrites[0].Class.From` /
`ClassRewrites[0].Class.To` semantics.

`JniRuntime.JniTypeManager.GetReplacementMethodInfo()` is the key
integration for all other "replacement" semantics.  Given the
source type, source method name, and source JNI signature, it is
responsible for providing an *overriding* target type, target method
name, and target JNI signature.

For `ClassRewrites[0].Methods[0]`, this allows "renaming"
`Activity.onCreate()` to `MAMActivity.onMAMCreate()`:

	var r = JniEnvironment.Runtime.TypeManager.GetReplacementMethodInfo (
	    "android/app/Activity",
	    "onCreate",
	    "(Landroid/os/Bundle;)V"
	);
	// is equivalent to:
	var r = new JniRuntime.ReplacementMethodInfo {
	    SourceJniType                   = "android/app/Activity",   // from input parameter
	    SourceJniMethodName             = "onCreate",               // from input parameter
	    SourceJniMethodSignature        = "(Landroid/os/Bundle;)V", // from input parameter

	    TargetJniType                   = "com/microsoft/intune/mam/client/app/MAMActivity",
	    TargetJniMethodName             = "onMAMCreate",
	    SourceJniMethodSignature        = null,                     // "use original value"
	    TargetJniMethodParameterCount   = null,                     // unchanged
	    TargetJniMethodInstanceToStatic = false,
	}

`ClassRewrites[1].Methods[0]` is "weird", in that it has
`MakeStatic: true`.  The semantics for when `MakeStatic: true` exists
is that the "original" method is an *instance* method, and the target
method is a *`static`* method, *and* the `this` instance now becomes
a *parameter* to the method.  This is likewise supported via
`JniRuntime.JniTypeManager.GetReplacementMethodInfo()`, and is
identified by:

 1. `ReplacementMethodInfo.TargetJniMethodParameterCount` being a
    non-`null` value, and

 2. `ReplacementMethodInfo.TargetJniMethodInstanceToStatic` is true.

Thus, `ClassRewrites[1].Methods[0]` is akin to:

	var r = JniEnvironment.Runtime.TypeManager.GetReplacementMethodInfo (
	    "android/content/pm/PackageManager",
	    "checkPermission",
	    "(Ljava/lang/String;Ljava/lang/String;)I"
	);
	// is equivalent to:
	var r = new JniRuntime.ReplacementMethodInfo {
	    SourceJniType                   = "android/content/pm/PackageManager",              // from input parameter
	    SourceJniMethodName             = "checkPermission",                                // from input parameter
	    SourceJniMethodSignature        = "(Ljava/lang/String;Ljava/lang/String;)I",        // from input parameter

	    TargetJniType                   = "com/microsoft/intune/mam/client/content/pm/MAMPackageManagement",
	    TargetJniMethodName             = "checkPermission",
	    SourceJniMethodSignature        = "(Landroid/content/pm/PackageManager;Ljava/lang/String;Ljava/lang/String;)I",
	    TargetJniMethodParameterCount   = 3,
	    TargetJniMethodInstanceToStatic = true,
	}

(Note that the subclass is responsible for providing this data.)

`ReplacementMethodInfo.TargetJniMethodParameterCount` must be the
number of parameters that the target method accepts.  This is needed
so that `JniPeerMembers.JniInstanceMethods.TryInvoke*StaticRedirect()`
can appropriately reallocate the `JniArgumentValue*` array, so that
the `this` parameter can be added to the beginning of the parameters.

~~ Overhead? ~~

All these extra invocations into `JniRuntime.JniTypeManager` imply
additional overhead to invoke a Java method.  However, this is all
done during the *first* time a method is looked up, after which the
`JniMethodInfo` instance is *cached* for a given
(type, method, signature) tuple.

To demonstrate overheads, `samples/Hello` has been updated to accept
a new `-t` value; when provided, it invokes the
`java.lang.Object.hashCode()` method 1 million times and calculates
the average invocation overhead:

	% dotnet run --project samples/Hello -- -t
	Object.hashCode: 1000000 invocations. Total=00:00:00.5196758; Average=0.0005196758ms

If we replace the .NET 6 `samples/Hello/bin/Debug/Java.Interop.dll`
assembly with e.g. `bin/Debug/Java.Interop.dll`, we can compare to
performance *without* these new changes, as the changes are only in
the .NET 6 build:

	% \cp bin/Debug/Java.Interop{.dll,.pdb} samples/Hello/bin/Debug
	% dotnet samples/Hello/bin/Debug/Hello.dll -t
	Object.hashCode: 1000000 invocations. Total=00:00:00.5386676; Average=0.0005386676ms

There is a fair bit of variation in `dotnet Hello.dll -t` output,
but they're all roughly similar.  There doesn't appear to be
significant overhead for this particular test.

[0]: https://en.wikipedia.org/wiki/Android_Runtime
[1]: https://developer.android.com/studio/write/java8-support
[2]: https://developer.android.com/studio/write/java8-support-table
[3]: https://docs.microsoft.com/en-us/mem/intune/developer/app-sdk-xamarin
[4]: https://www.nuget.org/packages/Microsoft.Intune.MAM.Remapper.Tasks/
jonpryor added a commit to jonpryor/java.interop that referenced this issue May 12, 2022
Context: dotnet#867

There are two scenarios for which a "generalized" type & member
remapping solution would be useful:

 1. Desugaring
 2. Microsoft Intune MAM

Note: this new support is only present when targeting .NET 6+,
and will not (currently) be present in Classic Xamarin.Android.

~~ Desugaring ~~

Context: dotnet/android#4574 (comment)
Context: dotnet/android#6142 (comment)

The [Android Runtime][0] is responsible for running the Dalvik
bytecode within e.g. `classes.dex`.  The Android runtime and Dalvik
bytecode formats have apparently changed over time in order to
support newer Java language features, such as support for static
interface methods:

	package org.webrtc;

	public /* partial */ interface EGLBase {
	    public static EGLBase create() { /* … */}
	}

Support for static interface methods was added in Java 8 and in
Android v8.0 (API-26).  If you want to use static interface methods
on an Android device running API-25 or earlier, you must [desugar][1]
the Java Bytecode.  The result of [desugaring][2] the above
`org.webrtc.EGLBase` type is that the static methods are moved to a
*new* type, with a `$-CC` suffix, "as if" the Java code were instead:

	package org.webrtc;

	public /* partial */ interface EGLBase {
	    // …
	}

	public final /* partial */ class EGLBase$-CC {
	    public static EGLBase create { /* … */ }
	}

While this works for pure Java code, this *does not work* with
Xamarin.Android, because our bindings currently expect (require) that
the types & members our binding process detects don't "move":

	// C# Binding for EGLBase
	namespace Org.Webrtc {
	    public partial interface IEGLBase {
	        private static readonly JniPeerMembers _members = new JniPeerMembers ("org/webrtc/EGLBase", typeof (IEGLBase));
	        public static IEGLBase Create()
	        {
	            const string __id = "create.()Lorg/webrtc/EglBase;"
	            var __rm = _members.StaticMethods.InvokeObjectMethod (__id, null);
	            return Java.Lang.Object.GetObject<IEGLBase>(__rm.Handle, JniHandleOwnership.TransferLocalRef);
	        }
	    }
	}

The `new JniPeerMembers(…)` invocation will use `JNIEnv::FindClass()`
to lookup the `org/webrtc/EGLBase` type, and `IEGLBase.Create()` will
use `JNIEnv::GetStaticMethodID()` and `JNIEnv::CallStaticObjectMethod()`
to lookup and invoke the `EGLBase.create()` method.

However, when desugaring is used, *there is no* `EGLBase.create()`
method.  Consequently, in a "desugared" environment,
`Java.Lang.NoSuchMethodError` will be thrown when `IEGLBase.Create()`
is invoked, as `EGLBase.create()` doesn't exist; it "should" instead
be looking for `EGLBase$-CC.create()`!

Introduce partial support for this scenario by adding the new method:

	namespace Java.Interop {
	    public partial class JniRuntime {
	        public partial class JniTypeManager {
	            public IReadOnlyList<string>? GetStaticMethodFallbackTypes (string jniSimpleReference) =>
	                GetStaticMethodFallbackTypesCore (jniSimpleReference);
	            protected virtual IReadOnlyList<string>? GetStaticMethodFallbackTypesCore (string jniSimpleReference) => null;
	        }
	    }
	}

`JniPeerMembers.JniStaticMethods.GetMethodInfo()` will attempt to
lookup the static method, "as normal".  If the method cannot be
found, then `JniRuntime.JniTypeManager.GetStaticMethodFallbackTypes()`
will be called, and we'll attempt to resolve the static method on each
type returned by `GetStaticMethodFallbackTypes()`.
If `GetStaticMethodFallbackTypes()` returns `null` or no fallback type
provides the method, then a `NoSuchMethodError` will be thrown.

TODO: Update xamarin-android to override
`GetStaticMethodFallbackTypes()`, to return
`$"{jniSimpleReference}$-CC"`.

~~ Microsoft Intune MAM ~~

Context: https://www.nuget.org/packages/Microsoft.Intune.MAM.Remapper.Tasks/
Context: https://docs.microsoft.com/en-us/mem/intune/developer/app-sdk-xamarin#remapper

The [Microsoft Intune App SDK Xamarin Bindings][3] is in a
similar-yet-different position: it involves Java & Dalvik Bytecode
manipulation for various security purposes, and in order to make that
work reasonably from Xamarin.Android, they *also* rewrite
Xamarin.Android IL so that it's consistent with the manipulated
Dalvik bytecode.

See `readme.md` and `content/MonoAndroid10/remapping-config.json`
within the [`Microsoft.Intune.MAM.Remapper.Tasks NuGet package`][4]
for some additional details/tidbits such as:

> This task operates on assemblies to replace the base type classes
> which Microsoft Intune requires to implement its SDK (for example,
> Intune requires using a `MAMActivity` in place of `Activity` and
> methods such as `OnMAMCreate` instead of `OnCreate`).

`remapping-config.json` contains JSON fragments such as:

	"ClassRewrites": [
	  {
	    "Class": {
	      "From": "android.app.Activity",
	      "To": "com.microsoft.intune.mam.client.app.MAMActivity"
	    },
	    "Methods": [
	      {
	        "MakeStatic": false,
	        "OriginalName": "onCreate",
	        "NewName": "onMAMCreate",
	        "OriginalParams": [
	          "android.os.Bundle"
	        ]
	      }
	    ]
	  },
	  {
	    "Class": {
	      "From": "android.content.pm.PackageManager",
	      "To": "com.microsoft.intune.mam.client.content.pm.MAMPackageManagement"
	    },
	    "Methods": [
	      {
	        "MakeStatic": true,
	        "OriginalName": "checkPermission",
	      }
	    ]
	  }
	]
	"GlobalMethodCalls": [
	  {
	    "Class": {
	      "From": "android.app.PendingIntent",
	      "To": "com.microsoft.intune.mam.client.app.MAMPendingIntent"
	    },
	    "Methods": [
	      {
	        "MakeStatic": false,
	        "OriginalName": "getActivities"
	      },
	    ]
	  }
	]

While what the InTune team has *works*, it suffers from a number of
"workflow" problems, e.g. incremental builds are problematic (they
might try to rewrite assemblies which were already rewritten), IL
rewriting is *always* "fun", and if we change IL binding patterns,
they'll need to support previous "binding styles" and newer styles.

Introduce partial support for this scenario by adding the following
members to `Java.Interop.JniRuntime.JniTypeManager`:

	namespace Java.Interop {
	    public partial class JniRuntime {

	        public struct ReplacementMethodInfo {
	            public  string? SourceJniType                   {get; set;}
	            public  string? SourceJniMethodName             {get; set;}
	            public  string? SourceJniMethodSignature        {get; set;}
	            public  string? TargetJniType                   {get; set;}
	            public  string? TargetJniMethodName             {get; set;}
	            public  string? TargetJniMethodSignature        {get; set;}
	            public  int?    TargetJniMethodParameterCount   {get; set;}
	            public  bool    TargetJniMethodInstanceToStatic {get; set;}
	        }

	        public partial class JniTypeManager {

	            public string? GetReplacementType (string jniSimpleReference) =>
	                GetReplacementTypeCore (jniSimpleReference);
	            protected virtual string? GetReplacementTypeCore (string jniSimpleReference) => null;

	            public ReplacementMethodInfo? GetReplacementMethodInfo (string jniSimpleReference, string jniMethodName, string jniMethodSignature) =>
	                GetReplacementMethodInfoCore (jniSimpleReference, jniMethodName,jniMethodSignature);
	            protected virtual ReplacementMethodInfo? GetReplacementMethodInfoCore (string jniSimpleReference, string jniMethodName, string jniMethodSignature) => null;
	        }
	    }
	}

These new methods are used by `JniPeerMembers`.

Consider "normal" usage of `JniPeerMembers`, within a binding:

	partial class Activity {
	    static readonly JniPeerMembers _members = new XAPeerMembers (jniPeerTypeName:"android/app/Activity", managedPeerType:typeof (Activity));
	}

`JniRuntime.JniTypeManager.GetReplacementType()` allows "replacing"
the `jniPeerTypeName` value with a "replacement" JNI type name.
The "replacement" type will be used for all field and method lookups.
This allows supporting the `ClassRewrites[0].Class.From` /
`ClassRewrites[0].Class.To` semantics.

`JniRuntime.JniTypeManager.GetReplacementMethodInfo()` is the key
integration for all other "replacement" semantics.  Given the
source type, source method name, and source JNI signature, it is
responsible for providing an *overriding* target type, target method
name, and target JNI signature.

For `ClassRewrites[0].Methods[0]`, this allows "renaming"
`Activity.onCreate()` to `MAMActivity.onMAMCreate()`:

	var r = JniEnvironment.Runtime.TypeManager.GetReplacementMethodInfo (
	    "android/app/Activity",
	    "onCreate",
	    "(Landroid/os/Bundle;)V"
	);
	// is equivalent to:
	var r = new JniRuntime.ReplacementMethodInfo {
	    SourceJniType                   = "android/app/Activity",   // from input parameter
	    SourceJniMethodName             = "onCreate",               // from input parameter
	    SourceJniMethodSignature        = "(Landroid/os/Bundle;)V", // from input parameter

	    TargetJniType                   = "com/microsoft/intune/mam/client/app/MAMActivity",
	    TargetJniMethodName             = "onMAMCreate",
	    SourceJniMethodSignature        = null,                     // "use original value"
	    TargetJniMethodParameterCount   = null,                     // unchanged
	    TargetJniMethodInstanceToStatic = false,
	}

`ClassRewrites[1].Methods[0]` is "weird", in that it has
`MakeStatic: true`.  The semantics for when `MakeStatic: true` exists
is that the "original" method is an *instance* method, and the target
method is a *`static`* method, *and* the `this` instance now becomes
a *parameter* to the method.  This is likewise supported via
`JniRuntime.JniTypeManager.GetReplacementMethodInfo()`, and is
identified by:

 1. `ReplacementMethodInfo.TargetJniMethodParameterCount` being a
    non-`null` value, and

 2. `ReplacementMethodInfo.TargetJniMethodInstanceToStatic` is true.

Thus, `ClassRewrites[1].Methods[0]` is akin to:

	var r = JniEnvironment.Runtime.TypeManager.GetReplacementMethodInfo (
	    "android/content/pm/PackageManager",
	    "checkPermission",
	    "(Ljava/lang/String;Ljava/lang/String;)I"
	);
	// is equivalent to:
	var r = new JniRuntime.ReplacementMethodInfo {
	    SourceJniType                   = "android/content/pm/PackageManager",              // from input parameter
	    SourceJniMethodName             = "checkPermission",                                // from input parameter
	    SourceJniMethodSignature        = "(Ljava/lang/String;Ljava/lang/String;)I",        // from input parameter

	    TargetJniType                   = "com/microsoft/intune/mam/client/content/pm/MAMPackageManagement",
	    TargetJniMethodName             = "checkPermission",
	    SourceJniMethodSignature        = "(Landroid/content/pm/PackageManager;Ljava/lang/String;Ljava/lang/String;)I",
	    TargetJniMethodParameterCount   = 3,
	    TargetJniMethodInstanceToStatic = true,
	}

(Note that the subclass is responsible for providing this data.)

`ReplacementMethodInfo.TargetJniMethodParameterCount` must be the
number of parameters that the target method accepts.  This is needed
so that `JniPeerMembers.JniInstanceMethods.TryInvoke*StaticRedirect()`
can appropriately reallocate the `JniArgumentValue*` array, so that
the `this` parameter can be added to the beginning of the parameters.

~~ Overhead? ~~

All these extra invocations into `JniRuntime.JniTypeManager` imply
additional overhead to invoke a Java method.  However, this is all
done during the *first* time a method is looked up, after which the
`JniMethodInfo` instance is *cached* for a given
(type, method, signature) tuple.

To demonstrate overheads, `samples/Hello` has been updated to accept
a new `-t` value; when provided, it invokes the
`java.lang.Object.hashCode()` method 1 million times and calculates
the average invocation overhead:

	% dotnet run --project samples/Hello -- -t
	Object.hashCode: 1000000 invocations. Total=00:00:00.5196758; Average=0.0005196758ms

If we replace the .NET 6 `samples/Hello/bin/Debug/Java.Interop.dll`
assembly with e.g. `bin/Debug/Java.Interop.dll`, we can compare to
performance *without* these new changes, as the changes are only in
the .NET 6 build:

	% \cp bin/Debug/Java.Interop{.dll,.pdb} samples/Hello/bin/Debug
	% dotnet samples/Hello/bin/Debug/Hello.dll -t
	Object.hashCode: 1000000 invocations. Total=00:00:00.5386676; Average=0.0005386676ms

There is a fair bit of variation in `dotnet Hello.dll -t` output,
but they're all roughly similar.  There doesn't appear to be
significant overhead for this particular test.

[0]: https://en.wikipedia.org/wiki/Android_Runtime
[1]: https://developer.android.com/studio/write/java8-support
[2]: https://developer.android.com/studio/write/java8-support-table
[3]: https://docs.microsoft.com/en-us/mem/intune/developer/app-sdk-xamarin
[4]: https://www.nuget.org/packages/Microsoft.Intune.MAM.Remapper.Tasks/
jonpryor added a commit to jonpryor/java.interop that referenced this issue May 16, 2022
Context: dotnet#867

There are two scenarios for which a "generalized" type & member
remapping solution would be useful:

 1. Desugaring
 2. Microsoft Intune MAM

Note: this new support is only present when targeting .NET 6+,
and will not (currently) be present in Classic Xamarin.Android.

~~ Desugaring ~~

Context: dotnet/android#4574 (comment)
Context: dotnet/android#6142 (comment)

The [Android Runtime][0] is responsible for running the Dalvik
bytecode within e.g. `classes.dex`.  The Android runtime and Dalvik
bytecode formats have apparently changed over time in order to
support newer Java language features, such as support for static
interface methods:

	package org.webrtc;

	public /* partial */ interface EGLBase {
	    public static EGLBase create() { /* … */}
	}

Support for static interface methods was added in Java 8 and in
Android v8.0 (API-26).  If you want to use static interface methods
on an Android device running API-25 or earlier, you must [desugar][1]
the Java Bytecode.  The result of [desugaring][2] the above
`org.webrtc.EGLBase` type is that the static methods are moved to a
*new* type, with a `$-CC` suffix, "as if" the Java code were instead:

	package org.webrtc;

	public /* partial */ interface EGLBase {
	    // …
	}

	public final /* partial */ class EGLBase$-CC {
	    public static EGLBase create { /* … */ }
	}

While this works for pure Java code, this *does not work* with
Xamarin.Android, because our bindings currently expect (require) that
the types & members our binding process detects don't "move":

	// C# Binding for EGLBase
	namespace Org.Webrtc {
	    public partial interface IEGLBase {
	        private static readonly JniPeerMembers _members = new JniPeerMembers ("org/webrtc/EGLBase", typeof (IEGLBase));
	        public static IEGLBase Create()
	        {
	            const string __id = "create.()Lorg/webrtc/EglBase;"
	            var __rm = _members.StaticMethods.InvokeObjectMethod (__id, null);
	            return Java.Lang.Object.GetObject<IEGLBase>(__rm.Handle, JniHandleOwnership.TransferLocalRef);
	        }
	    }
	}

The `new JniPeerMembers(…)` invocation will use `JNIEnv::FindClass()`
to lookup the `org/webrtc/EGLBase` type, and `IEGLBase.Create()` will
use `JNIEnv::GetStaticMethodID()` and `JNIEnv::CallStaticObjectMethod()`
to lookup and invoke the `EGLBase.create()` method.

However, when desugaring is used, *there is no* `EGLBase.create()`
method.  Consequently, in a "desugared" environment,
`Java.Lang.NoSuchMethodError` will be thrown when `IEGLBase.Create()`
is invoked, as `EGLBase.create()` doesn't exist; it "should" instead
be looking for `EGLBase$-CC.create()`!

Introduce partial support for this scenario by adding the new method:

	namespace Java.Interop {
	    public partial class JniRuntime {
	        public partial class JniTypeManager {
	            public IReadOnlyList<string>? GetStaticMethodFallbackTypes (string jniSimpleReference) =>
	                GetStaticMethodFallbackTypesCore (jniSimpleReference);
	            protected virtual IReadOnlyList<string>? GetStaticMethodFallbackTypesCore (string jniSimpleReference) => null;
	        }
	    }
	}

`JniPeerMembers.JniStaticMethods.GetMethodInfo()` will attempt to
lookup the static method, "as normal".  If the method cannot be
found, then `JniRuntime.JniTypeManager.GetStaticMethodFallbackTypes()`
will be called, and we'll attempt to resolve the static method on each
type returned by `GetStaticMethodFallbackTypes()`.
If `GetStaticMethodFallbackTypes()` returns `null` or no fallback type
provides the method, then a `NoSuchMethodError` will be thrown.

TODO: Update xamarin-android to override
`GetStaticMethodFallbackTypes()`, to return
`$"{jniSimpleReference}$-CC"`.

~~ Microsoft Intune MAM ~~

Context: https://www.nuget.org/packages/Microsoft.Intune.MAM.Remapper.Tasks/
Context: https://docs.microsoft.com/en-us/mem/intune/developer/app-sdk-xamarin#remapper

The [Microsoft Intune App SDK Xamarin Bindings][3] is in a
similar-yet-different position: it involves Java & Dalvik Bytecode
manipulation for various security purposes, and in order to make that
work reasonably from Xamarin.Android, they *also* rewrite
Xamarin.Android IL so that it's consistent with the manipulated
Dalvik bytecode.

See `readme.md` and `content/MonoAndroid10/remapping-config.json`
within the [`Microsoft.Intune.MAM.Remapper.Tasks NuGet package`][4]
for some additional details/tidbits such as:

> This task operates on assemblies to replace the base type classes
> which Microsoft Intune requires to implement its SDK (for example,
> Intune requires using a `MAMActivity` in place of `Activity` and
> methods such as `OnMAMCreate` instead of `OnCreate`).

`remapping-config.json` contains JSON fragments such as:

	"ClassRewrites": [
	  {
	    "Class": {
	      "From": "android.app.Activity",
	      "To": "com.microsoft.intune.mam.client.app.MAMActivity"
	    },
	    "Methods": [
	      {
	        "MakeStatic": false,
	        "OriginalName": "onCreate",
	        "NewName": "onMAMCreate",
	        "OriginalParams": [
	          "android.os.Bundle"
	        ]
	      }
	    ]
	  },
	  {
	    "Class": {
	      "From": "android.content.pm.PackageManager",
	      "To": "com.microsoft.intune.mam.client.content.pm.MAMPackageManagement"
	    },
	    "Methods": [
	      {
	        "MakeStatic": true,
	        "OriginalName": "checkPermission",
	      }
	    ]
	  }
	]
	"GlobalMethodCalls": [
	  {
	    "Class": {
	      "From": "android.app.PendingIntent",
	      "To": "com.microsoft.intune.mam.client.app.MAMPendingIntent"
	    },
	    "Methods": [
	      {
	        "MakeStatic": false,
	        "OriginalName": "getActivities"
	      },
	    ]
	  }
	]

While what the InTune team has *works*, it suffers from a number of
"workflow" problems, e.g. incremental builds are problematic (they
might try to rewrite assemblies which were already rewritten), IL
rewriting is *always* "fun", and if we change IL binding patterns,
they'll need to support previous "binding styles" and newer styles.

Introduce partial support for this scenario by adding the following
members to `Java.Interop.JniRuntime.JniTypeManager`:

	namespace Java.Interop {
	    public partial class JniRuntime {

	        public struct ReplacementMethodInfo {
	            public  string? SourceJniType                   {get; set;}
	            public  string? SourceJniMethodName             {get; set;}
	            public  string? SourceJniMethodSignature        {get; set;}
	            public  string? TargetJniType                   {get; set;}
	            public  string? TargetJniMethodName             {get; set;}
	            public  string? TargetJniMethodSignature        {get; set;}
	            public  int?    TargetJniMethodParameterCount   {get; set;}
	            public  bool    TargetJniMethodInstanceToStatic {get; set;}
	        }

	        public partial class JniTypeManager {

	            public string? GetReplacementType (string jniSimpleReference) =>
	                GetReplacementTypeCore (jniSimpleReference);
	            protected virtual string? GetReplacementTypeCore (string jniSimpleReference) => null;

	            public ReplacementMethodInfo? GetReplacementMethodInfo (string jniSimpleReference, string jniMethodName, string jniMethodSignature) =>
	                GetReplacementMethodInfoCore (jniSimpleReference, jniMethodName,jniMethodSignature);
	            protected virtual ReplacementMethodInfo? GetReplacementMethodInfoCore (string jniSimpleReference, string jniMethodName, string jniMethodSignature) => null;
	        }
	    }
	}

These new methods are used by `JniPeerMembers`.

Consider "normal" usage of `JniPeerMembers`, within a binding:

	partial class Activity {
	    static readonly JniPeerMembers _members = new XAPeerMembers (jniPeerTypeName:"android/app/Activity", managedPeerType:typeof (Activity));
	}

`JniRuntime.JniTypeManager.GetReplacementType()` allows "replacing"
the `jniPeerTypeName` value with a "replacement" JNI type name.
The "replacement" type will be used for all field and method lookups.
This allows supporting the `ClassRewrites[0].Class.From` /
`ClassRewrites[0].Class.To` semantics.

`JniRuntime.JniTypeManager.GetReplacementMethodInfo()` is the key
integration for all other "replacement" semantics.  Given the
source type, source method name, and source JNI signature, it is
responsible for providing an *overriding* target type, target method
name, and target JNI signature.

For `ClassRewrites[0].Methods[0]`, this allows "renaming"
`Activity.onCreate()` to `MAMActivity.onMAMCreate()`:

	var r = JniEnvironment.Runtime.TypeManager.GetReplacementMethodInfo (
	    "android/app/Activity",
	    "onCreate",
	    "(Landroid/os/Bundle;)V"
	);
	// is equivalent to:
	var r = new JniRuntime.ReplacementMethodInfo {
	    SourceJniType                   = "android/app/Activity",   // from input parameter
	    SourceJniMethodName             = "onCreate",               // from input parameter
	    SourceJniMethodSignature        = "(Landroid/os/Bundle;)V", // from input parameter

	    TargetJniType                   = "com/microsoft/intune/mam/client/app/MAMActivity",
	    TargetJniMethodName             = "onMAMCreate",
	    SourceJniMethodSignature        = null,                     // "use original value"
	    TargetJniMethodParameterCount   = null,                     // unchanged
	    TargetJniMethodInstanceToStatic = false,
	}

`ClassRewrites[1].Methods[0]` is "weird", in that it has
`MakeStatic: true`.  The semantics for when `MakeStatic: true` exists
is that the "original" method is an *instance* method, and the target
method is a *`static`* method, *and* the `this` instance now becomes
a *parameter* to the method.  This is likewise supported via
`JniRuntime.JniTypeManager.GetReplacementMethodInfo()`, and is
identified by:

 1. `ReplacementMethodInfo.TargetJniMethodParameterCount` being a
    non-`null` value, and

 2. `ReplacementMethodInfo.TargetJniMethodInstanceToStatic` is true.

Thus, `ClassRewrites[1].Methods[0]` is akin to:

	var r = JniEnvironment.Runtime.TypeManager.GetReplacementMethodInfo (
	    "android/content/pm/PackageManager",
	    "checkPermission",
	    "(Ljava/lang/String;Ljava/lang/String;)I"
	);
	// is equivalent to:
	var r = new JniRuntime.ReplacementMethodInfo {
	    SourceJniType                   = "android/content/pm/PackageManager",              // from input parameter
	    SourceJniMethodName             = "checkPermission",                                // from input parameter
	    SourceJniMethodSignature        = "(Ljava/lang/String;Ljava/lang/String;)I",        // from input parameter

	    TargetJniType                   = "com/microsoft/intune/mam/client/content/pm/MAMPackageManagement",
	    TargetJniMethodName             = "checkPermission",
	    SourceJniMethodSignature        = "(Landroid/content/pm/PackageManager;Ljava/lang/String;Ljava/lang/String;)I",
	    TargetJniMethodParameterCount   = 3,
	    TargetJniMethodInstanceToStatic = true,
	}

(Note that the subclass is responsible for providing this data.)

`ReplacementMethodInfo.TargetJniMethodParameterCount` must be the
number of parameters that the target method accepts.  This is needed
so that `JniPeerMembers.JniInstanceMethods.TryInvoke*StaticRedirect()`
can appropriately reallocate the `JniArgumentValue*` array, so that
the `this` parameter can be added to the beginning of the parameters.

~~ Overhead? ~~

All these extra invocations into `JniRuntime.JniTypeManager` imply
additional overhead to invoke a Java method.  However, this is all
done during the *first* time a method is looked up, after which the
`JniMethodInfo` instance is *cached* for a given
(type, method, signature) tuple.

To demonstrate overheads, `samples/Hello` has been updated to accept
a new `-t` value; when provided, it invokes the
`java.lang.Object.hashCode()` method 1 million times and calculates
the average invocation overhead:

	% dotnet run --project samples/Hello -- -t
	Object.hashCode: 1000000 invocations. Total=00:00:00.5196758; Average=0.0005196758ms

If we replace the .NET 6 `samples/Hello/bin/Debug/Java.Interop.dll`
assembly with e.g. `bin/Debug/Java.Interop.dll`, we can compare to
performance *without* these new changes, as the changes are only in
the .NET 6 build:

	% \cp bin/Debug/Java.Interop{.dll,.pdb} samples/Hello/bin/Debug
	% dotnet samples/Hello/bin/Debug/Hello.dll -t
	Object.hashCode: 1000000 invocations. Total=00:00:00.5386676; Average=0.0005386676ms

There is a fair bit of variation in `dotnet Hello.dll -t` output,
but they're all roughly similar.  There doesn't appear to be
significant overhead for this particular test.

[0]: https://en.wikipedia.org/wiki/Android_Runtime
[1]: https://developer.android.com/studio/write/java8-support
[2]: https://developer.android.com/studio/write/java8-support-table
[3]: https://docs.microsoft.com/en-us/mem/intune/developer/app-sdk-xamarin
[4]: https://www.nuget.org/packages/Microsoft.Intune.MAM.Remapper.Tasks/
jonpryor added a commit to jonpryor/java.interop that referenced this issue May 16, 2022
Context: dotnet#867

There are two scenarios for which a "generalized" type & member
remapping solution would be useful:

 1. Desugaring
 2. Microsoft Intune MAM

Note: this new support is only present when targeting .NET 6+,
and will not (currently) be present in Classic Xamarin.Android.

~~ Desugaring ~~

Context: dotnet/android#4574 (comment)
Context: dotnet/android#6142 (comment)

The [Android Runtime][0] is responsible for running the Dalvik
bytecode within e.g. `classes.dex`.  The Android runtime and Dalvik
bytecode formats have apparently changed over time in order to
support newer Java language features, such as support for static
interface methods:

	package org.webrtc;

	public /* partial */ interface EGLBase {
	    public static EGLBase create() { /* … */}
	}

Support for static interface methods was added in Java 8 and in
Android v8.0 (API-26).  If you want to use static interface methods
on an Android device running API-25 or earlier, you must [desugar][1]
the Java Bytecode.  The result of [desugaring][2] the above
`org.webrtc.EGLBase` type is that the static methods are moved to a
*new* type, with a `$-CC` suffix, "as if" the Java code were instead:

	package org.webrtc;

	public /* partial */ interface EGLBase {
	    // …
	}

	public final /* partial */ class EGLBase$-CC {
	    public static EGLBase create { /* … */ }
	}

While this works for pure Java code, this *does not work* with
Xamarin.Android, because our bindings currently expect (require) that
the types & members our binding process detects don't "move":

	// C# Binding for EGLBase
	namespace Org.Webrtc {
	    public partial interface IEGLBase {
	        private static readonly JniPeerMembers _members = new JniPeerMembers ("org/webrtc/EGLBase", typeof (IEGLBase));
	        public static IEGLBase Create()
	        {
	            const string __id = "create.()Lorg/webrtc/EglBase;"
	            var __rm = _members.StaticMethods.InvokeObjectMethod (__id, null);
	            return Java.Lang.Object.GetObject<IEGLBase>(__rm.Handle, JniHandleOwnership.TransferLocalRef);
	        }
	    }
	}

The `new JniPeerMembers(…)` invocation will use `JNIEnv::FindClass()`
to lookup the `org/webrtc/EGLBase` type, and `IEGLBase.Create()` will
use `JNIEnv::GetStaticMethodID()` and `JNIEnv::CallStaticObjectMethod()`
to lookup and invoke the `EGLBase.create()` method.

However, when desugaring is used, *there is no* `EGLBase.create()`
method.  Consequently, in a "desugared" environment,
`Java.Lang.NoSuchMethodError` will be thrown when `IEGLBase.Create()`
is invoked, as `EGLBase.create()` doesn't exist; it "should" instead
be looking for `EGLBase$-CC.create()`!

Introduce partial support for this scenario by adding the new method:

	namespace Java.Interop {
	    public partial class JniRuntime {
	        public partial class JniTypeManager {
	            public IReadOnlyList<string>? GetStaticMethodFallbackTypes (string jniSimpleReference) =>
	                GetStaticMethodFallbackTypesCore (jniSimpleReference);
	            protected virtual IReadOnlyList<string>? GetStaticMethodFallbackTypesCore (string jniSimpleReference) => null;
	        }
	    }
	}

`JniPeerMembers.JniStaticMethods.GetMethodInfo()` will attempt to
lookup the static method, "as normal".  If the method cannot be
found, then `JniRuntime.JniTypeManager.GetStaticMethodFallbackTypes()`
will be called, and we'll attempt to resolve the static method on each
type returned by `GetStaticMethodFallbackTypes()`.
If `GetStaticMethodFallbackTypes()` returns `null` or no fallback type
provides the method, then a `NoSuchMethodError` will be thrown.

TODO: Update xamarin-android to override
`GetStaticMethodFallbackTypes()`, to return
`$"{jniSimpleReference}$-CC"`.

~~ Microsoft Intune MAM ~~

Context: https://www.nuget.org/packages/Microsoft.Intune.MAM.Remapper.Tasks/
Context: https://docs.microsoft.com/en-us/mem/intune/developer/app-sdk-xamarin#remapper

The [Microsoft Intune App SDK Xamarin Bindings][3] is in a
similar-yet-different position: it involves Java & Dalvik Bytecode
manipulation for various security purposes, and in order to make that
work reasonably from Xamarin.Android, they *also* rewrite
Xamarin.Android IL so that it's consistent with the manipulated
Dalvik bytecode.

See `readme.md` and `content/MonoAndroid10/remapping-config.json`
within the [`Microsoft.Intune.MAM.Remapper.Tasks NuGet package`][4]
for some additional details/tidbits such as:

> This task operates on assemblies to replace the base type classes
> which Microsoft Intune requires to implement its SDK (for example,
> Intune requires using a `MAMActivity` in place of `Activity` and
> methods such as `OnMAMCreate` instead of `OnCreate`).

`remapping-config.json` contains JSON fragments such as:

	"ClassRewrites": [
	  {
	    "Class": {
	      "From": "android.app.Activity",
	      "To": "com.microsoft.intune.mam.client.app.MAMActivity"
	    },
	    "Methods": [
	      {
	        "MakeStatic": false,
	        "OriginalName": "onCreate",
	        "NewName": "onMAMCreate",
	        "OriginalParams": [
	          "android.os.Bundle"
	        ]
	      }
	    ]
	  },
	  {
	    "Class": {
	      "From": "android.content.pm.PackageManager",
	      "To": "com.microsoft.intune.mam.client.content.pm.MAMPackageManagement"
	    },
	    "Methods": [
	      {
	        "MakeStatic": true,
	        "OriginalName": "checkPermission",
	      }
	    ]
	  }
	]
	"GlobalMethodCalls": [
	  {
	    "Class": {
	      "From": "android.app.PendingIntent",
	      "To": "com.microsoft.intune.mam.client.app.MAMPendingIntent"
	    },
	    "Methods": [
	      {
	        "MakeStatic": false,
	        "OriginalName": "getActivities"
	      },
	    ]
	  }
	]

While what the InTune team has *works*, it suffers from a number of
"workflow" problems, e.g. incremental builds are problematic (they
might try to rewrite assemblies which were already rewritten), IL
rewriting is *always* "fun", and if we change IL binding patterns,
they'll need to support previous "binding styles" and newer styles.

Introduce partial support for this scenario by adding the following
members to `Java.Interop.JniRuntime.JniTypeManager`:

	namespace Java.Interop {
	    public partial class JniRuntime {

	        public struct ReplacementMethodInfo {
	            public  string? SourceJniType                   {get; set;}
	            public  string? SourceJniMethodName             {get; set;}
	            public  string? SourceJniMethodSignature        {get; set;}
	            public  string? TargetJniType                   {get; set;}
	            public  string? TargetJniMethodName             {get; set;}
	            public  string? TargetJniMethodSignature        {get; set;}
	            public  int?    TargetJniMethodParameterCount   {get; set;}
	            public  bool    TargetJniMethodInstanceToStatic {get; set;}
	        }

	        public partial class JniTypeManager {

	            public string? GetReplacementType (string jniSimpleReference) =>
	                GetReplacementTypeCore (jniSimpleReference);
	            protected virtual string? GetReplacementTypeCore (string jniSimpleReference) => null;

	            public ReplacementMethodInfo? GetReplacementMethodInfo (string jniSimpleReference, string jniMethodName, string jniMethodSignature) =>
	                GetReplacementMethodInfoCore (jniSimpleReference, jniMethodName,jniMethodSignature);
	            protected virtual ReplacementMethodInfo? GetReplacementMethodInfoCore (string jniSimpleReference, string jniMethodName, string jniMethodSignature) => null;
	        }
	    }
	}

These new methods are used by `JniPeerMembers`.

Consider "normal" usage of `JniPeerMembers`, within a binding:

	partial class Activity {
	    static readonly JniPeerMembers _members = new XAPeerMembers (jniPeerTypeName:"android/app/Activity", managedPeerType:typeof (Activity));
	}

`JniRuntime.JniTypeManager.GetReplacementType()` allows "replacing"
the `jniPeerTypeName` value with a "replacement" JNI type name.
The "replacement" type will be used for all field and method lookups.
This allows supporting the `ClassRewrites[0].Class.From` /
`ClassRewrites[0].Class.To` semantics.

`JniRuntime.JniTypeManager.GetReplacementMethodInfo()` is the key
integration for all other "replacement" semantics.  Given the
source type, source method name, and source JNI signature, it is
responsible for providing an *overriding* target type, target method
name, and target JNI signature.

For `ClassRewrites[0].Methods[0]`, this allows "renaming"
`Activity.onCreate()` to `MAMActivity.onMAMCreate()`:

	var r = JniEnvironment.Runtime.TypeManager.GetReplacementMethodInfo (
	    "android/app/Activity",
	    "onCreate",
	    "(Landroid/os/Bundle;)V"
	);
	// is equivalent to:
	var r = new JniRuntime.ReplacementMethodInfo {
	    SourceJniType                   = "android/app/Activity",   // from input parameter
	    SourceJniMethodName             = "onCreate",               // from input parameter
	    SourceJniMethodSignature        = "(Landroid/os/Bundle;)V", // from input parameter

	    TargetJniType                   = "com/microsoft/intune/mam/client/app/MAMActivity",
	    TargetJniMethodName             = "onMAMCreate",
	    SourceJniMethodSignature        = null,                     // "use original value"
	    TargetJniMethodParameterCount   = null,                     // unchanged
	    TargetJniMethodInstanceToStatic = false,
	}

`ClassRewrites[1].Methods[0]` is "weird", in that it has
`MakeStatic: true`.  The semantics for when `MakeStatic: true` exists
is that the "original" method is an *instance* method, and the target
method is a *`static`* method, *and* the `this` instance now becomes
a *parameter* to the method.  This is likewise supported via
`JniRuntime.JniTypeManager.GetReplacementMethodInfo()`, and is
identified by:

 1. `ReplacementMethodInfo.TargetJniMethodParameterCount` being a
    non-`null` value, and

 2. `ReplacementMethodInfo.TargetJniMethodInstanceToStatic` is true.

Thus, `ClassRewrites[1].Methods[0]` is akin to:

	var r = JniEnvironment.Runtime.TypeManager.GetReplacementMethodInfo (
	    "android/content/pm/PackageManager",
	    "checkPermission",
	    "(Ljava/lang/String;Ljava/lang/String;)I"
	);
	// is equivalent to:
	var r = new JniRuntime.ReplacementMethodInfo {
	    SourceJniType                   = "android/content/pm/PackageManager",              // from input parameter
	    SourceJniMethodName             = "checkPermission",                                // from input parameter
	    SourceJniMethodSignature        = "(Ljava/lang/String;Ljava/lang/String;)I",        // from input parameter

	    TargetJniType                   = "com/microsoft/intune/mam/client/content/pm/MAMPackageManagement",
	    TargetJniMethodName             = "checkPermission",
	    SourceJniMethodSignature        = "(Landroid/content/pm/PackageManager;Ljava/lang/String;Ljava/lang/String;)I",
	    TargetJniMethodParameterCount   = 3,
	    TargetJniMethodInstanceToStatic = true,
	}

(Note that the subclass is responsible for providing this data.)

`ReplacementMethodInfo.TargetJniMethodParameterCount` must be the
number of parameters that the target method accepts.  This is needed
so that `JniPeerMembers.JniInstanceMethods.TryInvoke*StaticRedirect()`
can appropriately reallocate the `JniArgumentValue*` array, so that
the `this` parameter can be added to the beginning of the parameters.

~~ Overhead? ~~

All these extra invocations into `JniRuntime.JniTypeManager` imply
additional overhead to invoke a Java method.  However, this is all
done during the *first* time a method is looked up, after which the
`JniMethodInfo` instance is *cached* for a given
(type, method, signature) tuple.

To demonstrate overheads, `samples/Hello` has been updated to accept
a new `-t` value; when provided, it invokes the
`java.lang.Object.hashCode()` method 1 million times and calculates
the average invocation overhead:

	% dotnet run --project samples/Hello -- -t
	Object.hashCode: 1000000 invocations. Total=00:00:00.5196758; Average=0.0005196758ms

If we replace the .NET 6 `samples/Hello/bin/Debug/Java.Interop.dll`
assembly with e.g. `bin/Debug/Java.Interop.dll`, we can compare to
performance *without* these new changes, as the changes are only in
the .NET 6 build:

	% \cp bin/Debug/Java.Interop{.dll,.pdb} samples/Hello/bin/Debug
	% dotnet samples/Hello/bin/Debug/Hello.dll -t
	Object.hashCode: 1000000 invocations. Total=00:00:00.5386676; Average=0.0005386676ms

There is a fair bit of variation in `dotnet Hello.dll -t` output,
but they're all roughly similar.  There doesn't appear to be
significant overhead for this particular test.

[0]: https://en.wikipedia.org/wiki/Android_Runtime
[1]: https://developer.android.com/studio/write/java8-support
[2]: https://developer.android.com/studio/write/java8-support-table
[3]: https://docs.microsoft.com/en-us/mem/intune/developer/app-sdk-xamarin
[4]: https://www.nuget.org/packages/Microsoft.Intune.MAM.Remapper.Tasks/
jonpryor added a commit to jonpryor/java.interop that referenced this issue May 19, 2022
Context: dotnet#867

There are two scenarios for which a "generalized" type & member
remapping solution would be useful:

 1. Desugaring
 2. Microsoft Intune MAM

Note: this new support is only present when targeting .NET 6+,
and will not (currently) be present in Classic Xamarin.Android.

~~ Desugaring ~~

Context: dotnet/android#4574 (comment)
Context: dotnet/android#6142 (comment)

The [Android Runtime][0] is responsible for running the Dalvik
bytecode within e.g. `classes.dex`.  The Android runtime and Dalvik
bytecode formats have apparently changed over time in order to
support newer Java language features, such as support for static
interface methods:

	package org.webrtc;

	public /* partial */ interface EGLBase {
	    public static EGLBase create() { /* … */}
	}

Support for static interface methods was added in Java 8 and in
Android v8.0 (API-26).  If you want to use static interface methods
on an Android device running API-25 or earlier, you must [desugar][1]
the Java Bytecode.  The result of [desugaring][2] the above
`org.webrtc.EGLBase` type is that the static methods are moved to a
*new* type, with a `$-CC` suffix, "as if" the Java code were instead:

	package org.webrtc;

	public /* partial */ interface EGLBase {
	    // …
	}

	public final /* partial */ class EGLBase$-CC {
	    public static EGLBase create { /* … */ }
	}

While this works for pure Java code, this *does not work* with
Xamarin.Android, because our bindings currently expect (require) that
the types & members our binding process detects don't "move":

	// C# Binding for EGLBase
	namespace Org.Webrtc {
	    public partial interface IEGLBase {
	        private static readonly JniPeerMembers _members = new JniPeerMembers ("org/webrtc/EGLBase", typeof (IEGLBase));
	        public static IEGLBase Create()
	        {
	            const string __id = "create.()Lorg/webrtc/EglBase;"
	            var __rm = _members.StaticMethods.InvokeObjectMethod (__id, null);
	            return Java.Lang.Object.GetObject<IEGLBase>(__rm.Handle, JniHandleOwnership.TransferLocalRef);
	        }
	    }
	}

The `new JniPeerMembers(…)` invocation will use `JNIEnv::FindClass()`
to lookup the `org/webrtc/EGLBase` type, and `IEGLBase.Create()` will
use `JNIEnv::GetStaticMethodID()` and `JNIEnv::CallStaticObjectMethod()`
to lookup and invoke the `EGLBase.create()` method.

However, when desugaring is used, *there is no* `EGLBase.create()`
method.  Consequently, in a "desugared" environment,
`Java.Lang.NoSuchMethodError` will be thrown when `IEGLBase.Create()`
is invoked, as `EGLBase.create()` doesn't exist; it "should" instead
be looking for `EGLBase$-CC.create()`!

Introduce partial support for this scenario by adding the new method:

	namespace Java.Interop {
	    public partial class JniRuntime {
	        public partial class JniTypeManager {
	            public IReadOnlyList<string>? GetStaticMethodFallbackTypes (string jniSimpleReference) =>
	                GetStaticMethodFallbackTypesCore (jniSimpleReference);
	            protected virtual IReadOnlyList<string>? GetStaticMethodFallbackTypesCore (string jniSimpleReference) => null;
	        }
	    }
	}

`JniPeerMembers.JniStaticMethods.GetMethodInfo()` will attempt to
lookup the static method, "as normal".  If the method cannot be
found, then `JniRuntime.JniTypeManager.GetStaticMethodFallbackTypes()`
will be called, and we'll attempt to resolve the static method on each
type returned by `GetStaticMethodFallbackTypes()`.
If `GetStaticMethodFallbackTypes()` returns `null` or no fallback type
provides the method, then a `NoSuchMethodError` will be thrown.

TODO: Update xamarin-android to override
`GetStaticMethodFallbackTypes()`, to return
`$"{jniSimpleReference}$-CC"`.

~~ Microsoft Intune MAM ~~

Context: https://www.nuget.org/packages/Microsoft.Intune.MAM.Remapper.Tasks/
Context: https://docs.microsoft.com/en-us/mem/intune/developer/app-sdk-xamarin#remapper

The [Microsoft Intune App SDK Xamarin Bindings][3] is in a
similar-yet-different position: it involves Java & Dalvik Bytecode
manipulation for various security purposes, and in order to make that
work reasonably from Xamarin.Android, they *also* rewrite
Xamarin.Android IL so that it's consistent with the manipulated
Dalvik bytecode.

See `readme.md` and `content/MonoAndroid10/remapping-config.json`
within the [`Microsoft.Intune.MAM.Remapper.Tasks NuGet package`][4]
for some additional details/tidbits such as:

> This task operates on assemblies to replace the base type classes
> which Microsoft Intune requires to implement its SDK (for example,
> Intune requires using a `MAMActivity` in place of `Activity` and
> methods such as `OnMAMCreate` instead of `OnCreate`).

`remapping-config.json` contains JSON fragments such as:

	"ClassRewrites": [
	  {
	    "Class": {
	      "From": "android.app.Activity",
	      "To": "com.microsoft.intune.mam.client.app.MAMActivity"
	    },
	    "Methods": [
	      {
	        "MakeStatic": false,
	        "OriginalName": "onCreate",
	        "NewName": "onMAMCreate",
	        "OriginalParams": [
	          "android.os.Bundle"
	        ]
	      }
	    ]
	  },
	  {
	    "Class": {
	      "From": "android.content.pm.PackageManager",
	      "To": "com.microsoft.intune.mam.client.content.pm.MAMPackageManagement"
	    },
	    "Methods": [
	      {
	        "MakeStatic": true,
	        "OriginalName": "checkPermission",
	      }
	    ]
	  }
	]
	"GlobalMethodCalls": [
	  {
	    "Class": {
	      "From": "android.app.PendingIntent",
	      "To": "com.microsoft.intune.mam.client.app.MAMPendingIntent"
	    },
	    "Methods": [
	      {
	        "MakeStatic": false,
	        "OriginalName": "getActivities"
	      },
	    ]
	  }
	]

While what the InTune team has *works*, it suffers from a number of
"workflow" problems, e.g. incremental builds are problematic (they
might try to rewrite assemblies which were already rewritten), IL
rewriting is *always* "fun", and if we change IL binding patterns,
they'll need to support previous "binding styles" and newer styles.

Introduce partial support for this scenario by adding the following
members to `Java.Interop.JniRuntime.JniTypeManager`:

	namespace Java.Interop {
	    public partial class JniRuntime {

	        public struct ReplacementMethodInfo {
	            public  string? SourceJniType                   {get; set;}
	            public  string? SourceJniMethodName             {get; set;}
	            public  string? SourceJniMethodSignature        {get; set;}
	            public  string? TargetJniType                   {get; set;}
	            public  string? TargetJniMethodName             {get; set;}
	            public  string? TargetJniMethodSignature        {get; set;}
	            public  int?    TargetJniMethodParameterCount   {get; set;}
	            public  bool    TargetJniMethodInstanceToStatic {get; set;}
	        }

	        public partial class JniTypeManager {

	            public string? GetReplacementType (string jniSimpleReference) =>
	                GetReplacementTypeCore (jniSimpleReference);
	            protected virtual string? GetReplacementTypeCore (string jniSimpleReference) => null;

	            public ReplacementMethodInfo? GetReplacementMethodInfo (string jniSimpleReference, string jniMethodName, string jniMethodSignature) =>
	                GetReplacementMethodInfoCore (jniSimpleReference, jniMethodName,jniMethodSignature);
	            protected virtual ReplacementMethodInfo? GetReplacementMethodInfoCore (string jniSimpleReference, string jniMethodName, string jniMethodSignature) => null;
	        }
	    }
	}

These new methods are used by `JniPeerMembers`.

Consider "normal" usage of `JniPeerMembers`, within a binding:

	partial class Activity {
	    static readonly JniPeerMembers _members = new XAPeerMembers (jniPeerTypeName:"android/app/Activity", managedPeerType:typeof (Activity));
	}

`JniRuntime.JniTypeManager.GetReplacementType()` allows "replacing"
the `jniPeerTypeName` value with a "replacement" JNI type name.
The "replacement" type will be used for all field and method lookups.
This allows supporting the `ClassRewrites[0].Class.From` /
`ClassRewrites[0].Class.To` semantics.

`JniRuntime.JniTypeManager.GetReplacementMethodInfo()` is the key
integration for all other "replacement" semantics.  Given the
source type, source method name, and source JNI signature, it is
responsible for providing an *overriding* target type, target method
name, and target JNI signature.

For `ClassRewrites[0].Methods[0]`, this allows "renaming"
`Activity.onCreate()` to `MAMActivity.onMAMCreate()`:

	var r = JniEnvironment.Runtime.TypeManager.GetReplacementMethodInfo (
	    "android/app/Activity",
	    "onCreate",
	    "(Landroid/os/Bundle;)V"
	);
	// is equivalent to:
	var r = new JniRuntime.ReplacementMethodInfo {
	    SourceJniType                   = "android/app/Activity",   // from input parameter
	    SourceJniMethodName             = "onCreate",               // from input parameter
	    SourceJniMethodSignature        = "(Landroid/os/Bundle;)V", // from input parameter

	    TargetJniType                   = "com/microsoft/intune/mam/client/app/MAMActivity",
	    TargetJniMethodName             = "onMAMCreate",
	    SourceJniMethodSignature        = null,                     // "use original value"
	    TargetJniMethodParameterCount   = null,                     // unchanged
	    TargetJniMethodInstanceToStatic = false,
	}

`ClassRewrites[1].Methods[0]` is "weird", in that it has
`MakeStatic: true`.  The semantics for when `MakeStatic: true` exists
is that the "original" method is an *instance* method, and the target
method is a *`static`* method, *and* the `this` instance now becomes
a *parameter* to the method.  This is likewise supported via
`JniRuntime.JniTypeManager.GetReplacementMethodInfo()`, and is
identified by:

 1. `ReplacementMethodInfo.TargetJniMethodParameterCount` being a
    non-`null` value, and

 2. `ReplacementMethodInfo.TargetJniMethodInstanceToStatic` is true.

Thus, `ClassRewrites[1].Methods[0]` is akin to:

	var r = JniEnvironment.Runtime.TypeManager.GetReplacementMethodInfo (
	    "android/content/pm/PackageManager",
	    "checkPermission",
	    "(Ljava/lang/String;Ljava/lang/String;)I"
	);
	// is equivalent to:
	var r = new JniRuntime.ReplacementMethodInfo {
	    SourceJniType                   = "android/content/pm/PackageManager",              // from input parameter
	    SourceJniMethodName             = "checkPermission",                                // from input parameter
	    SourceJniMethodSignature        = "(Ljava/lang/String;Ljava/lang/String;)I",        // from input parameter

	    TargetJniType                   = "com/microsoft/intune/mam/client/content/pm/MAMPackageManagement",
	    TargetJniMethodName             = "checkPermission",
	    SourceJniMethodSignature        = "(Landroid/content/pm/PackageManager;Ljava/lang/String;Ljava/lang/String;)I",
	    TargetJniMethodParameterCount   = 3,
	    TargetJniMethodInstanceToStatic = true,
	}

(Note that the subclass is responsible for providing this data.)

`ReplacementMethodInfo.TargetJniMethodParameterCount` must be the
number of parameters that the target method accepts.  This is needed
so that `JniPeerMembers.JniInstanceMethods.TryInvoke*StaticRedirect()`
can appropriately reallocate the `JniArgumentValue*` array, so that
the `this` parameter can be added to the beginning of the parameters.

~~ Overhead? ~~

All these extra invocations into `JniRuntime.JniTypeManager` imply
additional overhead to invoke a Java method.  However, this is all
done during the *first* time a method is looked up, after which the
`JniMethodInfo` instance is *cached* for a given
(type, method, signature) tuple.

To demonstrate overheads, `samples/Hello` has been updated to accept
a new `-t` value; when provided, it invokes the
`java.lang.Object.hashCode()` method 1 million times and calculates
the average invocation overhead:

	% dotnet run --project samples/Hello -- -t
	Object.hashCode: 1000000 invocations. Total=00:00:00.5196758; Average=0.0005196758ms

If we replace the .NET 6 `samples/Hello/bin/Debug/Java.Interop.dll`
assembly with e.g. `bin/Debug/Java.Interop.dll`, we can compare to
performance *without* these new changes, as the changes are only in
the .NET 6 build:

	% \cp bin/Debug/Java.Interop{.dll,.pdb} samples/Hello/bin/Debug
	% dotnet samples/Hello/bin/Debug/Hello.dll -t
	Object.hashCode: 1000000 invocations. Total=00:00:00.5386676; Average=0.0005386676ms

There is a fair bit of variation in `dotnet Hello.dll -t` output,
but they're all roughly similar.  There doesn't appear to be
significant overhead for this particular test.

[0]: https://en.wikipedia.org/wiki/Android_Runtime
[1]: https://developer.android.com/studio/write/java8-support
[2]: https://developer.android.com/studio/write/java8-support-table
[3]: https://docs.microsoft.com/en-us/mem/intune/developer/app-sdk-xamarin
[4]: https://www.nuget.org/packages/Microsoft.Intune.MAM.Remapper.Tasks/
jonpryor added a commit to jonpryor/java.interop that referenced this issue May 19, 2022
Context: dotnet#867

There are two scenarios for which a "generalized" type & member
remapping solution would be useful:

 1. Desugaring
 2. Microsoft Intune MAM

Note: this new support is only present when targeting .NET 6+,
and will not (currently) be present in Classic Xamarin.Android.

~~ Desugaring ~~

Context: dotnet/android#4574 (comment)
Context: dotnet/android#6142 (comment)

The [Android Runtime][0] is responsible for running the Dalvik
bytecode within e.g. `classes.dex`.  The Android runtime and Dalvik
bytecode formats have apparently changed over time in order to
support newer Java language features, such as support for static
interface methods:

	package org.webrtc;

	public /* partial */ interface EGLBase {
	    public static EGLBase create() { /* … */}
	}

Support for static interface methods was added in Java 8 and in
Android v8.0 (API-26).  If you want to use static interface methods
on an Android device running API-25 or earlier, you must [desugar][1]
the Java Bytecode.  The result of [desugaring][2] the above
`org.webrtc.EGLBase` type is that the static methods are moved to a
*new* type, with a `$-CC` suffix, "as if" the Java code were instead:

	package org.webrtc;

	public /* partial */ interface EGLBase {
	    // …
	}

	public final /* partial */ class EGLBase$-CC {
	    public static EGLBase create { /* … */ }
	}

While this works for pure Java code, this *does not work* with
Xamarin.Android, because our bindings currently expect (require) that
the types & members our binding process detects don't "move":

	// C# Binding for EGLBase
	namespace Org.Webrtc {
	    public partial interface IEGLBase {
	        private static readonly JniPeerMembers _members = new JniPeerMembers ("org/webrtc/EGLBase", typeof (IEGLBase));
	        public static IEGLBase Create()
	        {
	            const string __id = "create.()Lorg/webrtc/EglBase;"
	            var __rm = _members.StaticMethods.InvokeObjectMethod (__id, null);
	            return Java.Lang.Object.GetObject<IEGLBase>(__rm.Handle, JniHandleOwnership.TransferLocalRef);
	        }
	    }
	}

The `new JniPeerMembers(…)` invocation will use `JNIEnv::FindClass()`
to lookup the `org/webrtc/EGLBase` type, and `IEGLBase.Create()` will
use `JNIEnv::GetStaticMethodID()` and `JNIEnv::CallStaticObjectMethod()`
to lookup and invoke the `EGLBase.create()` method.

However, when desugaring is used, *there is no* `EGLBase.create()`
method.  Consequently, in a "desugared" environment,
`Java.Lang.NoSuchMethodError` will be thrown when `IEGLBase.Create()`
is invoked, as `EGLBase.create()` doesn't exist; it "should" instead
be looking for `EGLBase$-CC.create()`!

Introduce partial support for this scenario by adding the new method:

	namespace Java.Interop {
	    public partial class JniRuntime {
	        public partial class JniTypeManager {
	            public IReadOnlyList<string>? GetStaticMethodFallbackTypes (string jniSimpleReference) =>
	                GetStaticMethodFallbackTypesCore (jniSimpleReference);
	            protected virtual IReadOnlyList<string>? GetStaticMethodFallbackTypesCore (string jniSimpleReference) => null;
	        }
	    }
	}

`JniPeerMembers.JniStaticMethods.GetMethodInfo()` will attempt to
lookup the static method, "as normal".  If the method cannot be
found, then `JniRuntime.JniTypeManager.GetStaticMethodFallbackTypes()`
will be called, and we'll attempt to resolve the static method on each
type returned by `GetStaticMethodFallbackTypes()`.
If `GetStaticMethodFallbackTypes()` returns `null` or no fallback type
provides the method, then a `NoSuchMethodError` will be thrown.

TODO: Update xamarin-android to override
`GetStaticMethodFallbackTypes()`, to return
`$"{jniSimpleReference}$-CC"`.

~~ Microsoft Intune MAM ~~

Context: https://www.nuget.org/packages/Microsoft.Intune.MAM.Remapper.Tasks/
Context: https://docs.microsoft.com/en-us/mem/intune/developer/app-sdk-xamarin#remapper

The [Microsoft Intune App SDK Xamarin Bindings][3] is in a
similar-yet-different position: it involves Java & Dalvik Bytecode
manipulation for various security purposes, and in order to make that
work reasonably from Xamarin.Android, they *also* rewrite
Xamarin.Android IL so that it's consistent with the manipulated
Dalvik bytecode.

See `readme.md` and `content/MonoAndroid10/remapping-config.json`
within the [`Microsoft.Intune.MAM.Remapper.Tasks NuGet package`][4]
for some additional details/tidbits such as:

> This task operates on assemblies to replace the base type classes
> which Microsoft Intune requires to implement its SDK (for example,
> Intune requires using a `MAMActivity` in place of `Activity` and
> methods such as `OnMAMCreate` instead of `OnCreate`).

`remapping-config.json` contains JSON fragments such as:

	"ClassRewrites": [
	  {
	    "Class": {
	      "From": "android.app.Activity",
	      "To": "com.microsoft.intune.mam.client.app.MAMActivity"
	    },
	    "Methods": [
	      {
	        "MakeStatic": false,
	        "OriginalName": "onCreate",
	        "NewName": "onMAMCreate",
	        "OriginalParams": [
	          "android.os.Bundle"
	        ]
	      }
	    ]
	  },
	  {
	    "Class": {
	      "From": "android.content.pm.PackageManager",
	      "To": "com.microsoft.intune.mam.client.content.pm.MAMPackageManagement"
	    },
	    "Methods": [
	      {
	        "MakeStatic": true,
	        "OriginalName": "checkPermission",
	      }
	    ]
	  }
	]
	"GlobalMethodCalls": [
	  {
	    "Class": {
	      "From": "android.app.PendingIntent",
	      "To": "com.microsoft.intune.mam.client.app.MAMPendingIntent"
	    },
	    "Methods": [
	      {
	        "MakeStatic": false,
	        "OriginalName": "getActivities"
	      },
	    ]
	  }
	]

While what the InTune team has *works*, it suffers from a number of
"workflow" problems, e.g. incremental builds are problematic (they
might try to rewrite assemblies which were already rewritten), IL
rewriting is *always* "fun", and if we change IL binding patterns,
they'll need to support previous "binding styles" and newer styles.

Introduce partial support for this scenario by adding the following
members to `Java.Interop.JniRuntime.JniTypeManager`:

	namespace Java.Interop {
	    public partial class JniRuntime {

	        public struct ReplacementMethodInfo {
	            public  string? SourceJniType                   {get; set;}
	            public  string? SourceJniMethodName             {get; set;}
	            public  string? SourceJniMethodSignature        {get; set;}
	            public  string? TargetJniType                   {get; set;}
	            public  string? TargetJniMethodName             {get; set;}
	            public  string? TargetJniMethodSignature        {get; set;}
	            public  int?    TargetJniMethodParameterCount   {get; set;}
	            public  bool    TargetJniMethodInstanceToStatic {get; set;}
	        }

	        public partial class JniTypeManager {

	            public string? GetReplacementType (string jniSimpleReference) =>
	                GetReplacementTypeCore (jniSimpleReference);
	            protected virtual string? GetReplacementTypeCore (string jniSimpleReference) => null;

	            public ReplacementMethodInfo? GetReplacementMethodInfo (string jniSimpleReference, string jniMethodName, string jniMethodSignature) =>
	                GetReplacementMethodInfoCore (jniSimpleReference, jniMethodName,jniMethodSignature);
	            protected virtual ReplacementMethodInfo? GetReplacementMethodInfoCore (string jniSimpleReference, string jniMethodName, string jniMethodSignature) => null;
	        }
	    }
	}

These new methods are used by `JniPeerMembers`.

Consider "normal" usage of `JniPeerMembers`, within a binding:

	partial class Activity {
	    static readonly JniPeerMembers _members = new XAPeerMembers (jniPeerTypeName:"android/app/Activity", managedPeerType:typeof (Activity));
	}

`JniRuntime.JniTypeManager.GetReplacementType()` allows "replacing"
the `jniPeerTypeName` value with a "replacement" JNI type name.
The "replacement" type will be used for all field and method lookups.
This allows supporting the `ClassRewrites[0].Class.From` /
`ClassRewrites[0].Class.To` semantics.

`JniRuntime.JniTypeManager.GetReplacementMethodInfo()` is the key
integration for all other "replacement" semantics.  Given the
source type, source method name, and source JNI signature, it is
responsible for providing an *overriding* target type, target method
name, and target JNI signature.

For `ClassRewrites[0].Methods[0]`, this allows "renaming"
`Activity.onCreate()` to `MAMActivity.onMAMCreate()`:

	var r = JniEnvironment.Runtime.TypeManager.GetReplacementMethodInfo (
	    "android/app/Activity",
	    "onCreate",
	    "(Landroid/os/Bundle;)V"
	);
	// is equivalent to:
	var r = new JniRuntime.ReplacementMethodInfo {
	    SourceJniType                   = "android/app/Activity",   // from input parameter
	    SourceJniMethodName             = "onCreate",               // from input parameter
	    SourceJniMethodSignature        = "(Landroid/os/Bundle;)V", // from input parameter

	    TargetJniType                   = "com/microsoft/intune/mam/client/app/MAMActivity",
	    TargetJniMethodName             = "onMAMCreate",
	    SourceJniMethodSignature        = null,                     // "use original value"
	    TargetJniMethodParameterCount   = null,                     // unchanged
	    TargetJniMethodInstanceToStatic = false,
	}

`ClassRewrites[1].Methods[0]` is "weird", in that it has
`MakeStatic: true`.  The semantics for when `MakeStatic: true` exists
is that the "original" method is an *instance* method, and the target
method is a *`static`* method, *and* the `this` instance now becomes
a *parameter* to the method.  This is likewise supported via
`JniRuntime.JniTypeManager.GetReplacementMethodInfo()`, and is
identified by:

 1. `ReplacementMethodInfo.TargetJniMethodParameterCount` being a
    non-`null` value, and

 2. `ReplacementMethodInfo.TargetJniMethodInstanceToStatic` is true.

Thus, `ClassRewrites[1].Methods[0]` is akin to:

	var r = JniEnvironment.Runtime.TypeManager.GetReplacementMethodInfo (
	    "android/content/pm/PackageManager",
	    "checkPermission",
	    "(Ljava/lang/String;Ljava/lang/String;)I"
	);
	// is equivalent to:
	var r = new JniRuntime.ReplacementMethodInfo {
	    SourceJniType                   = "android/content/pm/PackageManager",              // from input parameter
	    SourceJniMethodName             = "checkPermission",                                // from input parameter
	    SourceJniMethodSignature        = "(Ljava/lang/String;Ljava/lang/String;)I",        // from input parameter

	    TargetJniType                   = "com/microsoft/intune/mam/client/content/pm/MAMPackageManagement",
	    TargetJniMethodName             = "checkPermission",
	    SourceJniMethodSignature        = "(Landroid/content/pm/PackageManager;Ljava/lang/String;Ljava/lang/String;)I",
	    TargetJniMethodParameterCount   = 3,
	    TargetJniMethodInstanceToStatic = true,
	}

(Note that the subclass is responsible for providing this data.)

`ReplacementMethodInfo.TargetJniMethodParameterCount` must be the
number of parameters that the target method accepts.  This is needed
so that `JniPeerMembers.JniInstanceMethods.TryInvoke*StaticRedirect()`
can appropriately reallocate the `JniArgumentValue*` array, so that
the `this` parameter can be added to the beginning of the parameters.

~~ Overhead? ~~

All these extra invocations into `JniRuntime.JniTypeManager` imply
additional overhead to invoke a Java method.  However, this is all
done during the *first* time a method is looked up, after which the
`JniMethodInfo` instance is *cached* for a given
(type, method, signature) tuple.

To demonstrate overheads, `samples/Hello` has been updated to accept
a new `-t` value; when provided, it invokes the
`java.lang.Object.hashCode()` method 1 million times and calculates
the average invocation overhead:

	% dotnet run --project samples/Hello -- -t
	Object.hashCode: 1000000 invocations. Total=00:00:00.5196758; Average=0.0005196758ms

If we replace the .NET 6 `samples/Hello/bin/Debug/Java.Interop.dll`
assembly with e.g. `bin/Debug/Java.Interop.dll`, we can compare to
performance *without* these new changes, as the changes are only in
the .NET 6 build:

	% \cp bin/Debug/Java.Interop{.dll,.pdb} samples/Hello/bin/Debug
	% dotnet samples/Hello/bin/Debug/Hello.dll -t
	Object.hashCode: 1000000 invocations. Total=00:00:00.5386676; Average=0.0005386676ms

There is a fair bit of variation in `dotnet Hello.dll -t` output,
but they're all roughly similar.  There doesn't appear to be
significant overhead for this particular test.

[0]: https://en.wikipedia.org/wiki/Android_Runtime
[1]: https://developer.android.com/studio/write/java8-support
[2]: https://developer.android.com/studio/write/java8-support-table
[3]: https://docs.microsoft.com/en-us/mem/intune/developer/app-sdk-xamarin
[4]: https://www.nuget.org/packages/Microsoft.Intune.MAM.Remapper.Tasks/
jonpryor added a commit to jonpryor/java.interop that referenced this issue May 19, 2022
Context: dotnet#867

There are two scenarios for which a "generalized" type & member
remapping solution would be useful:

 1. Desugaring
 2. Microsoft Intune MAM

Note: this new support is only present when targeting .NET 6+,
and will not (currently) be present in Classic Xamarin.Android.

~~ Desugaring ~~

Context: dotnet/android#4574 (comment)
Context: dotnet/android#6142 (comment)

The [Android Runtime][0] is responsible for running the Dalvik
bytecode within e.g. `classes.dex`.  The Android runtime and Dalvik
bytecode formats have apparently changed over time in order to
support newer Java language features, such as support for static
interface methods:

	package org.webrtc;

	public /* partial */ interface EGLBase {
	    public static EGLBase create() { /* … */}
	}

Support for static interface methods was added in Java 8 and in
Android v8.0 (API-26).  If you want to use static interface methods
on an Android device running API-25 or earlier, you must [desugar][1]
the Java Bytecode.  The result of [desugaring][2] the above
`org.webrtc.EGLBase` type is that the static methods are moved to a
*new* type, with a `$-CC` suffix, "as if" the Java code were instead:

	package org.webrtc;

	public /* partial */ interface EGLBase {
	    // …
	}

	public final /* partial */ class EGLBase$-CC {
	    public static EGLBase create { /* … */ }
	}

While this works for pure Java code, this *does not work* with
Xamarin.Android, because our bindings currently expect (require) that
the types & members our binding process detects don't "move":

	// C# Binding for EGLBase
	namespace Org.Webrtc {
	    public partial interface IEGLBase {
	        private static readonly JniPeerMembers _members = new JniPeerMembers ("org/webrtc/EGLBase", typeof (IEGLBase));
	        public static IEGLBase Create()
	        {
	            const string __id = "create.()Lorg/webrtc/EglBase;"
	            var __rm = _members.StaticMethods.InvokeObjectMethod (__id, null);
	            return Java.Lang.Object.GetObject<IEGLBase>(__rm.Handle, JniHandleOwnership.TransferLocalRef);
	        }
	    }
	}

The `new JniPeerMembers(…)` invocation will use `JNIEnv::FindClass()`
to lookup the `org/webrtc/EGLBase` type, and `IEGLBase.Create()` will
use `JNIEnv::GetStaticMethodID()` and `JNIEnv::CallStaticObjectMethod()`
to lookup and invoke the `EGLBase.create()` method.

However, when desugaring is used, *there is no* `EGLBase.create()`
method.  Consequently, in a "desugared" environment,
`Java.Lang.NoSuchMethodError` will be thrown when `IEGLBase.Create()`
is invoked, as `EGLBase.create()` doesn't exist; it "should" instead
be looking for `EGLBase$-CC.create()`!

Introduce partial support for this scenario by adding the new method:

	namespace Java.Interop {
	    public partial class JniRuntime {
	        public partial class JniTypeManager {
	            public IReadOnlyList<string>? GetStaticMethodFallbackTypes (string jniSimpleReference) =>
	                GetStaticMethodFallbackTypesCore (jniSimpleReference);
	            protected virtual IReadOnlyList<string>? GetStaticMethodFallbackTypesCore (string jniSimpleReference) => null;
	        }
	    }
	}

`JniPeerMembers.JniStaticMethods.GetMethodInfo()` will attempt to
lookup the static method, "as normal".  If the method cannot be
found, then `JniRuntime.JniTypeManager.GetStaticMethodFallbackTypes()`
will be called, and we'll attempt to resolve the static method on each
type returned by `GetStaticMethodFallbackTypes()`.
If `GetStaticMethodFallbackTypes()` returns `null` or no fallback type
provides the method, then a `NoSuchMethodError` will be thrown.

TODO: Update xamarin-android to override
`GetStaticMethodFallbackTypes()`, to return
`$"{jniSimpleReference}$-CC"`.

~~ Microsoft Intune MAM ~~

Context: https://www.nuget.org/packages/Microsoft.Intune.MAM.Remapper.Tasks/
Context: https://docs.microsoft.com/en-us/mem/intune/developer/app-sdk-xamarin#remapper

The [Microsoft Intune App SDK Xamarin Bindings][3] is in a
similar-yet-different position: it involves Java & Dalvik Bytecode
manipulation for various security purposes, and in order to make that
work reasonably from Xamarin.Android, they *also* rewrite
Xamarin.Android IL so that it's consistent with the manipulated
Dalvik bytecode.

See `readme.md` and `content/MonoAndroid10/remapping-config.json`
within the [`Microsoft.Intune.MAM.Remapper.Tasks NuGet package`][4]
for some additional details/tidbits such as:

> This task operates on assemblies to replace the base type classes
> which Microsoft Intune requires to implement its SDK (for example,
> Intune requires using a `MAMActivity` in place of `Activity` and
> methods such as `OnMAMCreate` instead of `OnCreate`).

`remapping-config.json` contains JSON fragments such as:

	"ClassRewrites": [
	  {
	    "Class": {
	      "From": "android.app.Activity",
	      "To": "com.microsoft.intune.mam.client.app.MAMActivity"
	    },
	    "Methods": [
	      {
	        "MakeStatic": false,
	        "OriginalName": "onCreate",
	        "NewName": "onMAMCreate",
	        "OriginalParams": [
	          "android.os.Bundle"
	        ]
	      }
	    ]
	  },
	  {
	    "Class": {
	      "From": "android.content.pm.PackageManager",
	      "To": "com.microsoft.intune.mam.client.content.pm.MAMPackageManagement"
	    },
	    "Methods": [
	      {
	        "MakeStatic": true,
	        "OriginalName": "checkPermission",
	      }
	    ]
	  }
	]
	"GlobalMethodCalls": [
	  {
	    "Class": {
	      "From": "android.app.PendingIntent",
	      "To": "com.microsoft.intune.mam.client.app.MAMPendingIntent"
	    },
	    "Methods": [
	      {
	        "MakeStatic": false,
	        "OriginalName": "getActivities"
	      },
	    ]
	  }
	]

While what the InTune team has *works*, it suffers from a number of
"workflow" problems, e.g. incremental builds are problematic (they
might try to rewrite assemblies which were already rewritten), IL
rewriting is *always* "fun", and if we change IL binding patterns,
they'll need to support previous "binding styles" and newer styles.

Introduce partial support for this scenario by adding the following
members to `Java.Interop.JniRuntime.JniTypeManager`:

	namespace Java.Interop {
	    public partial class JniRuntime {

	        public struct ReplacementMethodInfo {
	            public  string? SourceJniType                   {get; set;}
	            public  string? SourceJniMethodName             {get; set;}
	            public  string? SourceJniMethodSignature        {get; set;}
	            public  string? TargetJniType                   {get; set;}
	            public  string? TargetJniMethodName             {get; set;}
	            public  string? TargetJniMethodSignature        {get; set;}
	            public  int?    TargetJniMethodParameterCount   {get; set;}
	            public  bool    TargetJniMethodInstanceToStatic {get; set;}
	        }

	        public partial class JniTypeManager {

	            public string? GetReplacementType (string jniSimpleReference) =>
	                GetReplacementTypeCore (jniSimpleReference);
	            protected virtual string? GetReplacementTypeCore (string jniSimpleReference) => null;

	            public ReplacementMethodInfo? GetReplacementMethodInfo (string jniSimpleReference, string jniMethodName, string jniMethodSignature) =>
	                GetReplacementMethodInfoCore (jniSimpleReference, jniMethodName,jniMethodSignature);
	            protected virtual ReplacementMethodInfo? GetReplacementMethodInfoCore (string jniSimpleReference, string jniMethodName, string jniMethodSignature) => null;
	        }
	    }
	}

These new methods are used by `JniPeerMembers`.

Consider "normal" usage of `JniPeerMembers`, within a binding:

	partial class Activity {
	    static readonly JniPeerMembers _members = new XAPeerMembers (jniPeerTypeName:"android/app/Activity", managedPeerType:typeof (Activity));
	}

`JniRuntime.JniTypeManager.GetReplacementType()` allows "replacing"
the `jniPeerTypeName` value with a "replacement" JNI type name.
The "replacement" type will be used for all field and method lookups.
This allows supporting the `ClassRewrites[0].Class.From` /
`ClassRewrites[0].Class.To` semantics.

`JniRuntime.JniTypeManager.GetReplacementMethodInfo()` is the key
integration for all other "replacement" semantics.  Given the
source type, source method name, and source JNI signature, it is
responsible for providing an *overriding* target type, target method
name, and target JNI signature.

For `ClassRewrites[0].Methods[0]`, this allows "renaming"
`Activity.onCreate()` to `MAMActivity.onMAMCreate()`:

	var r = JniEnvironment.Runtime.TypeManager.GetReplacementMethodInfo (
	    "android/app/Activity",
	    "onCreate",
	    "(Landroid/os/Bundle;)V"
	);
	// is equivalent to:
	var r = new JniRuntime.ReplacementMethodInfo {
	    SourceJniType                   = "android/app/Activity",   // from input parameter
	    SourceJniMethodName             = "onCreate",               // from input parameter
	    SourceJniMethodSignature        = "(Landroid/os/Bundle;)V", // from input parameter

	    TargetJniType                   = "com/microsoft/intune/mam/client/app/MAMActivity",
	    TargetJniMethodName             = "onMAMCreate",
	    SourceJniMethodSignature        = null,                     // "use original value"
	    TargetJniMethodParameterCount   = null,                     // unchanged
	    TargetJniMethodInstanceToStatic = false,
	}

`ClassRewrites[1].Methods[0]` is "weird", in that it has
`MakeStatic: true`.  The semantics for when `MakeStatic: true` exists
is that the "original" method is an *instance* method, and the target
method is a *`static`* method, *and* the `this` instance now becomes
a *parameter* to the method.  This is likewise supported via
`JniRuntime.JniTypeManager.GetReplacementMethodInfo()`, and is
identified by:

 1. `ReplacementMethodInfo.TargetJniMethodParameterCount` being a
    non-`null` value, and

 2. `ReplacementMethodInfo.TargetJniMethodInstanceToStatic` is true.

Thus, `ClassRewrites[1].Methods[0]` is akin to:

	var r = JniEnvironment.Runtime.TypeManager.GetReplacementMethodInfo (
	    "android/content/pm/PackageManager",
	    "checkPermission",
	    "(Ljava/lang/String;Ljava/lang/String;)I"
	);
	// is equivalent to:
	var r = new JniRuntime.ReplacementMethodInfo {
	    SourceJniType                   = "android/content/pm/PackageManager",              // from input parameter
	    SourceJniMethodName             = "checkPermission",                                // from input parameter
	    SourceJniMethodSignature        = "(Ljava/lang/String;Ljava/lang/String;)I",        // from input parameter

	    TargetJniType                   = "com/microsoft/intune/mam/client/content/pm/MAMPackageManagement",
	    TargetJniMethodName             = "checkPermission",
	    SourceJniMethodSignature        = "(Landroid/content/pm/PackageManager;Ljava/lang/String;Ljava/lang/String;)I",
	    TargetJniMethodParameterCount   = 3,
	    TargetJniMethodInstanceToStatic = true,
	}

(Note that the subclass is responsible for providing this data.)

`ReplacementMethodInfo.TargetJniMethodParameterCount` must be the
number of parameters that the target method accepts.  This is needed
so that `JniPeerMembers.JniInstanceMethods.TryInvoke*StaticRedirect()`
can appropriately reallocate the `JniArgumentValue*` array, so that
the `this` parameter can be added to the beginning of the parameters.

~~ Overhead? ~~

All these extra invocations into `JniRuntime.JniTypeManager` imply
additional overhead to invoke a Java method.  However, this is all
done during the *first* time a method is looked up, after which the
`JniMethodInfo` instance is *cached* for a given
(type, method, signature) tuple.

To demonstrate overheads, `samples/Hello` has been updated to accept
a new `-t` value; when provided, it invokes the
`java.lang.Object.hashCode()` method 1 million times and calculates
the average invocation overhead:

	% dotnet run --project samples/Hello -- -t
	Object.hashCode: 1000000 invocations. Total=00:00:00.5196758; Average=0.0005196758ms

If we replace the .NET 6 `samples/Hello/bin/Debug/Java.Interop.dll`
assembly with e.g. `bin/Debug/Java.Interop.dll`, we can compare to
performance *without* these new changes, as the changes are only in
the .NET 6 build:

	% \cp bin/Debug/Java.Interop{.dll,.pdb} samples/Hello/bin/Debug
	% dotnet samples/Hello/bin/Debug/Hello.dll -t
	Object.hashCode: 1000000 invocations. Total=00:00:00.5386676; Average=0.0005386676ms

There is a fair bit of variation in `dotnet Hello.dll -t` output,
but they're all roughly similar.  There doesn't appear to be
significant overhead for this particular test.

[0]: https://en.wikipedia.org/wiki/Android_Runtime
[1]: https://developer.android.com/studio/write/java8-support
[2]: https://developer.android.com/studio/write/java8-support-table
[3]: https://docs.microsoft.com/en-us/mem/intune/developer/app-sdk-xamarin
[4]: https://www.nuget.org/packages/Microsoft.Intune.MAM.Remapper.Tasks/
jonpryor added a commit to dotnet/java-interop that referenced this issue May 20, 2022
Context: #867

There are two scenarios for which a "generalized" type & member
remapping solution would be useful:

 1. Desugaring
 2. Microsoft Intune MAM

Note: this new support is only present when targeting .NET 6+,
and will not (currently) be present in Classic Xamarin.Android.

~~ Desugaring ~~

Context: dotnet/android#4574 (comment)
Context: dotnet/android#6142 (comment)

The [Android Runtime][0] is responsible for running the Dalvik
bytecode within e.g. `classes.dex`.  The Android runtime and Dalvik
bytecode formats have apparently changed over time in order to
support newer Java language features, such as support for static
interface methods:

	package org.webrtc;

	public /* partial */ interface EGLBase {
	    public static EGLBase create() { /* … */}
	}

Support for static interface methods was added in Java 8 and in
Android v8.0 (API-26).  If you want to use static interface methods
on an Android device running API-25 or earlier, you must [desugar][1]
the Java Bytecode.  The result of [desugaring][2] the above
`org.webrtc.EGLBase` type is that the static methods are moved to a
*new* type, with a `$-CC` suffix, "as if" the Java code were instead:

	package org.webrtc;

	public /* partial */ interface EGLBase {
	    // …
	}

	public final /* partial */ class EGLBase$-CC {
	    public static EGLBase create { /* … */ }
	}

While this works for pure Java code, this *does not work* with
Xamarin.Android, because our bindings currently expect (require) that
the types & members our binding process detects don't "move":

	// C# Binding for EGLBase
	namespace Org.Webrtc {
	    public partial interface IEGLBase {
	        private static readonly JniPeerMembers _members = new JniPeerMembers ("org/webrtc/EGLBase", typeof (IEGLBase));
	        public static IEGLBase Create()
	        {
	            const string __id = "create.()Lorg/webrtc/EglBase;"
	            var __rm = _members.StaticMethods.InvokeObjectMethod (__id, null);
	            return Java.Lang.Object.GetObject<IEGLBase>(__rm.Handle, JniHandleOwnership.TransferLocalRef);
	        }
	    }
	}

The `new JniPeerMembers(…)` invocation will use `JNIEnv::FindClass()`
to lookup the `org/webrtc/EGLBase` type, and `IEGLBase.Create()` will
use `JNIEnv::GetStaticMethodID()` and `JNIEnv::CallStaticObjectMethod()`
to lookup and invoke the `EGLBase.create()` method.

However, when desugaring is used, *there is no* `EGLBase.create()`
method.  Consequently, in a "desugared" environment,
`Java.Lang.NoSuchMethodError` will be thrown when `IEGLBase.Create()`
is invoked, as `EGLBase.create()` doesn't exist; it "should" instead
be looking for `EGLBase$-CC.create()`!

Introduce partial support for this scenario by adding the new method:

	namespace Java.Interop {
	    public partial class JniRuntime {
	        public partial class JniTypeManager {
	            public IReadOnlyList<string>? GetStaticMethodFallbackTypes (string jniSimpleReference) =>
	                GetStaticMethodFallbackTypesCore (jniSimpleReference);
	            protected virtual IReadOnlyList<string>? GetStaticMethodFallbackTypesCore (string jniSimpleReference) => null;
	        }
	    }
	}

`JniPeerMembers.JniStaticMethods.GetMethodInfo()` will attempt to
lookup the static method, "as normal".  If the method cannot be
found, then `JniRuntime.JniTypeManager.GetStaticMethodFallbackTypes()`
will be called, and we'll attempt to resolve the static method on each
type returned by `GetStaticMethodFallbackTypes()`.
If `GetStaticMethodFallbackTypes()` returns `null` or no fallback type
provides the method, then a `NoSuchMethodError` will be thrown.

TODO: Update xamarin-android to override
`GetStaticMethodFallbackTypes()`, to return
`$"{jniSimpleReference}$-CC"`.

~~ Microsoft Intune MAM ~~

Context: https://www.nuget.org/packages/Microsoft.Intune.MAM.Remapper.Tasks/
Context: https://docs.microsoft.com/en-us/mem/intune/developer/app-sdk-xamarin#remapper

The [Microsoft Intune App SDK Xamarin Bindings][3] is in a
similar-yet-different position: it involves Java & Dalvik Bytecode
manipulation for various security purposes, and in order to make that
work reasonably from Xamarin.Android, they *also* rewrite
Xamarin.Android IL so that it's consistent with the manipulated
Dalvik bytecode.

See `readme.md` and `content/MonoAndroid10/remapping-config.json`
within the [`Microsoft.Intune.MAM.Remapper.Tasks NuGet package`][4]
for some additional details/tidbits such as:

> This task operates on assemblies to replace the base type classes
> which Microsoft Intune requires to implement its SDK (for example,
> Intune requires using a `MAMActivity` in place of `Activity` and
> methods such as `OnMAMCreate` instead of `OnCreate`).

	"ClassRewrites": [
	  {
	    "Class": {
	      "From": "android.app.Activity",
	      "To": "com.microsoft.intune.mam.client.app.MAMActivity"
	    },
	    "Methods": [
	      {
	        "MakeStatic": false,
	        "OriginalName": "onCreate",
	        "NewName": "onMAMCreate",
	        "OriginalParams": [
	          "android.os.Bundle"
	        ]
	      }
	    ]
	  },
	  {	
	    "Class": {
	      "From": "android.content.pm.PackageManager",
	      "To": "com.microsoft.intune.mam.client.content.pm.MAMPackageManagement"
	    },
	    "Methods": [
	      {
	        "MakeStatic": true,
	        "OriginalName": "checkPermission",
	      }
	    ]
	  }
	]
	"GlobalMethodCalls": [
	  {
	    "Class": {
	      "From": "android.app.PendingIntent",
	      "To": "com.microsoft.intune.mam.client.app.MAMPendingIntent"
	    },
	    "Methods": [
	      {
	        "MakeStatic": false,
	        "OriginalName": "getActivities"
	      },
	    ]
	  }
	]

While what the InTune team has *works*, it suffers from a number of
"workflow" problems, e.g. incremental builds are problematic (they
might try to rewrite assemblies which were already rewritten), IL
rewriting is *always* "fun", and if we change IL binding patterns,
they'll need to support previous "binding styles" and newer styles.

Introduce partial support for this scenario by adding the following
members to `Java.Interop.JniRuntime.JniTypeManager`:

	namespace Java.Interop {
	    public partial class JniRuntime {

	        public struct ReplacementMethodInfo {
	            public  string? SourceJniType                   {get; set;}
	            public  string? SourceJniMethodName             {get; set;}
	            public  string? SourceJniMethodSignature        {get; set;}
	            public  string? TargetJniType                   {get; set;}
	            public  string? TargetJniMethodName             {get; set;}
	            public  string? TargetJniMethodSignature        {get; set;}
	            public  int?    TargetJniMethodParameterCount   {get; set;}
	            public  bool    TargetJniMethodIsStatic         {get; set;}
	        }

	        public partial class JniTypeManager {

	            public string? GetReplacementType (string jniSimpleReference) =>
	                GetReplacementTypeCore (jniSimpleReference);
	            protected virtual string? GetReplacementTypeCore (string jniSimpleReference) => null;

	            public ReplacementMethodInfo? GetReplacementMethodInfo (string jniSimpleReference, string jniMethodName, string jniMethodSignature) =>
	                GetReplacementMethodInfoCore (jniSimpleReference, jniMethodName,jniMethodSignature);
	            protected virtual ReplacementMethodInfo? GetReplacementMethodInfoCore (string jniSimpleReference, string jniMethodName, string jniMethodSignature) => null;
	        }
	    }
	}

These new methods are used by `JniPeerMembers`.

Consider "normal" usage of `JniPeerMembers`, within a binding:

	partial class Activity {
	    static readonly JniPeerMembers _members = new XAPeerMembers (jniPeerTypeName:"android/app/Activity", managedPeerType:typeof (Activity));
	}

`JniRuntime.JniTypeManager.GetReplacementType()` allows "replacing"
the `jniPeerTypeName` value with a "replacement" JNI type name.
The "replacement" type will be used for all field and method lookups.
This allows supporting the `ClassRewrites[0].Class.From` / 
`ClassRewrites[0].Class.To` semantics.

`JniRuntime.JniTypeManager.GetReplacementMethodInfo()` is the key
integration for all other "replacement" semantics.  Given the
source type, source method name, and source JNI signature, it is
responsible for providing an *overriding* target type, target method
name, and target JNI signature.

For `ClassRewrites[0].Methods[0]`, this allows "renaming"
`Activity.onCreate()` to `MAMActivity.onMAMCreate()`:

	var r = JniEnvironment.Runtime.TypeManager.GetReplacementMethodInfo (
	    // Note: must match GetReplacementType() *output*, and since
	    // `Activity` is mapped to `MAMActivity`…
	    "com/microsoft/intune/mam/client/app/MAMActivity",
	    "onCreate",
	    "(Landroid/os/Bundle;)V"
	);
	// is equivalent to:
	var r = new JniRuntime.ReplacementMethodInfo {
	    SourceJniType                   = "com/microsoft/intune/mam/client/app/MAMActivity",
	                                                                // from input parameter
	    SourceJniMethodName             = "onCreate",               // from input parameter
	    SourceJniMethodSignature        = "(Landroid/os/Bundle;)V", // from input parameter

	    TargetJniType                   = "com/microsoft/intune/mam/client/app/MAMActivity",
	    TargetJniMethodName             = "onMAMCreate",
	    TargetJniMethodSignature        = null,                     // "use original value"
	    TargetJniMethodParameterCount   = null,                     // unchanged
	    TargetJniMethodIsStatic         = false,
	}

`ClassRewrites[1].Methods[0]` is "weird", in that it has
`MakeStatic: true`.  The semantics for when `MakeStatic: true` exists
is that the "original" method is an *instance* method, and the target
method is a *`static`* method, *and* the `this` instance now becomes
a *parameter* to the method.  This is likewise supported via
`JniRuntime.JniTypeManager.GetReplacementMethodInfo()`, and is
identified by:

 1. `ReplacementMethodInfo.TargetJniMethodParameterCount` being a
    non-`null` value, and

 2. `ReplacementMethodInfo.TargetJniMethodIsStatic` is true.

Thus, `ClassRewrites[1].Methods[0]` is akin to:

	var r = JniEnvironment.Runtime.TypeManager.GetReplacementMethodInfo (
	    "android/content/pm/PackageManager",
	    "checkPermission",
	    "(Ljava/lang/String;Ljava/lang/String;)I"
	);
	// is equivalent to:
	var r = new JniRuntime.ReplacementMethodInfo {
	    SourceJniType                   = "android/content/pm/PackageManager",          // from input parameter
	    SourceJniMethodName             = "checkPermission",                            // from input parameter
	    SourceJniMethodSignature        = "(Ljava/lang/String;Ljava/lang/String;)I",    // from input parameter

	    TargetJniType                   = "com/microsoft/intune/mam/client/content/pm/MAMPackageManagement",
	    TargetJniMethodName             = "CheckPermission",
	    TargetJniMethodSignature        = "(Landroid/content/pm/PackageManager;Ljava/lang/String;Ljava/lang/String;)I",
	                                      // Note: `PackageManager` is inserted as new first parameter
	    TargetJniMethodParameterCount   = 3,
	    TargetJniMethodIsStatic         = true,
	}

(Note that the subclass is responsible for providing this data.)

`ReplacementMethodInfo.TargetJniMethodParameterCount` must be the
number of parameters that the target method accepts.  This is needed
so that `JniPeerMembers.JniInstanceMethods.TryInvoke*StaticRedirect()`
can appropriately reallocate the `JniArgumentValue*` array, so that
the `this` parameter can be added to the beginning of the parameters.

~~ Overhead? ~~

All these extra invocations into `JniRuntime.JniTypeManager` imply
additional overhead to invoke a Java method.  However, this is all
done during the *first* time a method is looked up, after which the
`JniMethodInfo` instance is *cached* for a given
(type, method, signature) tuple.

To demonstrate overheads, `samples/Hello` has been updated to accept
a new `-t` value; when provided, it invokes the
`java.lang.Object.hashCode()` method 1 million times and calculates
the average invocation overhead:

	% dotnet run --project samples/Hello -- -t
	Object.hashCode: 1000000 invocations. Total=00:00:00.5196758; Average=0.0005196758ms

If we replace the .NET 6 `samples/Hello/bin/Debug/Java.Interop.dll`
assembly with e.g. `bin/Debug/Java.Interop.dll`, we can compare to
performance *without* these new changes, as the changes are only in
the .NET 6 build:

	% \cp bin/Debug/Java.Interop{.dll,.pdb} samples/Hello/bin/Debug
	% dotnet samples/Hello/bin/Debug/Hello.dll -t
	Object.hashCode: 1000000 invocations. Total=00:00:00.5386676; Average=0.0005386676ms

There is a fair bit of variation in `dotnet Hello.dll -t` output,
but they're all roughly similar.  There doesn't appear to be
significant overhead for this particular test.

~~ Other Changes ~~

Cleaned up the `JniPeerMembers` constructors.  The `Debug.Assert()`
calls were duplicative and redundant.

Replace the `Debug.Assert()` with `Debug.WriteLine()`.
`Mono.Android.NET-Tests.apk` was running into an "unfixable" scenario:

	WARNING: ManagedPeerType <=> JniTypeName Mismatch!
	javaVM.GetJniTypeInfoForType(typeof(Android.Runtime.JavaList)).JniTypeName="java/util/ArrayList" != "java/util/List"

This was because of [`Android.Runtime.JavaList`][5] using a
`JniPeerMembers` for `List` while registering `ArrayList`, causing
typemaps to associate `JavaList` with `ArrayList`:

	[Register ("java/util/ArrayList", DoNotGenerateAcw=true)]
	partial class JavaList {
	    internal static readonly JniPeerMembers list_members =
	        new XAPeerMembers ("java/util/List", typeof (JavaList), isInterface: true);
	}

@jonpryor doesn't want to try fixing this right now; turning the
assertion into a diagnostic message feels preferable.

[0]: https://en.wikipedia.org/wiki/Android_Runtime
[1]: https://developer.android.com/studio/write/java8-support
[2]: https://developer.android.com/studio/write/java8-support-table
[3]: https://docs.microsoft.com/en-us/mem/intune/developer/app-sdk-xamarin
[4]: https://www.nuget.org/packages/Microsoft.Intune.MAM.Remapper.Tasks/
[5]: https://github.com/xamarin/xamarin-android/blob/b250c04b09a2b725336ae6d6c5693e0b9f37e4cc/src/Mono.Android/Android.Runtime/JavaList.cs#L9-L13
@jpobst jpobst changed the title Can't call Java static interface methods in android libraries Can't call Java desugared static interface methods in android libraries before API-24 Dec 13, 2022
@jpobst jpobst removed the Area: App Runtime Issues in `libmonodroid.so`. label Dec 13, 2022
@jpobst
Copy link
Contributor

jpobst commented Apr 17, 2024

We believe desugar remapping is now fully supported in net8.0-android and beyond.

dotnet/java-interop#1077

@jpobst jpobst closed this as completed Apr 17, 2024
@github-actions github-actions bot locked and limited conversation to collaborators May 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Area: Bindings Issues in Java Library Binding projects.
Projects
None yet
Development

No branches or pull requests

4 participants