Skip to content

Commit 84d0d1d

Browse files
committed
[Java.Interop] Add JniType.AllocObject(), GetCachedInstanceMethod().
1 parent 2d0c19f commit 84d0d1d

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

src/Java.Interop/Java.Interop/JniType.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Reflection;
5-
using System.Text;
2+
using System.Threading;
3+
64
using Java.Interop;
75

86
namespace Java.Interop {
@@ -30,6 +28,11 @@ public JniInstanceMethodID GetConstructor (string signature)
3028
return JniMembers.GetMethodID (SafeHandle, "<init>", signature);
3129
}
3230

31+
public JniLocalReference AllocObject ()
32+
{
33+
return JniActivator.AllocObject (SafeHandle);
34+
}
35+
3336
public JniLocalReference NewObject (JniInstanceMethodID constructor, params JValue[] @params)
3437
{
3538
return JniActivator.NewObject (SafeHandle, constructor, @params);
@@ -39,5 +42,15 @@ public JniInstanceMethodID GetInstanceMethod (string name, string signature)
3942
{
4043
return JniMembers.GetMethodID (SafeHandle, name, signature);
4144
}
45+
46+
public JniInstanceMethodID GetCachedInstanceMethod (ref JniInstanceMethodID cachedMethod, string name, string signature)
47+
{
48+
if (cachedMethod != null && !cachedMethod.IsInvalid)
49+
return cachedMethod;
50+
var m = GetInstanceMethod (name, signature);
51+
if (Interlocked.CompareExchange (ref cachedMethod, m, null) != null)
52+
m.Dispose ();
53+
return cachedMethod;
54+
}
4255
}
4356
}

0 commit comments

Comments
 (0)