hi there,
i have a question about squeak object model. how can i check whether a method contains a certain piece of code? i.e if the method "isKindOf:" has the code "class inheritsFrom" - return true, otherwise - return false (in this case, isKindOf indeed has that code, so i expect to get true). obviously if i know the class+method, than i can check the code in the Browser, but how can i perform this check at runtime when the class+methods are given as an input? if the class is represented as aClass, the method is represented as aMethod, and the code to check is: 'check if this code exist', than i guess the start would be: checkClass: aClass andMethod: aMethod aClass respondsTo: aMethod ifTrue: [ ^ hmmmmm hasCode: 'check if this code exist' ] ifFalse: [ ^ false ] but what should i actually check in the ifTrue block? i need to be able to check this by code, and not with the refactoring browser. sorry for the (very) long question. thanks, dvir. |
If what you want is to see the source for a methods and compare that
source to something, then
aClass sourceCodeAt: aMethod "aMethod is assumed to be a Symbol here, so aSymbol or aSelector might be a better name" will do what you want. E.g. Array sourceCodeAt: #printOn: will give you: a Text for 'printOn: aStream self shouldBePrintedAsLiteral ifTrue: [^self printAsLiteralFormOn: aStream]. self class = Array ifTrue: [^self printAsBraceFormOn: aStream]. ^super printOn: aStream' Cheers, Bob On 4/28/13 4:55 PM, Dvir Faivel wrote:
|
In reply to this post by Dvir Faivel
On 28-04-2013, at 1:55 PM, Dvir Faivel <[hidden email]> wrote: > hi there, > i have a question about squeak object model. > > how can i check whether a method contains a certain piece of code? You'll get better advice if you expand upon what you want & why; what is the real problem you are trying to solve here? If, for example you're simply wanting to programatically find all senders of a particular message then the code is already there - take a look at SystemNavigation>allCallsOn:fromBehaviors:sorted: to see how to scan all the methods implemented by a list of behaviours (ie classes or metaclasses) for sends of a particular message, including ones that are special cases. If you already know which class and method you want to look at then examine Behavior>whichSelectorsReferTo:specialByte:thorough: to see how the system does it. Or are you wanting to look for particular fragments of code with some sort of text based pattern matching? Or…? tim -- tim Rowledge; [hidden email]; http://www.rowledge.org/tim ASCII to ASCII, DOS to DOS. |
T hanks Bob! the example helped a lot. Tim, i want to change the behavior of some methods (at runtime, based on the user's decision).
for example, if a method of class A contains the comment: "hello, this is a comment" than if B inherits from A, this method wouldn't be inherited (an attempt to call this method from an object of type B would cause an exception to be thrown).
the reason i'm using a comment is that it doesn't affect the code. is there a simple way to add a code to a method?
for example, to add "this is a comment" to the end of the code? On Mon, Apr 29, 2013 at 2:46 AM, tim Rowledge <[hidden email]> wrote:
|
aClass compile: (aClass sourceCodeAt: aMethod)
string,' "** this is a comment**"'
Cheers, Bob On 4/29/13 4:03 PM, Dvir Faivel wrote:
|
Thanks (again)! On Mon, Apr 29, 2013 at 11:15 PM, Bob Arning <[hidden email]> wrote:
|
In reply to this post by Dvir Faivel
On 29-04-2013, at 1:03 PM, Dvir Faivel <[hidden email]> wrote: > Thanks Bob! the example helped a lot. > > Tim, i want to change the behavior of some methods (at runtime, based on the user's decision). > for example, if a method of class A contains the comment: > "hello, this is a comment" > than if B inherits from A, this method wouldn't be inherited (an attempt to call this method from an object of type B would cause an exception to be thrown). > the reason i'm using a comment is that it doesn't affect the code. > > is there a simple way to add a code to a method? > for example, to add "this is a comment" to the end of the code? It sounds to me as if you might be better off using a pragma; not that I'm any sort of expert on how pragmas are usable in current Squeak. We use a variety of pragmas in the VMMaker code to annotate methods for the C code generator; seems to me you are wanting something faintly similar. Perhaps an even simpler approach might be to add a method in your class B with the same selector and implement it as self shouldNotDoThisException raise tim -- tim Rowledge; [hidden email]; http://www.rowledge.org/tim Ornerythologists study bad tempered birds |
Free forum by Nabble | Edit this page |