Skip to content

Commit 7b8a6c4

Browse files
ian-coccimiglioctrueden
authored andcommitted
Update methods() functionality
1 parent 0a8141c commit 7b8a6c4

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

src/scyjava/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
jinstance,
126126
jstacktrace,
127127
methods,
128+
find_java_methods,
128129
numeric_bounds,
129130
)
130131
from ._versions import compare_version, get_version, is_version_at_least

src/scyjava/_types.py

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ def numeric_bounds(
324324
return None, None
325325

326326

327-
def methods(data) -> list[dict[str, Any]]:
327+
def find_java_methods(data) -> list[dict[str, Any]]:
328328
"""
329329
Use Java reflection to introspect the given Java object,
330330
returning a table of its available methods.
@@ -369,8 +369,59 @@ def methods(data) -> list[dict[str, Any]]:
369369
"returns": returns,
370370
}
371371
)
372+
sorted_table = sorted(table, key=lambda d: d["name"])
372373

373-
return table
374+
return sorted_table
375+
376+
377+
def map_syntax(base_type):
378+
"""
379+
Maps a java BaseType annotation (see link below) in an Java array
380+
to a specific type with an Python interpretable syntax.
381+
https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.3
382+
"""
383+
basetype_mapping = {
384+
"[B": "byte[]",
385+
"[C": "char[]",
386+
"[D": "double[]",
387+
"[F": "float[]",
388+
"[C": "int[]",
389+
"[J": "long[]",
390+
"[L": "[]", # array
391+
"[S": "short[]",
392+
"[Z": "boolean[]",
393+
}
394+
395+
if base_type in basetype_mapping:
396+
return basetype_mapping[base_type]
397+
elif base_type.__str__().startswith("[L"):
398+
return base_type.__str__()[2:-1] + "[]"
399+
else:
400+
return base_type
401+
402+
403+
def methods(data) -> str:
404+
table = find_java_methods(data)
405+
406+
offset = max(list(map(lambda l: len(l["returns"]), table)))
407+
all_methods = ""
408+
409+
for entry in table:
410+
entry["returns"] = map_syntax(entry["returns"])
411+
entry["arguments"] = [map_syntax(e) for e in entry["arguments"]]
412+
413+
if not entry["arguments"]:
414+
all_methods = (
415+
all_methods
416+
+ f'{entry["returns"].__str__():<{offset}} = {entry["name"]}()\n'
417+
)
418+
else:
419+
arg_string = ", ".join([r.__str__() for r in entry["arguments"]])
420+
all_methods = (
421+
all_methods
422+
+ f'{entry["returns"].__str__():<{offset}} = {entry["name"]}({arg_string})\n'
423+
)
424+
print(all_methods)
374425

375426

376427
def _is_jtype(the_type: type, class_name: str) -> bool:

0 commit comments

Comments
 (0)