Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 578
[TrimmableTypeMap] Runtime-only trimmable typemap support#11090
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
Merged
simonrozsival
merged 21 commits into
main
from
dev/simonrozsival/trimmable-runtime-fixesApr 10, 2026
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
3a8d509
[TrimmableTypeMap] Runtime trimmable typemap support
simonrozsival d4152df
[TrimmableTypeMap] Keep cached proxy-only JNI lookup
simonrozsival 5d16a12
[TrimmableTypeMap] Remove legacy TypeManager redirect
simonrozsival 656ddcf
[TrimmableTypeMap] Fail fast on missing peer proxies
simonrozsival 5a96a71
[TrimmableTypeMap] Remove duplicate JNI name helper
simonrozsival aa33e56
[TrimmableTypeMap] Prune internal peer helpers
simonrozsival 47089ad
[TrimmableTypeMap] Clarify Java object proxy lookup
simonrozsival c4b567c
[TrimmableTypeMap] Restore CreatePeer guard semantics
simonrozsival 9c5672b
Fix trimmable typemap review issues
simonrozsival 4a68393
Refine trimmable typemap cleanup
simonrozsival 2897a08
Inline sentinel cache handling
simonrozsival 7a7c186
Reuse managed proxy lookup
simonrozsival 48102b6
Rename typemap target lookup
simonrozsival 530b2b2
Extract Java proxy cache helper
simonrozsival 0bad300
Separate proxy lookup from activation
simonrozsival f626700
Refactor Java proxy resolution
simonrozsival 79ddbfb
Inline typemap cache resolution
simonrozsival cb32b91
Tidy typemap helper flow
simonrozsival af4a2df
Fail fast on typemap aliases
simonrozsival 6df784c
Fix Runtime helper qualification
simonrozsival b9f3b1b
Simplify Java proxy target lookup
simonrozsival File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 26 additions & 9 deletions
35 src/Mono.Android/Microsoft.Android.Runtime/JavaMarshalValueManager.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 104 additions & 34 deletions
138 src/Mono.Android/Microsoft.Android.Runtime/TrimmableTypeMap.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -20,6 +20,7 @@ namespace Microsoft.Android.Runtime; | ||
| class TrimmableTypeMap | ||
| { | ||
| static readonly Lock s_initLock = new (); | ||
| static readonly JavaPeerProxy s_noPeerSentinel = new MissingJavaPeerProxy (); | ||
| static TrimmableTypeMap? s_instance; | ||
| internal static TrimmableTypeMap Instance => | ||
| @@ -28,7 +29,8 @@ class TrimmableTypeMap | ||
| readonly IReadOnlyDictionary<string, Type> _typeMap; | ||
| readonly IReadOnlyDictionary<Type, Type> _proxyTypeMap; | ||
| readonly ConcurrentDictionary<Type, JavaPeerProxy?> _proxyCache = new (); | ||
| readonly ConcurrentDictionary<Type, JavaPeerProxy> _proxyCache = new (); | ||
| readonly ConcurrentDictionary<string, JavaPeerProxy> _peerProxyCache = new (StringComparer.Ordinal); | ||
| TrimmableTypeMap () | ||
| { | ||
| @@ -55,9 +57,6 @@ internal static void Initialize () | ||
| } | ||
| } | ||
| /// <summary> | ||
| /// Registers the <c>mono.android.Runtime.registerNatives</c> JNI native method. | ||
| /// </summary> | ||
| unsafe void RegisterNatives () | ||
| { | ||
| using var runtimeClass = new JniType ("mono/android/Runtime"u8); | ||
| @@ -68,49 +67,111 @@ unsafe void RegisterNatives () | ||
| } | ||
| } | ||
| internal bool TryGetType (string jniSimpleReference, [NotNullWhen (true)] out Type? type) | ||
| => _typeMap.TryGetValue (jniSimpleReference, out type); | ||
| internal bool TryGetTargetType (string jniSimpleReference, [NotNullWhen (true)] out Type? type) | ||
| { | ||
| type = GetProxyForJavaType (jniSimpleReference)?.TargetType; | ||
| return type is not null; | ||
| } | ||
| /// <summary> | ||
| /// Finds the proxy for a managed type using the generated proxy type map. | ||
| /// Results are cached per type. | ||
| /// </summary> | ||
| internal JavaPeerProxy? GetProxyForManagedType (Type managedType) | ||
| => _proxyCache.GetOrAdd (managedType, static (type, self) => self.ResolveProxyForManagedType (type), this); | ||
| JavaPeerProxy? GetProxyForManagedType (Type managedType) | ||
| { | ||
| var proxy = _proxyCache.GetOrAdd (managedType, static (type, self) => { | ||
| if (!self._proxyTypeMap.TryGetValue (type, out var proxyType)) { | ||
| return s_noPeerSentinel; | ||
| } | ||
| return proxyType.GetCustomAttribute<JavaPeerProxy> (inherit: false) ?? s_noPeerSentinel; | ||
| }, this); | ||
| return ReferenceEquals (proxy, s_noPeerSentinel) ? null : proxy; | ||
| } | ||
| JavaPeerProxy? ResolveProxyForManagedType (Type managedType) | ||
| JavaPeerProxy? GetProxyForJavaType (string className) | ||
| { | ||
| if (!_proxyTypeMap.TryGetValue (managedType, out var proxyType)) { | ||
| return null; | ||
| } | ||
| var proxy = _peerProxyCache.GetOrAdd (className, static (name, self) => { | ||
| if (!self._typeMap.TryGetValue (name, out var mappedType)) { | ||
| return s_noPeerSentinel; | ||
| } | ||
| return proxyType.GetCustomAttribute<JavaPeerProxy> (inherit: false); | ||
| var proxy = mappedType.GetCustomAttribute<JavaPeerProxy> (inherit: false); | ||
| if (proxy is null) { | ||
| // Alias typemap entries (for example "jni/name[1]") are not implemented yet. | ||
| // Support for them will be added in a follow-up for https://github.com/dotnet/android/issues/10788. | ||
| throw new NotImplementedException ( | ||
| $"Trimmable typemap alias handling is not implemented yet for '{name}'."); | ||
| } | ||
| return proxy; | ||
| }, this); | ||
| return ReferenceEquals (proxy, s_noPeerSentinel) ? null : proxy; | ||
| } | ||
| internal bool TryGetJniNameForManagedType (Type managedType, [NotNullWhen (true)] out string? jniName) | ||
| { | ||
| var proxy = GetProxyForManagedType (managedType); | ||
| if (proxy is not null) { | ||
| jniName = proxy.JniName; | ||
| return true; | ||
| } | ||
| jniName = null; | ||
| return false; | ||
| jniName = GetProxyForManagedType (managedType)?.JniName; | ||
| return jniName is not null; | ||
| } | ||
simonrozsival marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| /// <summary> | ||
| /// Creates a peer instance using the proxy's CreateInstance method. | ||
| /// Given a managed type, resolves the JNI name, finds the proxy, and calls CreateInstance. | ||
| /// </summary> | ||
| internal bool TryCreatePeer (Type type, IntPtr handle, JniHandleOwnership transfer) | ||
| internal JavaPeerProxy? GetProxyForJavaObject (IntPtr handle, Type? targetType = null) | ||
| { | ||
| var proxy = GetProxyForManagedType (type); | ||
| if (proxy is null) { | ||
| return false; | ||
| if (handle == IntPtr.Zero) { | ||
| return null; | ||
| } | ||
| return proxy.CreateInstance (handle, transfer) != null; | ||
| return TryGetProxyFromHierarchy (this, handle, targetType) ?? | ||
| TryGetProxyFromTargetType (this, handle, targetType); | ||
| static JavaPeerProxy? TryGetProxyFromHierarchy (TrimmableTypeMap self, IntPtr handle, Type? targetType) | ||
| { | ||
| var selfRef = new JniObjectReference (handle); | ||
| var jniClass = JniEnvironment.Types.GetObjectClass (selfRef); | ||
| try { | ||
| while (jniClass.IsValid) { | ||
| var className = JniEnvironment.Types.GetJniTypeNameFromClass (jniClass); | ||
| if (className != null) { | ||
| var proxy = self.GetProxyForJavaType (className); | ||
| if (proxy != null && (targetType is null || targetType.IsAssignableFrom (proxy.TargetType))) { | ||
| return proxy; | ||
| } | ||
| } | ||
| var super = JniEnvironment.Types.GetSuperclass (jniClass); | ||
| JniObjectReference.Dispose (ref jniClass); | ||
| jniClass = super; | ||
| } | ||
| } finally { | ||
| JniObjectReference.Dispose (ref jniClass); | ||
| } | ||
| return null; | ||
| } | ||
| static JavaPeerProxy? TryGetProxyFromTargetType (TrimmableTypeMap self, IntPtr handle, Type? targetType) | ||
| { | ||
| if (targetType is null) { | ||
| return null; | ||
| } | ||
| var proxy = self.GetProxyForManagedType (targetType); | ||
| // Verify the Java object is actually assignable to the target Java type | ||
| // before returning the fallback proxy. Without this, we'd create invalid peers | ||
| // (e.g., IAppendableInvoker wrapping a java.lang.Integer). | ||
| if (proxy is null || !self.TryGetJniNameForManagedType (targetType, out var targetJniName)) { | ||
| return null; | ||
| } | ||
| var selfRef = new JniObjectReference (handle); | ||
| var objClass = default (JniObjectReference); | ||
| var targetClass = default (JniObjectReference); | ||
| try { | ||
| objClass = JniEnvironment.Types.GetObjectClass (selfRef); | ||
| targetClass = JniEnvironment.Types.FindClass (targetJniName); | ||
| return JniEnvironment.Types.IsAssignableFrom (objClass, targetClass) ? proxy : null; | ||
| } finally { | ||
| JniObjectReference.Dispose (ref objClass); | ||
| JniObjectReference.Dispose (ref targetClass); | ||
| } | ||
| } | ||
| } | ||
| const DynamicallyAccessedMemberTypes Constructors = DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors; | ||
| @@ -162,4 +223,13 @@ static void OnRegisterNatives (IntPtr jnienv, IntPtr klass, IntPtr nativeClassHa | ||
| } | ||
| } | ||
| sealed class MissingJavaPeerProxy : JavaPeerProxy | ||
| { | ||
| public MissingJavaPeerProxy () : base ("<missing>", typeof (Java.Lang.Object), null) | ||
| { | ||
| } | ||
| public override IJavaPeerable? CreateInstance (IntPtr handle, JniHandleOwnership transfer) => null; | ||
| } | ||
| } | ||
2 changes: 1 addition & 1 deletion
2 src/Mono.Android/Microsoft.Android.Runtime/TrimmableTypeMapTypeManager.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.