Repository navigation
[TrimmableTypeMap] JavaCast/JavaAs + container support #11225
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
Changes from 3 commits
bafa7e5
a804994
c5062a6
d114f84
77a4662
5ab4247
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -514,9 +514,28 @@ void ProcessContext (HandleContext* context) | |
|
|
||
| if (RuntimeFeature.TrimmableTypeMap) { | ||
| try { | ||
| // Mirror legacy GetPeerType: callers commonly request universal | ||
| // interfaces / boxes (IJavaPeerable, object, Exception) — map these | ||
| // to a concrete peer type so the proxy lookup can succeed. | ||
| var resolvedTargetType = ResolvePeerType (targetType); | ||
|
|
||
| var typeMap = TrimmableTypeMap.Instance; | ||
| var proxy = typeMap.GetProxyForJavaObject (reference.Handle, targetType); | ||
| var peer = proxy?.CreateInstance (reference.Handle, JniHandleOwnership.DoNotTransfer); | ||
| var proxy = typeMap.GetProxyForJavaObject (reference.Handle, resolvedTargetType); | ||
|
|
||
| // Open-generic proxies (e.g. JavaList<>) cannot create closed | ||
| // instantiations (e.g. JavaList<int>) via CreateInstance because | ||
| // the generated IL can't newobj an open generic type. Activate the | ||
| // closed targetType directly via its (IntPtr, JniHandleOwnership) | ||
| // ctor — [DAM(Constructors)] on targetType guarantees the trimmer | ||
| // preserves the ctor metadata. | ||
| IJavaPeerable? peer; | ||
| if (proxy is not null && proxy.TargetType.IsGenericTypeDefinition && | ||
| resolvedTargetType is not null && | ||
| resolvedTargetType.IsGenericType && !resolvedTargetType.IsGenericTypeDefinition) { | ||
| peer = ActivateUsingReflection (resolvedTargetType, reference.Handle, JniHandleOwnership.DoNotTransfer); | ||
| } else { | ||
| peer = proxy?.CreateInstance (reference.Handle, JniHandleOwnership.DoNotTransfer); | ||
| } | ||
| if (peer is not null) { | ||
| var peerState = peer.JniManagedPeerState | JniManagedPeerStates.Replaceable; | ||
| if (global::Java.Interop.Runtime.IsGCUserPeer (peer.PeerReference.Handle)) { | ||
|
|
@@ -526,7 +545,22 @@ void ProcessContext (HandleContext* context) | |
| return peer; | ||
| } | ||
|
|
||
| var targetName = targetType?.AssemblyQualifiedName ?? "<null>"; | ||
| // Disambiguate the failure — match the contract of the base | ||
| // JniRuntime.JniValueManager.CreatePeer so JavaCast / JavaAs | ||
| // surface the right exception (or null) to callers: | ||
| // | ||
| // (a) target type has no Java mapping at all → ArgumentException | ||
| // (b) Java instance is not assignable to the target's Java class | ||
| // → return null (JavaAs returns null; JavaCast wraps to | ||
| // InvalidCastException via its `??` clause) | ||
| // (c) classes are compatible but no proxy / activation failed | ||
| // → NotSupportedException (genuine generator gap) | ||
| if (resolvedTargetType is not null && | ||
| IsIncompatibleCast (typeMap, ref reference, resolvedTargetType)) { | ||
| return null; | ||
| } | ||
|
|
||
| var targetName = resolvedTargetType?.AssemblyQualifiedName ?? "<null>"; | ||
| var javaType = JniEnvironment.Types.GetJniTypeNameFromInstance (reference); | ||
|
|
||
| throw new NotSupportedException ( | ||
|
|
@@ -541,6 +575,80 @@ void ProcessContext (HandleContext* context) | |
| return base.CreatePeer (ref reference, transfer, targetType); | ||
| } | ||
|
|
||
| [return: DynamicallyAccessedMembers (Constructors)] | ||
| static Type? ResolvePeerType ([DynamicallyAccessedMembers (Constructors)] Type? type) | ||
| { | ||
| if (type is null) { | ||
| return null; | ||
| } | ||
| if (type == typeof (object) || type == typeof (IJavaPeerable)) { | ||
| return typeof (global::Java.Interop.JavaObject); | ||
| } | ||
| if (type == typeof (Exception)) { | ||
| return typeof (JavaException); | ||
| } | ||
| return type; | ||
| } | ||
|
|
||
| static IJavaPeerable? ActivateUsingReflection ( | ||
| [DynamicallyAccessedMembers (Constructors)] | ||
| Type closedType, | ||
| IntPtr handle, | ||
| JniHandleOwnership transfer) | ||
| { | ||
| var ctor = closedType.GetConstructor (ActivationConstructorBindingFlags, null, XAConstructorSignature, null); | ||
|
jonathanpeppers marked this conversation as resolved.
Resolved
|
||
| if (ctor is null) { | ||
| return null; | ||
| } | ||
|
|
||
| return (IJavaPeerable) ctor.Invoke ([handle, transfer]); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// When the trimmable typemap proxy lookup yields no peer for a non-null | ||
| /// <paramref name="targetType"/>, decide whether the caller's request is a | ||
| /// genuine bad cast (return true → caller returns null) or a missing typemap | ||
| /// entry (throw <see cref="ArgumentException"/>). | ||
| /// Returns false when the target's Java class IS compatible with the | ||
| /// instance — letting the caller fall through to its NotSupportedException | ||
| /// branch. | ||
| /// </summary> | ||
| static bool IsIncompatibleCast ( | ||
| TrimmableTypeMap typeMap, | ||
| ref JniObjectReference reference, | ||
| Type targetType) | ||
| { | ||
| if (!typeMap.TryGetJniNameForManagedType (targetType, out var targetJniName)) { | ||
| throw new ArgumentException ( | ||
| $"Could not determine Java type corresponding to '{targetType.AssemblyQualifiedName}'.", | ||
| nameof (targetType)); | ||
| } | ||
|
|
||
| var instanceClass = JniEnvironment.Types.GetObjectClass (reference); | ||
| JniObjectReference targetClass = default; | ||
| try { | ||
| try { | ||
| targetClass = JniEnvironment.Types.FindClass (targetJniName); | ||
| } catch (Exception e) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤖 Consider narrowing to Rule: Challenge exception swallowing / Differentiate similar error messages (Postmortem
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot address this comment - change the code so that we catch |
||
| throw new ArgumentException ( | ||
| $"Could not find Java class '{targetJniName}'.", | ||
| nameof (targetType), e); | ||
| } | ||
|
|
||
| if (!JniEnvironment.Types.IsAssignableFrom (instanceClass, targetClass)) { | ||
| // Genuine bad cast — return null, callers translate this. | ||
| return true; | ||
| } | ||
| } finally { | ||
| JniObjectReference.Dispose (ref instanceClass); | ||
| JniObjectReference.Dispose (ref targetClass); | ||
| } | ||
|
|
||
| // Classes are compatible — caller should throw NotSupportedException | ||
| // (real proxy/activation gap). | ||
| return false; | ||
| } | ||
|
|
||
| protected override bool TryConstructPeer ( | ||
| IJavaPeerable self, | ||
| ref JniObjectReference reference, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.