Search code examples
smalltalkpharo

How to ask an object to return its class and methods source as a string?


I want to find the source of a Class and its methods (including all variables too, local , instance, class , class instance etc) and return them as a string or a collection of strings.

I have found MethodNode>>sourceText and CompiledMethod>>sourceCode but I have no clue how to send these messages or if they are the messages I am looking for.

Also any pointers as to how to navigate the Pharo syntax by code using something like AST would be greatly appreciated.


Solution

  • First ask the class of your object, then ask, for example, all the methods defined in this class and then collect the source-code of each method:

    'Pharo' class methods collect: [ :each | each sourceCode ]
    

    You can find the relevant methods for other properties of your class in the accessing protocols of Behavior, a superclass of Class.