Repository navigation
Expand file tree
/
Copy pathJavaPeerProxy.cs
More file actions
135 lines (119 loc) · 5.25 KB
/
Copy pathJavaPeerProxy.cs
File metadata and controls
135 lines (119 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#nullable enable
using System;
using Android.Runtime;
namespace Java.Interop
{
/// <summary>
/// Attribute applied to generated alias holder types. When multiple .NET types
/// map to the same JNI name (e.g., <c>JavaCollection</c> and <c>JavaCollection<T></c>
/// both map to <c>"java/util/Collection"</c>), the base JNI name entry points to
/// a plain holder class annotated with this attribute, which lists the indexed
/// TypeMap keys for each alias type.
/// </summary>
/// <remarks>
/// The alias holder is NOT a <see cref="JavaPeerProxy"/> subclass — this ensures
/// <c>GetCustomAttribute<JavaPeerProxy>()</c> returns null for alias entries,
/// keeping the fast path (non-alias types) free of alias checks.
/// </remarks>
[AttributeUsage (AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public sealed class JavaPeerAliasesAttribute : Attribute
{
/// <summary>
/// Gets the indexed TypeMap keys for this alias group (e.g., <c>"java/util/Collection[0]"</c>,
/// <c>"java/util/Collection[1]"</c>).
/// </summary>
public string[] Aliases { get; }
public JavaPeerAliasesAttribute (params string[] aliases) => Aliases = aliases;
}
/// <summary>
/// Base attribute class for generated proxy types that enable AOT-safe type mapping
/// between Java and .NET types.
/// </summary>
/// <remarks>
/// Proxy attributes are generated at build time and applied to the proxy type itself
/// (self-application pattern). The .NET runtime's <c>GetCustomAttribute<JavaPeerProxy>()</c>
/// instantiates the proxy in an AOT-safe manner — no <c>Activator.CreateInstance()</c> needed.
/// </remarks>
[AttributeUsage (AttributeTargets.Class | AttributeTargets.Interface, Inherited = false, AllowMultiple = false)]
public abstract class JavaPeerProxy : Attribute
{
protected JavaPeerProxy (string jniName, Type targetType)
: this (jniName, targetType, false)
{
}
protected JavaPeerProxy (string jniName, Type targetType, bool isSealed)
{
ArgumentNullException.ThrowIfNull (jniName);
ArgumentNullException.ThrowIfNull (targetType);
JniName = jniName;
TargetType = targetType;
IsSealed = isSealed;
}
/// <summary>
/// Gets the final JNI type name of the Java class this proxy represents.
/// </summary>
public string JniName { get; }
/// <summary>
/// Creates an instance of the target type using the JNI handle and ownership semantics.
/// This replaces the reflection-based constructor invocation used in the legacy path.
/// </summary>
/// <param name="handle">The JNI object reference handle.</param>
/// <param name="transfer">How to handle JNI reference ownership.</param>
/// <returns>A new instance of the target type wrapping the JNI handle, or null if activation is not supported.</returns>
public abstract IJavaPeerable? CreateInstance (IntPtr handle, JniHandleOwnership transfer);
/// <summary>
/// Gets the target .NET type that this proxy represents.
/// </summary>
public Type TargetType { get; }
/// <summary>
/// Gets whether the sealed target can be resolved directly using the runtime's class loader.
/// </summary>
/// <remarks>
/// Generated proxies pass <see langword="true"/> only for sealed Mono.Android targets. External
/// targets pass <see langword="false"/> even when sealed, because they can use a different class loader.
/// </remarks>
public bool IsSealed { get; }
/// <summary>
/// Returns <see langword="true"/> when the UCO constructor callback should skip
/// activation because a managed peer already exists for the given JNI handle
/// (e.g., when called from <c>FinishCreateInstance</c> after <c>StartCreateInstance</c>
/// already registered the peer).
/// </summary>
public static bool ShouldSkipActivation (IntPtr jniSelf)
{
var reference = new JniObjectReference (jniSelf, JniObjectReferenceType.Invalid);
var peer = JniEnvironment.Runtime.ValueManager.PeekPeer (reference);
if (peer != null && !IsActivationPeer (peer)) {
return true;
}
return JniEnvironment.WithinNewObjectScope;
}
public static IJavaPeerable? GetActivationPeer (IntPtr jniSelf)
{
var reference = new JniObjectReference (jniSelf, JniObjectReferenceType.Invalid);
var peer = JniEnvironment.Runtime.ValueManager.PeekPeer (reference);
return peer != null && IsActivationPeer (peer) ? peer : null;
}
public static void SetActivationPeerReference (IJavaPeerable peer, IntPtr jniSelf)
{
var reference = new JniObjectReference (jniSelf, JniObjectReferenceType.Invalid);
peer.SetPeerReference (reference);
peer.SetJniIdentityHashCode (JniEnvironment.References.GetIdentityHashCode (reference));
}
public static void MarkActivationPeerReplaceable (IntPtr jniSelf)
{
var reference = new JniObjectReference (jniSelf, JniObjectReferenceType.Invalid);
var peer = JniEnvironment.Runtime.ValueManager.PeekPeer (reference);
if (peer == null) {
return;
}
peer.SetJniManagedPeerState (peer.JniManagedPeerState | JniManagedPeerStates.Replaceable);
}
static bool IsActivationPeer (IJavaPeerable peer)
{
var state = peer.JniManagedPeerState;
return (state & JniManagedPeerStates.Activatable) == JniManagedPeerStates.Activatable
|| (state & JniManagedPeerStates.Replaceable) == JniManagedPeerStates.Replaceable;
}
}
}