Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added fields
  • Loading branch information
ian-coccimiglio committed Mar 27, 2025
commit abe73f423817253bdfecd5eab571e3181f20f90a
5 changes: 4 additions & 1 deletion src/scyjava/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,11 @@
jclass,
jinstance,
jstacktrace,
methods,
find_java_methods,
find_java_fields,
methods,
fields,
attrs,
numeric_bounds,
)
from ._versions import compare_version, get_version, is_version_at_least
Expand Down
67 changes: 39 additions & 28 deletions src/scyjava/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,9 +396,11 @@ def find_java_fields(data) -> list[dict[str, Any]]:

for f in fields:
name = f.getName()
table.append(name)
ftype = f.getType().getName()
table.append({"name": name, "type": ftype})
sorted_table = sorted(table, key=lambda d: d["name"])

return table
return sorted_table


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

# Handle fields
if entry["arguments"] is None:
return f"{return_val} = {obj_name}\n"

# Handle methods with no arguments
if not entry["arguments"]:
if len(entry["arguments"]) == 0:
return f"{return_val} {modifier} = {obj_name}()\n"
else:
arg_string = ", ".join([r.__str__() for r in entry["arguments"]])
return f"{return_val} {modifier} = {obj_name}({arg_string})\n"


# TODO
def fields(data) -> str:
"""
Writes data to a printed field names with the field value.
:param data: The object or class to inspect.
"""
# table = find_java_fields(data)

all_fields = ""
################
# FILL THIS IN #
################

print(all_fields)


# TODO
def attrs(data):
"""
Writes data to a printed field names with the field value. Alias for `fields(data)`.
:param data: The object or class to inspect.
"""
fields(data)


def get_source_code(data):
"""
Tries to find the source code using Scijava's SourceFinder'
Expand All @@ -496,6 +477,36 @@ def get_source_code(data):
return f"Unexpected {err=}, {type(err)=}"


def fields(data) -> str:
"""
Writes data to a printed field names with the field value.
:param data: The object or class to inspect.
"""
table = find_java_fields(data)
if len(table) == 0:
print("No fields found")
return

all_fields = ""
offset = max(list(map(lambda entry: len(entry["type"]), table)))
for entry in table:
entry["returns"] = _map_syntax(entry["type"])
entry["static"] = False
entry["arguments"] = None
entry_string = _make_pretty_string(entry, offset)
all_fields += entry_string

print(all_fields)


def attrs(data):
"""
Writes data to a printed field names with the field value. Alias for `fields(data)`.
:param data: The object or class to inspect.
"""
fields(data)


def methods(data, static: bool | None = None, source: bool = True) -> str:
"""
Writes data to a printed string of class methods with inputs, static modifier, arguments, and return values.
Expand Down