Skip to content
Closed
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
2 changes: 2 additions & 0 deletions src/Java.Interop/Java.Interop/JniRuntime.JniValueManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ protected virtual void Dispose (bool disposing)

public abstract List<JniSurfacedPeerInfo> GetSurfacedPeers ();

public abstract void ActivatePeer (JniObjectReference reference, Type peerType, Type []? constructorArguments, object? []? argumentValues);

public void ConstructPeer (IJavaPeerable peer, ref JniObjectReference reference, JniObjectReferenceOptions options)
{
if (peer == null)
Expand Down
43 changes: 2 additions & 41 deletions src/Java.Interop/Java.Interop/ManagedPeer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,31 +93,8 @@ static void Construct (

var ptypes = GetParameterTypes (JniEnvironment.Strings.ToString (n_constructorSignature));
var pvalues = GetValues (runtime, new JniObjectReference (n_constructorArguments), ptypes);
var ctor = type.GetConstructors (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
.FirstOrDefault (c =>
c.GetParameters ().Select (p => p.ParameterType).SequenceEqual (ptypes));
if (ctor == null) {
throw CreateMissingConstructorException (type, ptypes);
}
if (self != null) {
ctor.Invoke (self, pvalues);
return;
}

try {
var f = JniEnvironment.Runtime.MarshalMemberBuilder.CreateConstructActivationPeerFunc (ctor);
f (ctor, new JniObjectReference (n_self), pvalues);
}
catch (Exception e) {
var m = string.Format ("Could not activate {{ PeerReference={0} IdentityHashCode=0x{1} Java.Type={2} }} for managed type '{3}'.",
r_self,
runtime.ValueManager.GetJniIdentityHashCode (r_self).ToString ("x"),
JniEnvironment.Types.GetJniTypeNameFromInstance (r_self),
type.FullName);
Debug.WriteLine (m);

throw new NotSupportedException (m, e);
}
JniEnvironment.Runtime.ValueManager.ActivatePeer (new JniObjectReference (n_self), type, ptypes, pvalues);
}
catch (Exception e) when (JniEnvironment.Runtime.ExceptionShouldTransitionToJni (e)) {
envp.SetPendingException (e);
Expand All @@ -134,22 +111,6 @@ static Exception CreateJniLocationException ()
}
}

static Exception CreateMissingConstructorException (Type type, Type[] ptypes)
{
var message = new StringBuilder ();
message.Append ("Unable to find constructor ");
message.Append (type.FullName);
message.Append ("(");
if (ptypes.Length > 0) {
message.Append (ptypes [0].FullName);
for (int i = 1; i < ptypes.Length; ++i)
message.Append (", ").Append (ptypes [i].FullName);
}
message.Append (")");
message.Append (". Please provide the missing constructor.");
return new NotSupportedException (message.ToString (), CreateJniLocationException ());
}

static Type[] GetParameterTypes (string? signature)
{
if (string.IsNullOrEmpty (signature))
Expand Down Expand Up @@ -210,7 +171,7 @@ static void RegisterNativeMembers (
}
}

sealed class JniLocationException : Exception {
internal sealed class JniLocationException : Exception {

string stackTrace;

Expand Down
7 changes: 7 additions & 0 deletions src/Java.Interop/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,10 @@
"814f144e5d817efc4c6502cc012df310783348304e3ae38573c6d658c234025821fda87a0be8a0" +
"d504df564e2c93b2b878925f42503e9d54dfef9f9586d9e6f38a305769587b1de01f6c0410328b" +
"2c9733db")]
[assembly: InternalsVisibleTo (
"Java.Runtime.Environment, PublicKey=" +
"0024000004800000940000000602000000240000525341310004000011000000438ac2a5acfbf1" +
"6cbd2b2b47a62762f273df9cb2795ceccdf77d10bf508e69e7a362ea7a45455bbf3ac955e1f2e2" +
"814f144e5d817efc4c6502cc012df310783348304e3ae38573c6d658c234025821fda87a0be8a0" +
"d504df564e2c93b2b878925f42503e9d54dfef9f9586d9e6f38a305769587b1de01f6c0410328b" +
"2c9733db")]
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;

namespace Java.Interop {

Expand Down Expand Up @@ -229,6 +233,58 @@ public override IJavaPeerable PeekPeer (JniObjectReference reference)
return null;
}

static Exception CreateMissingConstructorException (Type type, Type [] ptypes)
{
var message = new StringBuilder ();
message.Append ("Unable to find constructor ");
message.Append (type.FullName);
message.Append ("(");
if (ptypes.Length > 0) {
message.Append (ptypes [0].FullName);
for (int i = 1; i < ptypes.Length; ++i)
message.Append (", ").Append (ptypes [i].FullName);
}
message.Append (")");
message.Append (". Please provide the missing constructor.");
return new NotSupportedException (message.ToString (), CreateJniLocationException ());
}

static Exception CreateJniLocationException ()
{
using (var e = new JavaException ()) {
return new JniLocationException (e.ToString ());
}
}

public override void ActivatePeer (JniObjectReference reference, Type peerType, Type [] constructorArguments, object [] argumentValues)
{
var ctor = peerType.GetConstructors (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
.FirstOrDefault (c => c.GetParameters ().Select (p => p.ParameterType).SequenceEqual (constructorArguments));
if (ctor == null) {
throw CreateMissingConstructorException (peerType, constructorArguments);
}
var runtime = JniEnvironment.Runtime;
var self = runtime.ValueManager.PeekPeer (reference);
if (self != null) {
ctor.Invoke (self, argumentValues);
return;
}

try {
var f = JniEnvironment.Runtime.MarshalMemberBuilder.CreateConstructActivationPeerFunc (ctor);
f (ctor, reference, 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),
peerType.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 @@ -59,6 +59,11 @@ public override IJavaPeerable PeekPeer (JniObjectReference reference)
{
return null;
}

public override void ActivatePeer (JniObjectReference reference, Type peerType, Type [] constructorArguments, object [] argumentValues)
{
throw new NotImplementedException ();
}
}

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

public override void ActivatePeer (JniObjectReference reference, Type peerType, Type [] constructorArguments, object [] argumentValues)
{
throw new NotImplementedException ();
}

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