From e10ed056bb784693fa4344fd31e096bff540b4d3 Mon Sep 17 00:00:00 2001 From: tshirtman Date: Thu, 3 Jan 2013 12:57:46 +0100 Subject: [PATCH] move testImpl to tests/test_proxy.py and move NativeInvocationHandler adapt jnius_jvm_desktop to have access to java jnius package --- Makefile | 5 +- jnius/jnius.pyx | 6 +- jnius/jnius_jvm_desktop.pxi | 13 +- jnius/jnius_utils.pxi | 139 ------------------ .../org/jnius}/NativeInvocationHandler.java | 0 tests/test_proxy.py | 138 +++++++++++++++++ 6 files changed, 149 insertions(+), 152 deletions(-) rename jnius/{ => src/org/jnius}/NativeInvocationHandler.java (100%) create mode 100644 tests/test_proxy.py diff --git a/Makefile b/Makefile index 8ab5fc5..af960f2 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ .PHONY: build_ext tests build_ext: + javac jnius/src/org/jnius/NativeInvocationHandler.java python setup.py build_ext --inplace -f html: @@ -14,7 +15,3 @@ tests: build_ext cd tests && javac org/jnius/InterfaceWithPublicEnum.java cd tests && javac org/jnius/ClassArgument.java cd tests && env PYTHONPATH=..:$(PYTHONPATH) nosetests-2.7 -v - -testimpl: build_ext - javac jnius/NativeInvocationHandler.java - python -c 'import jnius.jnius; jnius.jnius.test()' diff --git a/jnius/jnius.pyx b/jnius/jnius.pyx index 2ba4a08..fd2a555 100644 --- a/jnius/jnius.pyx +++ b/jnius/jnius.pyx @@ -85,9 +85,9 @@ Python:: ''' -__all__ = ( - 'JavaObject', 'JavaClass', 'JavaMethod', 'JavaField', 'MetaJavaClass', - 'JavaException', 'cast', 'find_javaclass') +__all__ = ('JavaObject', 'JavaClass', 'JavaMethod', 'JavaField', + 'MetaJavaClass', 'JavaException', 'cast', 'find_javaclass', + 'PythonJavaClass', 'java_implementation') from libc.stdlib cimport malloc, free diff --git a/jnius/jnius_jvm_desktop.pxi b/jnius/jnius_jvm_desktop.pxi index ac634b2..0e26971 100644 --- a/jnius/jnius_jvm_desktop.pxi +++ b/jnius/jnius_jvm_desktop.pxi @@ -20,18 +20,20 @@ def classpath(): import platform from glob import glob from os import environ - from os.path import realpath + from os.path import realpath, dirname - if 'CLASSPATH' not in environ: - return realpath('.') - cp = environ.get('CLASSPATH') if platform.system() == 'Windows': split_char = ';' else: split_char = ':' + + paths = [realpath('.'), dirname(__file__) + '/src/org/', ] + if 'CLASSPATH' not in environ: + return split_char.join(paths) + + cp = environ.get('CLASSPATH') pre_paths = cp.split(split_char) # deal with wildcards - paths = [] for path in pre_paths: if not path.endswith('*'): paths.append(path) @@ -41,7 +43,6 @@ def classpath(): result = split_char.join(paths) return result - cdef void create_jnienv(): cdef JavaVM* jvm cdef JavaVMInitArgs args diff --git a/jnius/jnius_utils.pxi b/jnius/jnius_utils.pxi index cefde1d..2fd6d76 100644 --- a/jnius/jnius_utils.pxi +++ b/jnius/jnius_utils.pxi @@ -366,9 +366,7 @@ cdef jobject invoke0(JNIEnv *j_env, jobject j_this, jobject j_proxy, jobject traceback.print_exc(e) - # now we need to create a proxy and pass it an invocation handler - cdef create_proxy_instance(JNIEnv *j_env, py_obj, j_interfaces): from .reflect import autoclass Proxy = autoclass('java.lang.reflect.Proxy') @@ -396,140 +394,3 @@ cdef create_proxy_instance(JNIEnv *j_env, py_obj, j_interfaces): # create the proxy and pass it the invocation handler return j_obj - -def test(): - from .reflect import autoclass - - print '1: declare a TestImplem that implement Collection' - - class TestImplemIterator(PythonJavaClass): - __javainterfaces__ = [ - #'java/util/Iterator', - 'java/util/ListIterator',] - - def __init__(self, collection, index=0): - super(TestImplemIterator, self).__init__() - self.collection = collection - self.index = index - - @java_implementation('()Z') - def hasNext(self): - return self.index < len(self.collection.data) - - @java_implementation('()Ljava/lang/Object;') - def next(self): - obj = self.collection.data[self.index] - self.index += 1 - return obj - - @java_implementation('()Z') - def hasPrevious(self): - return self.index >= 0 - - @java_implementation('()Ljava/lang/Object;') - def previous(self): - self.index -= 1 - obj = self.collection.data[self.index] - print "previous called", obj - return obj - - @java_implementation('()I') - def previousIndex(self): - return self.index - 1 - - @java_implementation('()Ljava/lang/String;') - def toString(self): - return repr(self) - - class TestImplem(PythonJavaClass): - __javainterfaces__ = ['java/util/List'] - - def __init__(self, *args): - super(TestImplem, self).__init__() - self.data = list(args) - - @java_implementation('()Ljava/util/Iterator;') - def iterator(self): - it = TestImplemIterator(self) - return it - - @java_implementation('()Ljava/lang/String;') - def toString(self): - return repr(self) - - @java_implementation('()I') - def size(self): - return len(self.data) - - @java_implementation('(I)Ljava/lang/Object;') - def get(self, index): - return self.data[index] - - @java_implementation('(ILjava/lang/Object;)Ljava/lang/Object;') - def set(self, index, obj): - old_object = self.data[index] - self.data[index] = obj - return old_object - - @java_implementation('()[Ljava/lang/Object;') - def toArray(self): - return self.data - - @java_implementation('()Ljava/util/ListIterator;') - def listIterator(self): - it = TestImplemIterator(self) - return it - - @java_implementation('(I)Ljava/util/ListIterator;', - name='ListIterator') - def listIteratorI(self, index): - it = TestImplemIterator(self, index) - return it - - - print '2: instanciate the class, with some data' - a = TestImplem(*range(10)) - print a - print dir(a) - - print '3: Do cast to a collection' - a2 = cast('java/util/Collection', a.j_self) - - print '4: Try few method on the collection' - Collections = autoclass('java.util.Collections') - #print Collections.enumeration(a) - #print Collections.enumeration(a) - ret = Collections.max(a) - print 'MAX returned', ret - - # the first one of the following methods will work, witchever it is - # the next ones will fail - print "reverse" - print Collections.reverse(a) - print a.data - - print "reverse" - print Collections.reverse(a) - print a.data - - print "before swap" - print Collections.swap(a, 2, 3) - print "after swap" - print a.data - - print "rotate" - print Collections.rotate(a, 5) - print a.data - - print 'Order of data before shuffle()', a.data - print Collections.shuffle(a) - print 'Order of data after shuffle()', a.data - - - # XXX We have issues for methosd with multiple signature - #print '-> Collections.max(a)' - #print Collections.max(a2) - #print '-> Collections.max(a)' - #print Collections.max(a2) - #print '-> Collections.shuffle(a)' - #print Collections.shuffle(a2) diff --git a/jnius/NativeInvocationHandler.java b/jnius/src/org/jnius/NativeInvocationHandler.java similarity index 100% rename from jnius/NativeInvocationHandler.java rename to jnius/src/org/jnius/NativeInvocationHandler.java diff --git a/tests/test_proxy.py b/tests/test_proxy.py new file mode 100644 index 0000000..99a3a36 --- /dev/null +++ b/tests/test_proxy.py @@ -0,0 +1,138 @@ +from jnius import autoclass, java_implementation, PythonJavaClass, cast + +print '1: declare a TestImplem that implement Collection' + + +class TestImplemIterator(PythonJavaClass): + __javainterfaces__ = [ + #'java/util/Iterator', + 'java/util/ListIterator', ] + + def __init__(self, collection, index=0): + super(TestImplemIterator, self).__init__() + self.collection = collection + self.index = index + + @java_implementation('()Z') + def hasNext(self): + return self.index < len(self.collection.data) + + @java_implementation('()Ljava/lang/Object;') + def next(self): + obj = self.collection.data[self.index] + self.index += 1 + return obj + + @java_implementation('()Z') + def hasPrevious(self): + return self.index >= 0 + + @java_implementation('()Ljava/lang/Object;') + def previous(self): + self.index -= 1 + obj = self.collection.data[self.index] + print "previous called", obj + return obj + + @java_implementation('()I') + def previousIndex(self): + return self.index - 1 + + @java_implementation('()Ljava/lang/String;') + def toString(self): + return repr(self) + + +class TestImplem(PythonJavaClass): + __javainterfaces__ = ['java/util/List'] + + def __init__(self, *args): + super(TestImplem, self).__init__() + self.data = list(args) + + @java_implementation('()Ljava/util/Iterator;') + def iterator(self): + it = TestImplemIterator(self) + return it + + @java_implementation('()Ljava/lang/String;') + def toString(self): + return repr(self) + + @java_implementation('()I') + def size(self): + return len(self.data) + + @java_implementation('(I)Ljava/lang/Object;') + def get(self, index): + return self.data[index] + + @java_implementation('(ILjava/lang/Object;)Ljava/lang/Object;') + def set(self, index, obj): + old_object = self.data[index] + self.data[index] = obj + return old_object + + @java_implementation('()[Ljava/lang/Object;') + def toArray(self): + return self.data + + @java_implementation('()Ljava/util/ListIterator;') + def listIterator(self): + it = TestImplemIterator(self) + return it + + @java_implementation('(I)Ljava/util/ListIterator;', + name='ListIterator') + def listIteratorI(self, index): + it = TestImplemIterator(self, index) + return it + + +print '2: instanciate the class, with some data' +a = TestImplem(*range(10)) +print a +print dir(a) + +print '3: Do cast to a collection' +a2 = cast('java/util/Collection', a.j_self) +print a2 + +print '4: Try few method on the collection' +Collections = autoclass('java.util.Collections') +#print Collections.enumeration(a) +#print Collections.enumeration(a) +ret = Collections.max(a) +print 'MAX returned', ret + +# the first one of the following methods will work, witchever it is +# the next ones will fail +print "reverse" +print Collections.reverse(a) +print a.data + +print "reverse" +print Collections.reverse(a) +print a.data + +print "before swap" +print Collections.swap(a, 2, 3) +print "after swap" +print a.data + +print "rotate" +print Collections.rotate(a, 5) +print a.data + +print 'Order of data before shuffle()', a.data +print Collections.shuffle(a) +print 'Order of data after shuffle()', a.data + + +# XXX We have issues for methosd with multiple signature +#print '-> Collections.max(a)' +#print Collections.max(a2) +#print '-> Collections.max(a)' +#print Collections.max(a2) +#print '-> Collections.shuffle(a)' +#print Collections.shuffle(a2)