From 9b758a81c3fb3f176ad290483986b75021e38a82 Mon Sep 17 00:00:00 2001 From: tshirtman Date: Mon, 31 Dec 2012 00:15:25 +0100 Subject: [PATCH] some clarifications for java classes extended support --- jnius/jnius_export_class.pxi | 3 +- jnius/jnius_utils.pxi | 58 ++++++++++++------- ...tion.java => nativeinvocationhandler.java} | 11 +--- 3 files changed, 42 insertions(+), 30 deletions(-) rename jnius/{native_invocation.java => nativeinvocationhandler.java} (71%) diff --git a/jnius/jnius_export_class.pxi b/jnius/jnius_export_class.pxi index 2dbdfd1..5d95d5e 100644 --- a/jnius/jnius_export_class.pxi +++ b/jnius/jnius_export_class.pxi @@ -63,7 +63,8 @@ class MetaJavaClass(type): interfaces[n] = jcs.j_env[0].FindClass(jcs.j_env, i) getProxyClass = jcs.j_env[0].GetStaticMethodID( - jcs.j_env, Proxy, "getProxyClass", "(Ljava/lang/ClassLoader,[Ljava/lang/Class;)Ljava/lang/Class;") + jcs.j_env, Proxy, "getProxyClass", + "(Ljava/lang/ClassLoader,[Ljava/lang/Class;)Ljava/lang/Class;") getClassLoader = jcs.j_env[0].GetStaticMethodID( jcs.j_env, baseclass, "getClassLoader", "()Ljava/lang/Class;") diff --git a/jnius/jnius_utils.pxi b/jnius/jnius_utils.pxi index 25de9e0..8a2a1d8 100644 --- a/jnius/jnius_utils.pxi +++ b/jnius/jnius_utils.pxi @@ -248,24 +248,24 @@ cdef class GenericNativeWrapper(object): def __cinit__(self, j_env, name, definition, callback): self.j_env = NULL - self.j_nm = JNINativeMethod + self.j_nm = JNINativeMethod def __init__(self, j_env, name, definition, callback): self.callback = callback self.definitions = parse_definition(definition) - self.nm.name = name - self.nm.signature = definitions - self.fnPtr = { - 'V': self.call_void, - 'L': self.call_obj, - 'D': self.call_double, - 'F': self.call_float, - 'J': self.call_long, - 'I': self.call_int, - 'S': self.call_short, - 'C': self.call_char, - 'B': self.call_byte, - 'Z': self.call_bool}[self.definitions[0]] + self.nm.name = name + self.nm.signature = definitions + self.fnPtr = { + 'V': self.call_void, + 'L': self.call_obj, + 'D': self.call_double, + 'F': self.call_float, + 'J': self.call_long, + 'I': self.call_int, + 'S': self.call_short, + 'C': self.call_char, + 'B': self.call_byte, + 'Z': self.call_bool}[self.definitions[0]] cdef void call_void(self, ...): cdef va_list j_args @@ -616,16 +616,32 @@ cdef class GenericNativeWrapper(object): # return self.callback(*args) +cdef class PythonJavaClass(object): + ''' + base class to create a java class from python + ''' + cdef JNIEnv *j_env + cdef jclass j_cls + cdef LocalRef j_self + + def __cinit__(self, *args): + self.j_env = NULL + self.j_cls = NULL + self.j_self = NULL + pass + + def __init__(self, *args, **kwargs): + self.j_self = create_proxy_instance() + cdef jobject invoke0( - JNIEnv *j_env, - jobject this, - jobject method, - jobjectArray args): + JNIEnv *j_env, + jobject this, + jobject method, + jobjectArray args): cdef jfieldID ptrField = j_env[0].GetFieldID(j_env.GetObjectClass(this), "ptr", "J") cdef jlong jptr = j_env.GetLongField(this, ptrField) - cdef NativeInvocationHandler *h = reinterpret_cast(jptr) - return h.Invoke(env, method, args); + return h.fnPtr(env, method, args); # now we need to create a proxy and pass it an invocation handler @@ -640,7 +656,7 @@ def create_proxy_instance(j_env, py_obj, j_interfaces): for name, definition, method in py_obj.j_methods: nw = GenericNativeWrapper(j_env, name, definition, method) - j_env.RegisterNatives(j_env[0], cls, nw.nm, 1) + j_env.RegisterNatives(j_env[0], cls, nw.nm, 1) # adds it to the invocationhandler diff --git a/jnius/native_invocation.java b/jnius/nativeinvocationhandler.java similarity index 71% rename from jnius/native_invocation.java rename to jnius/nativeinvocationhandler.java index 5484c66..e9cd58a 100644 --- a/jnius/native_invocation.java +++ b/jnius/nativeinvocationhandler.java @@ -1,6 +1,7 @@ -import java.lang.reflect.InvocationHandler; - package jnius; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; + public class NativeInvocationHandler implements InvocationHandler { public NativeInvocationHandler(long ptr) { this.ptr = ptr; @@ -14,9 +15,3 @@ public class NativeInvocationHandler implements InvocationHandler { private long ptr; } - -class NativeInvocationHandler { - public: - virtual ~NativeInvocationHandler(); - virtual jobject Invoke(JNIEnv *, jobject method, jobjectArray args) = 0; -};