@@ -336,9 +336,9 @@ def find_java_methods(data) -> list[dict[str, Any]]:
336336 if not isjava (data ):
337337 raise ValueError ("Not a Java object" )
338338
339- cls = data if jinstance (data , "java.lang.Class" ) else jclass (data )
339+ jcls = data if jinstance (data , "java.lang.Class" ) else jclass (data )
340340
341- methods = cls .getMethods ()
341+ methods = jcls .getMethods ()
342342
343343 # NB: Methods are returned in inconsistent order.
344344 # Arrays.sort(methods, (m1, m2) -> {
@@ -389,9 +389,9 @@ def find_java_fields(data) -> list[dict[str, Any]]:
389389 if not isjava (data ):
390390 raise ValueError ("Not a Java object" )
391391
392- cls = data if jinstance (data , "java.lang.Class" ) else jclass (data )
392+ jcls = data if jinstance (data , "java.lang.Class" ) else jclass (data )
393393
394- fields = cls .getFields ()
394+ fields = jcls .getFields ()
395395 table = []
396396
397397 for f in fields :
@@ -474,21 +474,64 @@ def attrs(data):
474474 fields (data )
475475
476476
477- def methods (data ) -> str :
477+ def get_source_code (data ):
478+ """
479+ Tries to find the source code using Scijava's SourceFinder'
480+ :param data: The object or class to check for source code.
481+ """
482+ types = jimport ("org.scijava.util.Types" )
483+ sf = jimport ("org.scijava.search.SourceFinder" )
484+ jstring = jimport ("java.lang.String" )
485+ try :
486+ jcls = data if jinstance (data , "java.lang.Class" ) else jclass (data )
487+ if types .location (jcls ).toString ().startsWith (jstring ("jrt" )):
488+ # Handles Java RunTime (jrt) exceptions.
489+ return "GitHub source code not available"
490+ url = sf .sourceLocation (jcls , None )
491+ urlstring = url .toString ()
492+ return urlstring
493+ except jimport ("java.lang.IllegalArgumentException" ) as err :
494+ return f"Illegal argument provided { err = } , { type (err )= } "
495+ except Exception as err :
496+ return f"Unexpected { err = } , { type (err )= } "
497+
498+
499+ def methods (data , static : bool | None = None , source : bool = True ) -> str :
478500 """
479501 Writes data to a printed string of class methods with inputs, static modifier, arguments, and return values.
480502
481503 :param data: The object or class to inspect.
504+ :param static: Which methods to print. Can be set as boolean to filter the class methods based on
505+ static vs. instance methods. Optional, default is None (prints all methods).
506+ :param source: Whether to print any available source code. Default True.
482507 """
483508 table = find_java_methods (data )
484509
510+ # Print source code
485511 offset = max (list (map (lambda entry : len (entry ["returns" ]), table )))
486512 all_methods = ""
513+ if source :
514+ urlstring = get_source_code (data )
515+ print (f"URL: { urlstring } " )
516+ else :
517+ pass
518+
519+ # Print methods
487520 for entry in table :
488521 entry ["returns" ] = _map_syntax (entry ["returns" ])
489522 entry ["arguments" ] = [_map_syntax (e ) for e in entry ["arguments" ]]
490- entry_string = _make_pretty_string (entry , offset )
491- all_methods += entry_string
523+ if static is None :
524+ entry_string = _make_pretty_string (entry , offset )
525+ all_methods += entry_string
526+
527+ elif static and entry ["static" ]:
528+ entry_string = _make_pretty_string (entry , offset )
529+ all_methods += entry_string
530+ elif not static and not entry ["static" ]:
531+ entry_string = _make_pretty_string (entry , offset )
532+ all_methods += entry_string
533+ else :
534+ continue
492535
493536 # 4 added to align the asterisk with output.
494537 print (f"{ '' :<{offset + 4 }} * indicates a static method" )
0 commit comments