From 6d91a2fe6278feb2c98a90a0a967f16eef82d9e8 Mon Sep 17 00:00:00 2001 From: Benson Margulies Date: Thu, 30 Apr 2015 14:04:58 -0400 Subject: [PATCH 1/4] Fix 159: passing the result of autoclass to java.lang.Class parameter. --- jnius/jnius_utils.pxi | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/jnius/jnius_utils.pxi b/jnius/jnius_utils.pxi index 31bdc6d1..2e2b0ae4 100644 --- a/jnius/jnius_utils.pxi +++ b/jnius/jnius_utils.pxi @@ -278,6 +278,11 @@ cdef int calculate_score(sign_args, args, is_varargs=False) except *: continue return -1 + # accept an autoclass class for java/lang/Class. + if hasattr(arg, '__javaclass__') and r == 'java/lang/Class': + score += 10 + continue + # if we pass a JavaClass, ensure the definition is matching # XXX FIXME what if we use a subclass or something ? if isinstance(arg, JavaClass): From 0c86904ede1b72f8539bc757b6745c069ad01fb4 Mon Sep 17 00:00:00 2001 From: Benson Margulies Date: Thu, 30 Apr 2015 16:13:16 -0400 Subject: [PATCH 2/4] Support to map getters to properties. --- jnius/reflect.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/jnius/reflect.py b/jnius/reflect.py index 8e260df7..ceaaa9de 100644 --- a/jnius/reflect.py +++ b/jnius/reflect.py @@ -137,6 +137,11 @@ def ensureclass(clsname): registers.append(clsname) autoclass(clsname) +def lower_name(s): + return s[:1].lower() + s[1:] if s else '' + +def bean_getter(s): + return (s.startswith('get') and len(s) > 3 and s[3].isupper()) or (s.startswith('is') and len(s) > 2 and s[2].isupper()) def autoclass(clsname): jniname = clsname.replace('.', '/') @@ -176,9 +181,12 @@ def autoclass(clsname): get_signature(method.getReturnType())) cls = JavaStaticMethod if static else JavaMethod classDict[name] = cls(sig, varargs=varargs) + if name != 'getClass' and bean_getter(name) and len(method.getParameterTypes()) == 0: + lowername = lower_name(name[3:]) + classDict[lowername] = (lambda n: property(lambda self: getattr(self, n)()))(name) continue - # multpile signatures + # multiple signatures signatures = [] for index, subname in enumerate(methods_name): if subname != name: From 2753c59573bf353e3729cf24c769d79b1705ae91 Mon Sep 17 00:00:00 2001 From: Benson Margulies Date: Thu, 30 Apr 2015 16:24:27 -0400 Subject: [PATCH 3/4] Make lists work like Python list containers. --- jnius/reflect.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/jnius/reflect.py b/jnius/reflect.py index ceaaa9de..2e0f1f56 100644 --- a/jnius/reflect.py +++ b/jnius/reflect.py @@ -215,6 +215,12 @@ def autoclass(clsname): classDict[name] = JavaMultipleMethod(signatures) + for iclass in c.getInterfaces(): + if iclass.getName() == 'java.util.List': + classDict['__getitem__'] = lambda self, index: self.get(index) + classDict['__len__'] = lambda self: self.size() + break + for field in c.getFields(): static = Modifier.isStatic(field.getModifiers()) sig = get_signature(field.getType()) From 0ab1468bc5d75f1488a001a951dc557120a78ba7 Mon Sep 17 00:00:00 2001 From: Benson Margulies Date: Thu, 30 Apr 2015 19:36:56 -0400 Subject: [PATCH 4/4] Fix issue #162 by making 'make tests' pass. --- Makefile | 24 ++++++++++++++++-------- tests/test_proxy.py | 12 ++++++++++-- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index e815409e..ada7e905 100644 --- a/Makefile +++ b/Makefile @@ -1,18 +1,26 @@ .PHONY: build_ext tests +JAVAC_OPTS=-target 1.6 -source 1.6 +JAVAC=javac $(JAVAC_OPTS) + build_ext: - javac jnius/src/org/jnius/NativeInvocationHandler.java + $(JAVAC) jnius/src/org/jnius/NativeInvocationHandler.java python setup.py build_ext --inplace -f -g +clean: + find . -name "*.class" -exec rm {} \; + rm -rf build + html: $(MAKE) -C docs html tests: build_ext - cd tests && javac org/jnius/HelloWorld.java - cd tests && javac org/jnius/BasicsTest.java - cd tests && javac org/jnius/MultipleMethods.java - cd tests && javac org/jnius/SimpleEnum.java - cd tests && javac org/jnius/InterfaceWithPublicEnum.java - cd tests && javac org/jnius/ClassArgument.java - cd tests && javac org/jnius/MultipleDimensions.java + cd tests && $(JAVAC) org/jnius/HelloWorld.java + cd tests && $(JAVAC) org/jnius/BasicsTest.java + cd tests && $(JAVAC) org/jnius/MultipleMethods.java + cd tests && $(JAVAC) org/jnius/SimpleEnum.java + cd tests && $(JAVAC) org/jnius/InterfaceWithPublicEnum.java + cd tests && $(JAVAC) org/jnius/ClassArgument.java + cd tests && $(JAVAC) org/jnius/MultipleDimensions.java + cp jnius/src/org/jnius/NativeInvocationHandler.class tests/org/jnius cd tests && env PYTHONPATH=..:$(PYTHONPATH) nosetests-2.7 -v diff --git a/tests/test_proxy.py b/tests/test_proxy.py index 7a5d7be4..c7630ce4 100644 --- a/tests/test_proxy.py +++ b/tests/test_proxy.py @@ -1,4 +1,5 @@ from jnius import autoclass, java_method, PythonJavaClass, cast +from nose.tools import * print '1: declare a TestImplem that implement Collection' @@ -104,7 +105,7 @@ def bad_signature(self, *args): pass -print '2: instanciate the class, with some data' +print '2: instantiate the class, with some data' a = TestImplem(*range(10)) print a print dir(a) @@ -150,4 +151,11 @@ def bad_signature(self, *args): #print Collections.shuffle(a2) # test bad signature -TestBadSignature() +threw = False +try: + TestBadSignature() +except Exception: + threw = True + +if not threw: + raise Exception("Failed to throw for bad signature")