Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions src/Java.Interop/Java.Interop/JniRuntime.JniValueManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
Expand Down Expand Up @@ -88,7 +90,29 @@ protected virtual void Dispose (bool disposing)

public abstract List<JniSurfacedPeerInfo> GetSurfacedPeers ();

public abstract void ActivatePeer (IJavaPeerable? self, JniObjectReference reference, ConstructorInfo cinfo, object? []? argumentValues);
public virtual void ActivatePeer (
JniObjectReference reference,
[DynamicallyAccessedMembers (Constructors)] Type type,
ConstructorInfo cinfo,
object?[]? argumentValues)
{
try {
var self = (IJavaPeerable) RuntimeHelpers.GetUninitializedObject (type);
self.SetPeerReference (reference);
cinfo.Invoke (self, argumentValues);
} catch (Exception e) {
var m = string.Format (
CultureInfo.InvariantCulture,
"Could not activate {{ PeerReference={0} IdentityHashCode=0x{1} Java.Type={2} }} for managed type '{3}'.",
reference,
GetJniIdentityHashCode (reference).ToString ("x", CultureInfo.InvariantCulture),
JniEnvironment.Types.GetJniTypeNameFromInstance (reference),
type.FullName);
Debug.WriteLine (m);

throw new NotSupportedException (m, e);
}
Comment thread
simonrozsival marked this conversation as resolved.
}

public void ConstructPeer (IJavaPeerable peer, ref JniObjectReference reference, JniObjectReferenceOptions options)
{
Expand Down Expand Up @@ -294,6 +318,7 @@ static Type GetPeerType ([DynamicallyAccessedMembers (Constructors)] Type type)
return CreatePeer (ref reference, JniObjectReferenceOptions.Copy, targetType);
}

// This base method implementation is NOT reachable in trimmable typemap - it is featureswitch guarded
public virtual IJavaPeerable? CreatePeer (
ref JniObjectReference reference,
JniObjectReferenceOptions transfer,
Expand Down Expand Up @@ -709,7 +734,7 @@ static JniValueMarshaler GetObjectArrayMarshaler (Type elementType)

// FIXME: https://github.com/dotnet/java-interop/issues/1192
[UnconditionalSuppressMessage ("Trimming", "IL2060", Justification = makeGenericMethodMessage)]
[UnconditionalSuppressMessage ("Trimming", "IL3050", Justification = makeGenericMethodMessage)]
[UnconditionalSuppressMessage ("AOT", "IL3050", Justification = makeGenericMethodMessage)]
static MethodInfo MakeGenericMethod (MethodInfo method, Type type) =>
method.MakeGenericMethod (type);

Expand Down Expand Up @@ -964,4 +989,3 @@ public override void DestroyGenericArgumentState (object? value, ref JniValueMar
}
}
}

2 changes: 1 addition & 1 deletion src/Java.Interop/Java.Interop/ManagedPeer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ static void Construct (
return;
}

JniEnvironment.Runtime.ValueManager.ActivatePeer (self, new JniObjectReference (n_self), cinfo, pvalues);
JniEnvironment.Runtime.ValueManager.ActivatePeer (new JniObjectReference (n_self), type, cinfo, pvalues);
}
catch (Exception e) {
__r?.OnUserUnhandledException (ref envp, e);
Expand Down
2 changes: 2 additions & 0 deletions src/Java.Interop/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ static Java.Interop.JniEnvironment.InstanceFields.GetFieldID(Java.Interop.JniObj
static Java.Interop.JniEnvironment.InstanceMethods.GetMethodID(Java.Interop.JniObjectReference type, System.ReadOnlySpan<byte> name, System.ReadOnlySpan<byte> signature) -> Java.Interop.JniMethodInfo!
static Java.Interop.JniEnvironment.StaticFields.GetStaticFieldID(Java.Interop.JniObjectReference type, System.ReadOnlySpan<byte> name, System.ReadOnlySpan<byte> signature) -> Java.Interop.JniFieldInfo!
static Java.Interop.JniEnvironment.StaticMethods.GetStaticMethodID(Java.Interop.JniObjectReference type, System.ReadOnlySpan<byte> name, System.ReadOnlySpan<byte> signature) -> Java.Interop.JniMethodInfo!
*REMOVED*abstract Java.Interop.JniRuntime.JniValueManager.ActivatePeer(Java.Interop.IJavaPeerable? self, Java.Interop.JniObjectReference reference, System.Reflection.ConstructorInfo! cinfo, object?[]? argumentValues) -> void
Comment thread
simonrozsival marked this conversation as resolved.
virtual Java.Interop.JniRuntime.JniValueManager.ActivatePeer(Java.Interop.JniObjectReference reference, System.Type! type, System.Reflection.ConstructorInfo! cinfo, object?[]? argumentValues) -> void
35 changes: 0 additions & 35 deletions src/Java.Runtime.Environment/Java.Interop/ManagedValueManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,41 +198,6 @@ public override void FinalizePeer (IJavaPeerable value)
value.Finalized ();
}

public override void ActivatePeer (IJavaPeerable? self, JniObjectReference reference, ConstructorInfo cinfo, object?[]? argumentValues)
{
var runtime = JniEnvironment.Runtime;

try {
ActivateViaReflection (reference, cinfo, argumentValues);
} catch (Exception e) {
var m = string.Format ("Could not activate {{ PeerReference={0} IdentityHashCode=0x{1} Java.Type={2} }} for managed type '{3}'.",
reference,
runtime.ValueManager.GetJniIdentityHashCode (reference).ToString ("x"),
JniEnvironment.Types.GetJniTypeNameFromInstance (reference),
cinfo.DeclaringType?.FullName);
Debug.WriteLine (m);

throw new NotSupportedException (m, e);
}
}

void ActivateViaReflection (JniObjectReference reference, ConstructorInfo cinfo, object?[]? argumentValues)
{
var declType = cinfo.DeclaringType ?? throw new NotSupportedException ("Do not know the type to create!");

var self = GetUninitializedObject (declType);
self.SetPeerReference (reference);

cinfo.Invoke (self, argumentValues);

// FIXME: https://github.com/dotnet/java-interop/issues/1192
const string getUninitializedObject = "This code path is not used in Android projects.";
[UnconditionalSuppressMessage ("Trimming", "IL2067", Justification = getUninitializedObject)]
[UnconditionalSuppressMessage ("Trimming", "IL2072", Justification = getUninitializedObject)]
static IJavaPeerable GetUninitializedObject (Type type) =>
(IJavaPeerable) System.Runtime.CompilerServices.RuntimeHelpers.GetUninitializedObject (type);
}

public override List<JniSurfacedPeerInfo> GetSurfacedPeers ()
{
if (RegisteredInstances == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,35 +258,6 @@ static Exception CreateJniLocationException ()
}
}

const string NotUsedInAndroid = "This code path is not used in Android projects.";

public override void ActivatePeer (IJavaPeerable? self, JniObjectReference reference, ConstructorInfo cinfo, object?[]? argumentValues)
{
var runtime = JniEnvironment.Runtime;

try {
var declType = cinfo.DeclaringType ?? throw new NotSupportedException ("Do not know the type to create!");
var instance = GetUninitializedObject (declType);
instance.SetPeerReference (reference);
cinfo.Invoke (instance, argumentValues);

// FIXME: https://github.com/dotnet/java-interop/issues/1192
[UnconditionalSuppressMessage ("Trimming", "IL2067", Justification = NotUsedInAndroid)]
[UnconditionalSuppressMessage ("Trimming", "IL2072", Justification = NotUsedInAndroid)]
static IJavaPeerable GetUninitializedObject (Type type) =>
(IJavaPeerable) System.Runtime.CompilerServices.RuntimeHelpers.GetUninitializedObject (type);
} catch (Exception e) {
var m = string.Format ("Could not activate {{ PeerReference={0} IdentityHashCode=0x{1} Java.Type={2} }} for managed type '{3}'.",
reference,
runtime.ValueManager.GetJniIdentityHashCode (reference).ToString ("x"),
JniEnvironment.Types.GetJniTypeNameFromInstance (reference),
cinfo.DeclaringType?.FullName);
Debug.WriteLine (m);

throw new NotSupportedException (m, e);
}
}

public override void FinalizePeer (IJavaPeerable value)
{
var h = value.PeerReference;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ public override IJavaPeerable PeekPeer (JniObjectReference reference)
{
return null;
}

public override void ActivatePeer (IJavaPeerable self, JniObjectReference reference, ConstructorInfo cinfo, object [] argumentValues)
{
throw new NotImplementedException ();
}
}

[Test]
Expand Down
5 changes: 0 additions & 5 deletions tests/Java.Interop-Tests/Java.Interop/JniRuntimeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,6 @@ public override IJavaPeerable PeekPeer (JniObjectReference reference)
return null;
}

public override void ActivatePeer (IJavaPeerable self, JniObjectReference reference, ConstructorInfo cinfo, object [] argumentValues)
{
throw new NotImplementedException ();
}

public override void RemovePeer (IJavaPeerable peer)
{
}
Expand Down