Skip to content

Commit 3fcc714

Browse files
committed
[Java.Interop] Verify JniPeerMembers managedPeerType parameter.
Commit b754516 fixed an issue wherein the JniPeerMembers managedPeerType parameter didn't match the type declaring the JniPeerMembers parameter (which would result in "broken" JniPeerMembers.InstanceMethods.Call*Method() behavior as the virtual-vs.-nonvirtual type check would fail), and asks: > (I wonder if there's a way to check/assert for this...) Yes, there is a way: use JavaVM.GetJniTypeInfoForType() and see if JniTypeInfo.JniTypeName matches the jniPeerType value. Add a Debug.Assert() to ensure that this is true. Note: Why Debug.Assert() and not ArgumentException? Because I only want to have this extra checking occur in Debug builds, not Release builds. This may be a mistake...especially since Debug.Assert() only logs to TestOutput-<AssembyName>.txt, and doesn't throw an exception or abort the process, and now that I look for the assert messages I see a great many of them in the log file...
1 parent b754516 commit 3fcc714

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

src/Java.Interop/Java.Interop/JniPeerMembers.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics;
23
using System.Collections.Generic;
34

45
namespace Java.Interop {
@@ -14,6 +15,13 @@ public JniPeerMembers (string jniPeerType, Type managedPeerType)
1415
if (!typeof (IJavaObject).IsAssignableFrom (managedPeerType))
1516
throw new ArgumentException ("'managedPeerType' must implement the IJavaObject interface.", "managedPeerType");
1617

18+
Debug.Assert (
19+
JniEnvironment.Current.JavaVM.GetJniTypeInfoForType (managedPeerType).JniTypeName == jniPeerType,
20+
string.Format ("ManagedPeerType <=> JniTypeName Mismatch! javaVM.GetJniTypeInfoForType(typeof({0})).JniTypeName=\"{1}\" != \"{2}\"",
21+
managedPeerType.FullName,
22+
JniEnvironment.Current.JavaVM.GetJniTypeInfoForType (managedPeerType).JniTypeName,
23+
jniPeerType));
24+
1725
JniPeerTypeName = jniPeerType;
1826
ManagedPeerType = managedPeerType;
1927
instanceMethods = new JniPeerInstanceMethods (this);

0 commit comments

Comments
 (0)