How do I make a duplicate of a class?

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

How do I make a duplicate of a class?

Costas
Hi,

I would like to duplicate a class and subclasses (under another name
of course). What is the best way to do this? I didn't see a cut and
paste so I thought editing chunk files is the only way.

Regards,

Costas


Reply | Threaded
Open this post in threaded view
|

Re: How do I make a duplicate of a class?

Ian Bartholomew-17
Costas,

> I would like to duplicate a class and subclasses (under another name
> of course). What is the best way to do this? I didn't see a cut and
> paste so I thought editing chunk files is the only way.

It's not something you would want to do every day (is it?) so a workspace
script will probably suffice.  The following seems to work but there is one
possible problem (at least) with the replacement of the class/superclass
name strings in the definition.  If the two names have common parts (MyClass
and MyClassExtension for example) the the string replacement part will have
to be refined (or manually rename them afterwards)

topClass := Number.
prefix := 'Duplicate'.
topClass withAllSubclassesDo: [:each |
    className := each name.
    superclassName := each superclass name.
    definition := each definition
        copyReplaceAll: '#' , className
        with:'#' , prefix , className.
    each == topClass
        ifFalse: [
            definition := definition
                copyReplaceAll: superclassName
                with: prefix , superclassName].
    newClass := Compiler evaluate: definition.
    newClass
        comment: each comment;
        categories: each categories.
    each class methodDictionary do: [:eachCM |
        newClass class
            compile: eachCM getSource
            categories: eachCM categories].
    each methodDictionary do: [:eachCM |
        newClass
            compile: eachCM getSource
            categories: eachCM categories]]

Regards
    Ian


Reply | Threaded
Open this post in threaded view
|

Re: How do I make a duplicate of a class?

Ian Bartholomew-17
>                The following seems to work but there is one
> possible problem (at least) with the replacement of the class/superclass
> name strings in the definition.

Doh. Use a Stream and it not only becomes neater but doesn't exhibit the
above problem. I think it was a bit too early on a Monday morning :-)

topClass := Number.
prefix := 'Duplicate'.
topClass withAllSubclassesDo: [:each |
    inStream := each definition readStream.
    outStream := String writeStream.
    each == topClass ifFalse: [outStream nextPutAll: prefix].
    outStream
        nextPutAll: (inStream upTo: $#);
        nextPut: $#;
        nextPutAll: prefix;
        nextPutAll: inStream upToEnd.
    newClass := Compiler evaluate: outStream contents.
    newClass
        comment: each comment;
        categories: each categories.
    each class methodDictionary do: [:eachCM |
        newClass class
            compile: eachCM getSource
            categories: eachCM categories].
    each methodDictionary do: [:eachCM |
        newClass
            compile: eachCM getSource
            categories: eachCM categories]]


Reply | Threaded
Open this post in threaded view
|

Re: How do I make a duplicate of a class?

Chris Uppal-3
In reply to this post by Costas
"Costas wrote:

> I would like to duplicate a class and subclasses (under another name
> of course). What is the best way to do this? I didn't see a cut and
> paste so I thought editing chunk files is the only way.

Ian's talked about how to do it with a script, but if you have only a
handfull of classes to replicate then it's quite easy to do it with cut and
paste.  The key point is that if you select the root of the method category
tree (labelled "all") and drag that to a new class then all the methods will
be copied.  Remember to set up any instvars first.  Also you need to
drag&drop the class-side methods separately from the instance-side.

    -- chris


Reply | Threaded
Open this post in threaded view
|

Re: How do I make a duplicate of a class?

James Foster-3
In reply to this post by Costas
As you suggest, you can edit the chunks: file out the classes, edit the
file-out, and then file it back in. Another approach is to file out the
classes, then rename the originals in the image, then file in the prior
ones. That might be slightly less work.

James Foster
Fargo, ND


Reply | Threaded
Open this post in threaded view
|

Re: How do I make a duplicate of a class?

Costas Menico
In reply to this post by Ian Bartholomew-17
Ian,

Quite impressive code. Now, I can see though why there would be
problems (or not even make sense)  having such a feature built in to
Dolphin. I was just trying to copy a class and make changes to it. I
guess it would be useful if there was a namespace facility. And anyway
the simple drag and drop based on Chris' suggestion will do for now.

Thanks

On Mon, 29 Jul 2002 08:49:30 +0100, "Ian Bartholomew"
<[hidden email]> wrote:

>>                The following seems to work but there is one
>> possible problem (at least) with the replacement of the class/superclass
>> name strings in the definition.
>
>Doh. Use a Stream and it not only becomes neater but doesn't exhibit the
>above problem. I think it was a bit too early on a Monday morning :-)
>
>topClass := Number.
>prefix := 'Duplicate'.
>topClass withAllSubclassesDo: [:each |
>    inStream := each definition readStream.
>    outStream := String writeStream.
>    each == topClass ifFalse: [outStream nextPutAll: prefix].
>    outStream
>        nextPutAll: (inStream upTo: $#);
>        nextPut: $#;
>        nextPutAll: prefix;
>        nextPutAll: inStream upToEnd.
>    newClass := Compiler evaluate: outStream contents.
>    newClass
>        comment: each comment;
>        categories: each categories.
>    each class methodDictionary do: [:eachCM |
>        newClass class
>            compile: eachCM getSource
>            categories: eachCM categories].
>    each methodDictionary do: [:eachCM |
>        newClass
>            compile: eachCM getSource
>            categories: eachCM categories]]
>
>