using reflections in squeak

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

using reflections in squeak

Dvir Faivel
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.


Reply | Threaded
Open this post in threaded view
|

Re: using reflections in squeak

Bob Arning-2
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.



    



Reply | Threaded
Open this post in threaded view
|

Re: using reflections in squeak

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



Reply | Threaded
Open this post in threaded view
|

Re: using reflections in squeak

Dvir Faivel
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:

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.






************************************************************************************
This footnote confirms that this email message has been scanned by
PineApp Mail-SeCure for the presence of malicious code, vandals & computer viruses.
************************************************************************************






Reply | Threaded
Open this post in threaded view
|

Re: using reflections in squeak

Bob Arning-2
aClass compile: (aClass sourceCodeAt: aMethod) string,' "** this is a comment**"'

Cheers,
Bob

On 4/29/13 4:03 PM, Dvir Faivel wrote:
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:

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.






************************************************************************************
This footnote confirms that this email message has been scanned by
PineApp Mail-SeCure for the presence of malicious code, vandals & computer viruses.
************************************************************************************







    



Reply | Threaded
Open this post in threaded view
|

Re: using reflections in squeak

Dvir Faivel
Thanks (again)!


On Mon, Apr 29, 2013 at 11:15 PM, Bob Arning <[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:
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, <a href="tel:2013" value="+9722013" target="_blank">2013 at 2:46 AM, tim Rowledge <[hidden email]> wrote:

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.






************************************************************************************
This footnote confirms that this email message has been scanned by
PineApp Mail-SeCure for the presence of malicious code, vandals & computer viruses.
************************************************************************************







    





************************************************************************************
This footnote confirms that this email message has been scanned by
PineApp Mail-SeCure for the presence of malicious code, vandals & computer viruses.
************************************************************************************






************************************************************************************
This footnote confirms that this email message has been scanned by
PineApp Mail-SeCure for the presence of malicious code, vandals & computer viruses.
************************************************************************************







Reply | Threaded
Open this post in threaded view
|

Re: using reflections in squeak

timrowledge
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