"Moving" a method programmatically

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

"Moving" a method programmatically

Günther Schmidt
Hi,

how do I move a method (from the instance side) to the class side
programmatically.

I know that the RB does this somehow, I just haven't found it yet.

Thanks


Günther


Reply | Threaded
Open this post in threaded view
|

Re: "Moving" a method programmatically

Schwab,Wilhelm K
Günther,

> how do I move a method (from the instance side) to the class side
> programmatically.
>
> I know that the RB does this somehow, I just haven't found it yet.

The way I would approach it would be to get the source for the method
and then compile it as a class method.

Send #class to the class (it might help to ask OA for "The Strap"<g>)
and then send #compile:categories: to the result; the first argument is
the code.  Of course, it might not compile due to missing instance
variables.  My CodeGenerator goodie puts a wrapper around this kind of
stuff.  For just the problem you described, it would be overkill - with
the the possible exception of the preview framework.

Have a good one,

Bill

--
Wilhelm K. Schwab, Ph.D.
[hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: "Moving" a method

KlausK-2
In reply to this post by Günther Schmidt
Hi Günther,

try to drag the method with the right mousebutton and drop it onto of the
"class" radiobutton. Dolphin is so clever and asks you "move or copy"...

Cheers
Klaus


Reply | Threaded
Open this post in threaded view
|

Re: "Moving" a method

KlausK-2
In reply to this post by Günther Schmidt
Hi Günther,

try to drag the method with the right mousebutton and drop it over the
"class" radiobutton.
Dolphin is so clever and asks you for copy or move!

Cheers
Klaus


Reply | Threaded
Open this post in threaded view
|

Re: "Moving" a method programmatically

Chris Uppal-3
In reply to this post by Schwab,Wilhelm K
Bill, Günther,

> and then send #compile:categories: to the [class]

Better to use #compile:categories:package: or else you risk "forgetting" that a
method is loose.

    -- chris


Reply | Threaded
Open this post in threaded view
|

Re: "Moving" a method programmatically

Chris Uppal-3
> Bill, Günther,

> Better to use #compile:categories:package: or else you risk "forgetting"
> that a method is loose.

I remembered that this involves dredging up a fair bit of information from
various places, so here it is in full (tested only briefly).

    class := AClass.
    method := class compiledMethodAt: #aSelector.
    new := (class class)
                    compile: method getSource
                    categories: method categories
                    package: method owningPackage.
    new isNil ifFalse: [class removeSelector: method selector].

The isNil test is to avoid removing the method if it wouldn't compile on the
class side.

There may be a different ways of doing this which create an "undo" record for
the RB and/or forces the method to the class-side even if it won't compile
(with a dummy did-not-compile implementation as bytecode), but I don't know how
to do either of those things.

    -- chris