forked from bitter/android-jni-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JNIBridge.java
55 lines (46 loc) · 1.17 KB
/
JNIBridge.java
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
package bitter.jnibridge;
import java.lang.reflect.*;
public class JNIBridge
{
static native Object invoke(long ptr, Class clazz, Method method, Object[] args);
static native void delete(long ptr);
static Object newInterfaceProxy(final long ptr, final Class[] interfaces)
{
return Proxy.newProxyInstance(JNIBridge.class.getClassLoader(), interfaces, new InterfaceProxy(ptr));
}
static void disableInterfaceProxy(final Object proxy)
{
((InterfaceProxy) Proxy.getInvocationHandler(proxy)).disable();
}
private static class InterfaceProxy implements InvocationHandler
{
private Object m_InvocationLock = new Object[0];
private long m_Ptr;
public InterfaceProxy(final long ptr) { m_Ptr = ptr; }
public Object invoke(Object proxy, Method method, Object[] args)
{
synchronized (m_InvocationLock)
{
if (m_Ptr == 0)
return null;
return JNIBridge.invoke(m_Ptr, method.getDeclaringClass(), method, args);
}
}
public void finalize()
{
synchronized (m_InvocationLock)
{
if (m_Ptr == 0)
return;
JNIBridge.delete(m_Ptr);
}
}
public void disable()
{
synchronized (m_InvocationLock)
{
m_Ptr = 0;
}
}
}
}