简体   繁体   English

如何要求对象以字符串形式返回其类和方法源?

[英]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. 我找到了MethodNode>>sourceTextCompiledMethod>>sourceCode但是我不知道如何发送这些消息或它们是否是我正在寻找的消息。

Also any pointers as to how to navigate the Pharo syntax by code using something like AST would be greatly appreciated. 同样,关于如何使用诸如AST类的代码通过代码来导航Pharo语法的任何指针也将不胜感激。

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 . 您可以在Behavior访问协议Class的超类)中找到与该类的其他属性有关的方法。

In addition to the answer of Martin there are also other ways of doing this for example 除了马丁的答案之外,还有其他方法可以例如

(Object compiledMethodAt: #printString) sourceCode

will return the source code for a specific method. 将返回特定方法的源代码。

In case we want to navigate through the syntax using AST of a method: 如果我们想使用方法的AST浏览语法:

( Object compiledMethodAt: #halt ) ast nodesDo:[ :node |  node inspect].

Take a look at RBProgramNode and its relevant subclasses to have taste what is possible for navigating and processing source code. 看一下RBProgramNode及其相关的子类,以了解导航和处理源代码的可能性。

并且,根据您的确切需求,您应该注意特征,扩展方法和超类中定义的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM