22High-level convenience functions for inspecting Java objects.
33"""
44
5+ from sys import stdout as _stdout
6+
57from scyjava import _introspect
68
79
8- def members (data ):
10+ def members (data , writer = None ):
911 """
1012 Print all the members (constructors, fields, and methods)
1113 for a Java class, object, or class name.
1214
1315 :param data: The Java class, object, or fully qualified class name as string.
16+ :param writer: Function to which output will be sent, sys.stdout.write by default.
1417 """
15- _print_data (data , aspect = "all" )
18+ _print_data (data , aspect = "all" , writer = writer )
1619
1720
18- def constructors (data ):
21+ def constructors (data , writer = None ):
1922 """
2023 Print the constructors for a Java class, object, or class name.
2124
2225 :param data: The Java class, object, or fully qualified class name as string.
26+ :param writer: Function to which output will be sent, sys.stdout.write by default.
2327 """
24- _print_data (data , aspect = "constructors" )
28+ _print_data (data , aspect = "constructors" , writer = writer )
2529
2630
27- def fields (data ):
31+ def fields (data , writer = None ):
2832 """
2933 Print the fields for a Java class, object, or class name.
3034
3135 :param data: The Java class, object, or fully qualified class name as string.
36+ :param writer: Function to which output will be sent, sys.stdout.write by default.
3237 """
33- _print_data (data , aspect = "fields" )
38+ _print_data (data , aspect = "fields" , writer = writer )
3439
3540
36- def methods (data ):
41+ def methods (data , writer = None ):
3742 """
3843 Print the methods for a Java class, object, or class name.
3944
4045 :param data: The Java class, object, or fully qualified class name as string.
46+ :param writer: Function to which output will be sent, sys.stdout.write by default.
4147 """
4248 _print_data (data , aspect = "methods" )
4349
4450
45- def src (data ):
51+ def src (data , writer = None ):
4652 """
4753 Print the source code URL for a Java class, object, or class name.
4854
4955 :param data: The Java class, object, or fully qualified class name as string.
56+ :param writer: Function to which output will be sent, sys.stdout.write by default.
5057 """
58+ writer = writer or _stdout .write
5159 source_url = _introspect .jsource (data )
52- print (f"Source code URL: { source_url } " )
60+ writer (f"Source code URL: { source_url } \n " )
5361
5462
5563def _map_syntax (base_type ):
@@ -106,7 +114,9 @@ def _pretty_string(entry, offset):
106114 return f"{ return_val } { modifier } = { obj_name } ({ arg_string } )\n "
107115
108116
109- def _print_data (data , aspect , static : bool | None = None , source : bool = True ):
117+ def _print_data (
118+ data , aspect , static : bool | None = None , source : bool = True , writer = None
119+ ):
110120 """
111121 Write data to a printed table with inputs, static modifier,
112122 arguments, and return values.
@@ -117,17 +127,18 @@ def _print_data(data, aspect, static: bool | None = None, source: bool = True):
117127 Optional, default is None (prints all).
118128 :param source: Whether to print any available source code. Default True.
119129 """
130+ writer = writer or _stdout .write
120131 table = _introspect .jreflect (data , aspect )
121132 if len (table ) == 0 :
122- print (f"No { aspect } found" )
133+ writer (f"No { aspect } found\n " )
123134 return
124135
125136 # Print source code
126137 offset = max (list (map (lambda entry : len (entry ["returns" ] or "void" ), table )))
127138 all_methods = ""
128139 if source :
129140 urlstring = _introspect .jsource (data )
130- print (f"Source code URL: { urlstring } " )
141+ writer (f"Source code URL: { urlstring } \n " )
131142
132143 # Print methods
133144 for entry in table :
@@ -147,7 +158,8 @@ def _print_data(data, aspect, static: bool | None = None, source: bool = True):
147158 all_methods += entry_string
148159 else :
149160 continue
161+ all_methods += "\n "
150162
151163 # 4 added to align the asterisk with output.
152- print (f"{ '' :<{offset + 4 }} * indicates static modifier" )
153- print (all_methods )
164+ writer (f"{ '' :<{offset + 4 }} * indicates static modifier\n " )
165+ writer (all_methods )
0 commit comments