33class methods, fields, and source code URL.
44"""
55
6- from functools import partial
7- from typing import Any , Dict , List , Optional
6+ from typing import Any , Dict , List
87
98from scyjava ._jvm import jimport
109from scyjava ._types import isjava , jinstance , jclass
@@ -64,7 +63,7 @@ def jreflect(data, aspect: str = "all") -> List[Dict[str, Any]]:
6463 returns = (
6564 member .getReturnType ().getName ()
6665 if hasattr (member , "getReturnType" )
67- else (member .getType ().getName () if hasattr (member , "getType" ) else None )
66+ else (member .getType ().getName () if hasattr (member , "getType" ) else name )
6867 )
6968 table .append (
7069 {
@@ -79,58 +78,6 @@ def jreflect(data, aspect: str = "all") -> List[Dict[str, Any]]:
7978 return table
8079
8180
82- def _map_syntax (base_type ):
83- """
84- Map a Java BaseType annotation (see link below) in an Java array
85- to a specific type with an Python interpretable syntax.
86- https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.3
87- """
88- basetype_mapping = {
89- "[B" : "byte[]" ,
90- "[C" : "char[]" ,
91- "[D" : "double[]" ,
92- "[F" : "float[]" ,
93- "[I" : "int[]" ,
94- "[J" : "long[]" ,
95- "[L" : "[]" , # array
96- "[S" : "short[]" ,
97- "[Z" : "boolean[]" ,
98- }
99-
100- if base_type in basetype_mapping :
101- return basetype_mapping [base_type ]
102- # Handle the case of a returned array of an object
103- elif base_type .__str__ ().startswith ("[L" ):
104- return base_type .__str__ ()[2 :- 1 ] + "[]"
105- else :
106- return base_type
107-
108-
109- def _make_pretty_string (entry , offset ):
110- """
111- Print the entry with a specific formatting and aligned style.
112- :param entry: Dictionary of class names, modifiers, arguments, and return values.
113- :param offset: Offset between the return value and the method.
114- """
115-
116- # A star implies that the method is a static method
117- return_val = f"{ entry ['returns' ].__str__ ():<{offset }} "
118- # Handle whether to print static/instance modifiers
119- obj_name = f"{ entry ['name' ]} "
120- modifier = f"{ '*' :>4} " if "static" in entry ["mods" ] else f"{ '' :>4} "
121-
122- # Handle fields
123- if entry ["arguments" ] is None :
124- return f"{ return_val } { modifier } = { obj_name } \n "
125-
126- # Handle methods with no arguments
127- if len (entry ["arguments" ]) == 0 :
128- return f"{ return_val } { modifier } = { obj_name } ()\n "
129- else :
130- arg_string = ", " .join ([r .__str__ () for r in entry ["arguments" ]])
131- return f"{ return_val } { modifier } = { obj_name } ({ arg_string } )\n "
132-
133-
13481def jsource (data ):
13582 """
13683 Try to find the source code using SciJava's SourceFinder.
@@ -162,66 +109,3 @@ def jsource(data):
162109 return f"Not a Java class { str (type (data ))} "
163110 except Exception as err :
164111 return f"Unexpected { err = } , { type (err )= } "
165-
166-
167- def _print_data (data , aspect , static : Optional [bool ] = None , source : bool = True ):
168- """
169- Write data to a printed string of class methods with inputs, static modifier,
170- arguments, and return values.
171-
172- :param data: The object or class to inspect or fully qualified class name.
173- :param aspect: Whether to print class "fields" or "methods".
174- :param static:
175- Boolean filter on Static or Instance methods.
176- Optional, default is None (prints all).
177- :param source: Whether to print any available source code. Default True.
178- """
179- table = jreflect (data , aspect )
180- if len (table ) == 0 :
181- print (f"No { aspect } found" )
182- return
183-
184- # Print source code
185- offset = max (list (map (lambda entry : len (entry ["returns" ]), table )))
186- all_methods = ""
187- if source :
188- urlstring = jsource (data )
189- print (f"Source code URL: { urlstring } " )
190-
191- # Print methods
192- for entry in table :
193- entry ["returns" ] = _map_syntax (entry ["returns" ])
194- if entry ["arguments" ]:
195- entry ["arguments" ] = [_map_syntax (e ) for e in entry ["arguments" ]]
196- if static is None :
197- entry_string = _make_pretty_string (entry , offset )
198- all_methods += entry_string
199-
200- elif static and entry ["static" ]:
201- entry_string = _make_pretty_string (entry , offset )
202- all_methods += entry_string
203- elif not static and not entry ["static" ]:
204- entry_string = _make_pretty_string (entry , offset )
205- all_methods += entry_string
206- else :
207- continue
208-
209- # 4 added to align the asterisk with output.
210- print (f"{ '' :<{offset + 4 }} * indicates static modifier" )
211- print (all_methods )
212-
213-
214- # The functions with short names for quick usage.
215- methods = partial (_print_data , aspect = "methods" )
216- fields = partial (_print_data , aspect = "fields" )
217- attrs = partial (_print_data , aspect = "fields" )
218-
219-
220- def src (data ):
221- """
222- Print the source code URL for a Java class, object, or class name.
223-
224- :param data: The Java class, object, or fully qualified class name as string
225- """
226- source_url = jsource (data )
227- print (f"Source code URL: { source_url } " )
0 commit comments