Skip to content

Commit

Permalink
removed _JArrayGeneric in stub and improved test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
astrelsky committed Sep 3, 2024
1 parent 18313e3 commit c724123
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 16 deletions.
6 changes: 1 addition & 5 deletions jpype/_jarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def __class_getitem__(cls, key):
key = _jpype.JClass(key)
if isinstance(key, _jpype.JClass):
return type(key[0])
raise TypeError("Cannot instantiate unspecified array type")
raise TypeError("%s is not a Java class" % key)


class _JArrayProto(object):
Expand All @@ -116,10 +116,6 @@ def __iter__(self):
def __reversed__(self):
for elem in self[::-1]:
yield elem

def __contains__(self, item):
# "in" works without this but this should be more efficient
return _jpype.JClass("java.util.Arrays").asList(self).contains(item)

def clone(self):
""" Clone the Java array.
Expand Down
27 changes: 19 additions & 8 deletions jpype/_jarray.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
# See NOTICE file for details.
#
# *****************************************************************************
import abc
import collections.abc
import typing

Expand All @@ -26,18 +25,30 @@ __all__ = ['JArray']
T = typing.TypeVar('T')


class _JArrayGeneric(collections.abc.Sequence[T]):
class JArray(collections.abc.Collection[T], collections.abc.Reversible[T]):

@abc.abstractmethod
def __setitem__(self, index, value):
def __new__(cls, tp, dims=1):
...

@classmethod
def of(cls, array, dtype=None):
...

class JArray(_JArrayGeneric[T], metaclass=abc.ABCMeta):
def __len__(self):
...

def __new__(cls, tp, dims=1):
def __iter__(self):
...

@classmethod
def of(cls, array, dtype=None):
def __contains__(self, item):
# this is implicitly implemented
...

def __reversed__(self):
...

def __getitem__(self, key):
...

def __setitem__(self, index, value):
...
1 change: 0 additions & 1 deletion jpype/_jclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ def __new__(cls, jc, loader=None, initialize=True):
# Pass to class factory to create the type
return _jpype._getClass(jc)

@classmethod
def __class_getitem__(cls, index):
# enables JClass[1] to get a Class[]
return JClass("java.lang.Class")[index]
Expand Down
6 changes: 6 additions & 0 deletions native/python/pyjp_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,12 @@ static PyObject *PyJPClass_array(PyJPClass *self, PyObject *item)

if (self->m_Class == NULL)
{
// This is only reachable through an internal Jpype type that doesn't have a
// Java class equivalent such as JArray.
// __getitem__ takes precedence over __class_getitem__ which forces us through
// this path.
// If __class_getitem__ is not implemented in this class, the raised AttributeError
// will make its way back to Python.
PyObject *res = PyObject_CallMethod((PyObject *)self, "__class_getitem__", "O", item);
Py_DECREF(item);
return res;
Expand Down
14 changes: 12 additions & 2 deletions test/jpypetest/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,12 +604,22 @@ def testJArrayIndex(self):
jpype.JArray[10]

def testJArrayGeneric(self):
self.assertEqual(type(JObject[0]), JArray(JObject))
self.assertEqual(type(JObject[1]), JArray[JObject])

def testJArrayGeneric_Init(self):
Arrays = JClass("java.util.Arrays")
self.assertTrue(Arrays.equals(JObject[0], JArray(JObject)(0)))
self.assertTrue(Arrays.equals(JObject[0], JArray[JObject](0)))

def testJArrayInvalidGeneric(self):
with self.assertRaises(TypeError):
jpype.JArray[object]

def testJArrayGenericJClass(self):
self.assertEqual(type(JClass[0]), JArray[JClass])

def testJArrayGenericString(self):
self.assertEqual(type(JClass[0]), JArray["java.lang.Class"])

def testJArrayGenericStringInvalid(self):
with self.assertRaises(TypeError):
JArray["foo.bar"] # type: ignore

0 comments on commit c724123

Please sign in to comment.