Skip to content

Commit

Permalink
Merge pull request kivy#111 from abrasive/multidim
Browse files Browse the repository at this point in the history
Add support for multidimensional arrays
  • Loading branch information
akshayaurora committed Sep 14, 2014
2 parents 7826ca5 + 0279d2e commit b38a365
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 3 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ tests: build_ext
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 && env PYTHONPATH=..:$(PYTHONPATH) nosetests-2.7 -v
25 changes: 25 additions & 0 deletions jnius/jnius_conversion.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,20 @@ cdef convert_jarray_to_python(JNIEnv *j_env, definition, jobject j_object):
obj = convert_jobject_to_python(j_env, definition, j_object_item)
ret.append(obj)
j_env[0].DeleteLocalRef(j_env, j_object_item)

elif r == '[':
r = definition[1:]
ret = []
for i in range(array_size):
j_object_item = j_env[0].GetObjectArrayElement(
j_env, j_object, i)
if j_object_item == NULL:
ret.append(None)
continue
obj = convert_jarray_to_python(j_env, r, j_object_item)
ret.append(obj)
j_env[0].DeleteLocalRef(j_env, j_object_item)

else:
raise JavaException('Invalid return definition for array')

Expand Down Expand Up @@ -526,6 +540,17 @@ cdef jobject convert_pyarray_to_java(JNIEnv *j_env, definition, pyarray) except
else:
raise JavaException('Invalid variable used for L array', definition, pyarray)

elif definition[0] == '[':
subdef = definition[1:]
eproto = convert_pyarray_to_java(j_env, subdef, pyarray[0])
ret = j_env[0].NewObjectArray(
j_env, array_size, j_env[0].GetObjectClass(j_env, eproto), NULL)
j_env[0].SetObjectArrayElement(
j_env, <jobjectArray>ret, 0, eproto)
for i in range(1, array_size):
j_env[0].SetObjectArrayElement(
j_env, <jobjectArray>ret, i, convert_pyarray_to_java(j_env, subdef, pyarray[i]))

else:
raise JavaException('Invalid array definition', definition, pyarray)

Expand Down
6 changes: 3 additions & 3 deletions jnius/jnius_utils.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ cdef parse_definition(definition):
while len(argdef):
c = argdef[0]

# read the array char
# read the array char(s)
prefix = ''
if c == '[':
prefix = c
while c == '[':
prefix += c
argdef = argdef[1:]
c = argdef[0]

Expand Down
16 changes: 16 additions & 0 deletions tests/org/jnius/MultipleDimensions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.jnius;

public class MultipleDimensions {
public static boolean methodParamsMatrixI(int[][] x) {
if (x.length != 3 || x[0].length != 3)
return false;
return (x[0][0] == 1 && x[0][1] == 2 && x[1][2] == 6);
}
public static int[][] methodReturnMatrixI() {
int[][] matrix = {{1,2,3},
{4,5,6},
{7,8,9}};
return matrix;
}

}
12 changes: 12 additions & 0 deletions tests/test_multidimension.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import unittest
from jnius.reflect import autoclass

class MultipleDimensionsTest(unittest.TestCase):

def test_multiple_dimensions(self):
MultipleDims = autoclass('org.jnius.MultipleDimensions')
matrix = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
self.assertEquals(MultipleDims.methodParamsMatrixI(matrix), True)
self.assertEquals(MultipleDims.methodReturnMatrixI(), matrix)

0 comments on commit b38a365

Please sign in to comment.