I created a class, and in that class I have a method 'sendMessage: to: withArgs:' which recieves an object, a message and an array of arguments. The method is used to send messages to object and perform some algorithm. To use this method I have to create an instance x of the class I created and do something like x sendMessage: '+' to: '7' withArgs: '#(5)'. The result of this sending the message '+' to the object 7 with the parameter 5, plus some stuff that my algorithm does. But what I want is that the algorithm will be used in every method call, meaning 7+5 will call my 'sendMessage: to: withArgs:'. How can I do that? Or at least, Is there something called in each method sent to every object?
我创建了一个类,在该类中我有一个方法'sendMessage:to:withArgs:',它接收一个对象,一个消息和一个参数数组。该方法用于向对象发送消息并执行某些算法。要使用此方法,我必须创建我创建的类的实例x,并执行类似x sendMessage的操作:'+'到:'7'withArgs:'#(5)'。这将使用参数5将消息“+”发送到对象7,加上我的算法所做的一些事情。但我想要的是该算法将在每个方法调用中使用,这意味着7 + 5将调用我的'sendMessage:to:withArgs:'。我怎样才能做到这一点?或者至少,每个方法中是否有调用的方法发送到每个对象?
4 个解决方案
#1
It's kinda funny, we were just discussing that in the Squeak irc channel. Take a peek at ObjectViewer, perhaps.
这有点好笑,我们只是在Squeak irc频道讨论这个问题。也许可以看一下ObjectViewer。
In your example, you want to intercept the message sends to a SmallInteger. Funnily enough, ObjectViewer works with very much every class BUT SmallInteger.
在您的示例中,您希望拦截发送到SmallInteger的消息。有趣的是,ObjectViewer非常适用于每个类但都是SmallInteger。
So, to intercept message sends to myObject, do this.
因此,要拦截消息发送到myObject,请执行此操作。
Create class Intercepter, let it inherit from ObjectTracer, perhaps. Change doesNotUnderstand to something that serves you:
创建类Intercepter,让它继承ObjectTracer。将doNotUnderstand更改为为您服务的东西:
doesNotUnderstand: aMessage
"do Mojo to aMessage as you describe it"
Then, to get your stuff going, create your Intercepter:
然后,为了让你的东西继续下去,创建你的Intercepter:
myIntercepter := Intercepter on: myObject.
And then
myObject become: myInterceptor.
#2
In Squeak, see the class ObjectTracer. The class comment describes how to use it. You should be able to accomplish what you need with that, or at least using that as a model.
在Squeak中,请参阅ObjectTracer类。课程评论描述了如何使用它。您应该能够完成所需的工作,或者至少将其作为模型使用。
#3
Have a look at the Reflectivity.
看看反射率。
Unfortunately some of the paper links are not working, and I don't remember the exact invocation from the top of my head, but it's really easy to instrument code as you want, and even do it at runtime. Look for examples using class Link
.
不幸的是,一些纸质链接无法正常工作,我不记得从头顶开始的确切调用,但是根据需要设置代码非常容易,甚至可以在运行时进行。使用类Link查找示例。
#4
You can use method wrappers. To see what method wrappers are you can look for a paper called "Wrappers to the rescue". I think there is a package for squeak that already implements method wrappers. In addition, you can see how a test code coverage analysis is made in the last version of Pharo because it uses a kind of method wrapper to see what methods are evaluated during a test run. cheers, Gaboto
您可以使用方法包装器。要查看包装器的方法,您可以查找一篇名为“Wrappers to the rescue”的论文。我认为有一个squeak包已经实现了方法包装器。此外,您还可以看到在Pharo的最新版本中如何进行测试代码覆盖率分析,因为它使用一种方法包装器来查看在测试运行期间评估的方法。欢呼,Gaboto
#1
It's kinda funny, we were just discussing that in the Squeak irc channel. Take a peek at ObjectViewer, perhaps.
这有点好笑,我们只是在Squeak irc频道讨论这个问题。也许可以看一下ObjectViewer。
In your example, you want to intercept the message sends to a SmallInteger. Funnily enough, ObjectViewer works with very much every class BUT SmallInteger.
在您的示例中,您希望拦截发送到SmallInteger的消息。有趣的是,ObjectViewer非常适用于每个类但都是SmallInteger。
So, to intercept message sends to myObject, do this.
因此,要拦截消息发送到myObject,请执行此操作。
Create class Intercepter, let it inherit from ObjectTracer, perhaps. Change doesNotUnderstand to something that serves you:
创建类Intercepter,让它继承ObjectTracer。将doNotUnderstand更改为为您服务的东西:
doesNotUnderstand: aMessage
"do Mojo to aMessage as you describe it"
Then, to get your stuff going, create your Intercepter:
然后,为了让你的东西继续下去,创建你的Intercepter:
myIntercepter := Intercepter on: myObject.
And then
myObject become: myInterceptor.
#2
In Squeak, see the class ObjectTracer. The class comment describes how to use it. You should be able to accomplish what you need with that, or at least using that as a model.
在Squeak中,请参阅ObjectTracer类。课程评论描述了如何使用它。您应该能够完成所需的工作,或者至少将其作为模型使用。
#3
Have a look at the Reflectivity.
看看反射率。
Unfortunately some of the paper links are not working, and I don't remember the exact invocation from the top of my head, but it's really easy to instrument code as you want, and even do it at runtime. Look for examples using class Link
.
不幸的是,一些纸质链接无法正常工作,我不记得从头顶开始的确切调用,但是根据需要设置代码非常容易,甚至可以在运行时进行。使用类Link查找示例。
#4
You can use method wrappers. To see what method wrappers are you can look for a paper called "Wrappers to the rescue". I think there is a package for squeak that already implements method wrappers. In addition, you can see how a test code coverage analysis is made in the last version of Pharo because it uses a kind of method wrapper to see what methods are evaluated during a test run. cheers, Gaboto
您可以使用方法包装器。要查看包装器的方法,您可以查找一篇名为“Wrappers to the rescue”的论文。我认为有一个squeak包已经实现了方法包装器。此外,您还可以看到在Pharo的最新版本中如何进行测试代码覆盖率分析,因为它使用一种方法包装器来查看在测试运行期间评估的方法。欢呼,Gaboto