2
2
High-level convenience functions for inspecting Java objects.
3
3
"""
4
4
5
+ from sys import stdout as _stdout
6
+
5
7
from scyjava import _introspect
6
8
7
9
8
- def members (data ):
10
+ def members (data , writer = None ):
9
11
"""
10
12
Print all the members (constructors, fields, and methods)
11
13
for a Java class, object, or class name.
12
14
13
15
: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.
14
17
"""
15
- _print_data (data , aspect = "all" )
18
+ _print_data (data , aspect = "all" , writer = writer )
16
19
17
20
18
- def constructors (data ):
21
+ def constructors (data , writer = None ):
19
22
"""
20
23
Print the constructors for a Java class, object, or class name.
21
24
22
25
: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.
23
27
"""
24
- _print_data (data , aspect = "constructors" )
28
+ _print_data (data , aspect = "constructors" , writer = writer )
25
29
26
30
27
- def fields (data ):
31
+ def fields (data , writer = None ):
28
32
"""
29
33
Print the fields for a Java class, object, or class name.
30
34
31
35
: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.
32
37
"""
33
- _print_data (data , aspect = "fields" )
38
+ _print_data (data , aspect = "fields" , writer = writer )
34
39
35
40
36
- def methods (data ):
41
+ def methods (data , writer = None ):
37
42
"""
38
43
Print the methods for a Java class, object, or class name.
39
44
40
45
: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.
41
47
"""
42
48
_print_data (data , aspect = "methods" )
43
49
44
50
45
- def src (data ):
51
+ def src (data , writer = None ):
46
52
"""
47
53
Print the source code URL for a Java class, object, or class name.
48
54
49
55
: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.
50
57
"""
58
+ writer = writer or _stdout .write
51
59
source_url = _introspect .jsource (data )
52
- print (f"Source code URL: { source_url } " )
60
+ writer (f"Source code URL: { source_url } \n " )
53
61
54
62
55
63
def _map_syntax (base_type ):
@@ -106,7 +114,9 @@ def _pretty_string(entry, offset):
106
114
return f"{ return_val } { modifier } = { obj_name } ({ arg_string } )\n "
107
115
108
116
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
+ ):
110
120
"""
111
121
Write data to a printed table with inputs, static modifier,
112
122
arguments, and return values.
@@ -117,17 +127,18 @@ def _print_data(data, aspect, static: bool | None = None, source: bool = True):
117
127
Optional, default is None (prints all).
118
128
:param source: Whether to print any available source code. Default True.
119
129
"""
130
+ writer = writer or _stdout .write
120
131
table = _introspect .jreflect (data , aspect )
121
132
if len (table ) == 0 :
122
- print (f"No { aspect } found" )
133
+ writer (f"No { aspect } found\n " )
123
134
return
124
135
125
136
# Print source code
126
137
offset = max (list (map (lambda entry : len (entry ["returns" ] or "void" ), table )))
127
138
all_methods = ""
128
139
if source :
129
140
urlstring = _introspect .jsource (data )
130
- print (f"Source code URL: { urlstring } " )
141
+ writer (f"Source code URL: { urlstring } \n " )
131
142
132
143
# Print methods
133
144
for entry in table :
@@ -147,7 +158,8 @@ def _print_data(data, aspect, static: bool | None = None, source: bool = True):
147
158
all_methods += entry_string
148
159
else :
149
160
continue
161
+ all_methods += "\n "
150
162
151
163
# 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