Skip to content

Commit bc8be76

Browse files
committed
Add a jinstance method for checking Java type
1 parent d8c54ad commit bc8be76

File tree

5 files changed

+33
-13
lines changed

5 files changed

+33
-13
lines changed

src/scyjava/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
jarray,
103103
jclass,
104104
jimport,
105+
jinstance,
105106
jstacktrace,
106107
jvm_started,
107108
jvm_version,

src/scyjava/_convert.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from jpype import JArray, JBoolean, JByte, JChar, JDouble, JFloat, JInt, JLong, JShort
1010

11-
from ._java import JavaClasses, isjava, jclass, jimport, start_jvm
11+
from ._java import JavaClasses, isjava, jclass, jimport, jinstance, start_jvm
1212

1313

1414
# NB: We cannot use org.scijava.priority.Priority or other Java-side class
@@ -681,7 +681,7 @@ def _import_numpy(required=True):
681681
def _is_table(obj: Any) -> bool:
682682
"""Check if obj is a table."""
683683
try:
684-
return isinstance(obj, jimport("org.scijava.table.Table"))
684+
return jinstance(obj, "org.scijava.table.Table")
685685
except BaseException:
686686
# No worries if scijava-table is not available.
687687
pass

src/scyjava/_java.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,20 @@ def jclass(data):
384384
raise TypeError("Cannot glean class from data of type: " + str(type(data)))
385385

386386

387+
def jinstance(obj, jtype):
388+
"""
389+
Test if the given object is an instance of a particular Java type.
390+
391+
:param obj: The object to check.
392+
:param jtype: The Java type, as either a jimported class or as a string.
393+
:returns: True iff the object is an instance of that Java type.
394+
"""
395+
if isinstance(jtype, str):
396+
jtype = jimport(jtype)
397+
398+
return isinstance(obj, jtype)
399+
400+
387401
def jstacktrace(exc):
388402
"""
389403
Extract the Java-side stack trace from a Java exception.

tests/test_convert.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,16 @@
33

44
from jpype import JByte
55

6-
from scyjava import Converter, config, jarray, jclass, jimport, to_java, to_python
6+
from scyjava import (
7+
Converter,
8+
config,
9+
jarray,
10+
jclass,
11+
jimport,
12+
jinstance,
13+
to_java,
14+
to_python,
15+
)
716

817
config.endpoints.append("org.scijava:scijava-table")
918
config.add_option("-Djava.awt.headless=true")
@@ -182,7 +191,7 @@ def testDict(self):
182191
def testPath(self):
183192
py_path = Path(getcwd())
184193
j_path = to_java(py_path)
185-
assert isinstance(j_path, jimport("java.nio.file.Path"))
194+
assert jinstance(j_path, "java.nio.file.Path")
186195
assert str(j_path) == str(py_path)
187196

188197
actual = to_python(j_path)

tests/test_pandas.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import numpy.testing as npt
33
import pandas as pd
44

5-
from scyjava import config, jimport, to_java
5+
from scyjava import config, jinstance, to_java
66

77
config.endpoints.append("org.scijava:scijava-table")
88
config.add_option("-Djava.awt.headless=true")
@@ -28,8 +28,7 @@ def testPandasToTable(self):
2828
table = to_java(df)
2929

3030
assert_same_table(table, df)
31-
DefaultFloatTable = jimport("org.scijava.table.DefaultFloatTable")
32-
assert isinstance(table, DefaultFloatTable)
31+
assert jinstance(table, "org.scijava.table.DefaultFloatTable")
3332

3433
# Int table.
3534
columns = ["header1", "header2", "header3", "header4", "header5"]
@@ -40,8 +39,7 @@ def testPandasToTable(self):
4039
table = to_java(df)
4140

4241
assert_same_table(table, df)
43-
DefaultIntTable = jimport("org.scijava.table.DefaultIntTable")
44-
assert isinstance(table, DefaultIntTable)
42+
assert jinstance(table, "org.scijava.table.DefaultIntTable")
4543

4644
# Bool table.
4745
columns = ["header1", "header2", "header3", "header4", "header5"]
@@ -51,8 +49,7 @@ def testPandasToTable(self):
5149
table = to_java(df)
5250

5351
assert_same_table(table, df)
54-
DefaultBoolTable = jimport("org.scijava.table.DefaultBoolTable")
55-
assert isinstance(table, DefaultBoolTable)
52+
assert jinstance(table, "org.scijava.table.DefaultBoolTable")
5653

5754
# Mixed table.
5855
columns = ["header1", "header2", "header3", "header4", "header5"]
@@ -71,5 +68,4 @@ def testPandasToTable(self):
7168

7269
# Table types cannot be the same here, unless we want to cast.
7370
# assert_same_table(table, df)
74-
DefaultGenericTable = jimport("org.scijava.table.DefaultGenericTable")
75-
assert isinstance(table, DefaultGenericTable)
71+
assert jinstance(table, "org.scijava.table.DefaultGenericTable")

0 commit comments

Comments
 (0)