[squeak-dev] using reflections in squeak

Bob Arning arning315 at comcast.net
Sun Apr 28 22:57:05 UTC 2013


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:
> 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.
>
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20130428/b3431a13/attachment.htm


More information about the Squeak-dev mailing list