Skip to content

Commit 640d57d

Browse files
ian-coccimiglioctrueden
authored andcommitted
Implement fields introspection function
1 parent 1a0fd71 commit 640d57d

File tree

2 files changed

+43
-29
lines changed

2 files changed

+43
-29
lines changed

src/scyjava/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,11 @@
124124
jclass,
125125
jinstance,
126126
jstacktrace,
127-
methods,
128127
find_java_methods,
128+
find_java_fields,
129+
methods,
130+
fields,
131+
attrs,
129132
numeric_bounds,
130133
)
131134
from ._versions import compare_version, get_version, is_version_at_least

src/scyjava/_types.py

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -396,9 +396,11 @@ def find_java_fields(data) -> list[dict[str, Any]]:
396396

397397
for f in fields:
398398
name = f.getName()
399-
table.append(name)
399+
ftype = f.getType().getName()
400+
table.append({"name": name, "type": ftype})
401+
sorted_table = sorted(table, key=lambda d: d["name"])
400402

401-
return table
403+
return sorted_table
402404

403405

404406
def _map_syntax(base_type):
@@ -441,39 +443,18 @@ def _make_pretty_string(entry, offset):
441443
obj_name = f"{entry['name']}"
442444
modifier = f"{'*':>4}" if entry["static"] else f"{'':>4}"
443445

446+
# Handle fields
447+
if entry["arguments"] is None:
448+
return f"{return_val} = {obj_name}\n"
449+
444450
# Handle methods with no arguments
445-
if not entry["arguments"]:
451+
if len(entry["arguments"]) == 0:
446452
return f"{return_val} {modifier} = {obj_name}()\n"
447453
else:
448454
arg_string = ", ".join([r.__str__() for r in entry["arguments"]])
449455
return f"{return_val} {modifier} = {obj_name}({arg_string})\n"
450456

451457

452-
# TODO
453-
def fields(data) -> str:
454-
"""
455-
Writes data to a printed field names with the field value.
456-
:param data: The object or class to inspect.
457-
"""
458-
# table = find_java_fields(data)
459-
460-
all_fields = ""
461-
################
462-
# FILL THIS IN #
463-
################
464-
465-
print(all_fields)
466-
467-
468-
# TODO
469-
def attrs(data):
470-
"""
471-
Writes data to a printed field names with the field value. Alias for `fields(data)`.
472-
:param data: The object or class to inspect.
473-
"""
474-
fields(data)
475-
476-
477458
def get_source_code(data):
478459
"""
479460
Tries to find the source code using Scijava's SourceFinder'
@@ -496,6 +477,36 @@ def get_source_code(data):
496477
return f"Unexpected {err=}, {type(err)=}"
497478

498479

480+
def fields(data) -> str:
481+
"""
482+
Writes data to a printed field names with the field value.
483+
:param data: The object or class to inspect.
484+
"""
485+
table = find_java_fields(data)
486+
if len(table) == 0:
487+
print("No fields found")
488+
return
489+
490+
all_fields = ""
491+
offset = max(list(map(lambda entry: len(entry["type"]), table)))
492+
for entry in table:
493+
entry["returns"] = _map_syntax(entry["type"])
494+
entry["static"] = False
495+
entry["arguments"] = None
496+
entry_string = _make_pretty_string(entry, offset)
497+
all_fields += entry_string
498+
499+
print(all_fields)
500+
501+
502+
def attrs(data):
503+
"""
504+
Writes data to a printed field names with the field value. Alias for `fields(data)`.
505+
:param data: The object or class to inspect.
506+
"""
507+
fields(data)
508+
509+
499510
def methods(data, static: bool | None = None, source: bool = True) -> str:
500511
"""
501512
Writes data to a printed string of class methods with inputs, static modifier, arguments, and return values.

0 commit comments

Comments
 (0)