Refactor: how do i change all usages to another class

Is there already a feature in IDEA to replace all usages of a specific class with another class? (I don't mean a Rename)

For example, if I have two classes, one called Apple and on called Orange, and I want to replace all usages of Apple with Orange (and update imports), how would I accomplish this? Both classes already exist, so I can't use a Rename or Move or Copy, etc... I can't just do a Find & Replace, because imports would break, and that would find the words "Apple" in places I didn't want to change.

It's almost like I'm looking for a Replace refactoring....

7 comments
Comment actions Permalink

Ian Zabel wrote:

Is there already a feature in IDEA to replace all usages of a specific
class with another class? (I don't mean a Rename)


Tools -> Migrate? I haven't tried it myself but I think this is what
it's supposed to do.

0
Comment actions Permalink

You can do it in three steps:

1. Remove the Orange class from the project structure (be sure you keep a
copy, as you'll need it for step 3)
2. Use the extract superclass refactoring on Apple (and extract everything
the Apple has). Call the superclass Orange and place it where the old Orange
was. IDEA will ask you if you want to change usages of Apple with Orange
where possible, be sure to say yes.
3. Overwrite the new Orange class with the old one.

This presumes that the "refactoring" does not cause any problems in your
code, of course. If the original Orange can not substitute Apple then you
will have to update the code.

HTH,
Andrei

"Ian Zabel" <ian@ezabel.com> wrote in message
news:1439407.1037373964325.JavaMail.jrun@is.intellij.net...

Is there already a feature in IDEA to replace all usages of a specific

class with another class? (I don't mean a Rename)
>

For example, if I have two classes, one called Apple and on called Orange,

and I want to replace all usages of Apple with Orange (and update imports),
how would I accomplish this? Both classes already exist, so I can't use a
Rename or Move or Copy, etc... I can't just do a Find & Replace, because
imports would break, and that would find the words "Apple" in places I
didn't want to change.
>

It's almost like I'm looking for a Replace refactoring....



0
Comment actions Permalink

1. Copy Orange to OrangeX
2. Unsafe delete Orange
3. Rename Apple to Orange and back to Apple (This will handle the imports)
4. Rename OrangeX to Orange

"Ian Zabel" <ian@ezabel.com> wrote in message
news:1439407.1037373964325.JavaMail.jrun@is.intellij.net...

Is there already a feature in IDEA to replace all usages of a specific

class with another class? (I don't mean a Rename)
>

For example, if I have two classes, one called Apple and on called Orange,

and I want to replace all usages of Apple with Orange (and update imports),
how would I accomplish this? Both classes already exist, so I can't use a
Rename or Move or Copy, etc... I can't just do a Find & Replace, because
imports would break, and that would find the words "Apple" in places I
didn't want to change.
>

It's almost like I'm looking for a Replace refactoring....



0
Comment actions Permalink

I have a feature request that I created for this a while back:

http://www.intellij.net/tracker/idea/viewSCR?publicId=4059

Ian Zabel wrote:

Is there already a feature in IDEA to replace all usages of a specific class with another class? (I don't mean a Rename)

For example, if I have two classes, one called Apple and on called Orange, and I want to replace all usages of Apple with Orange (and update imports), how would I accomplish this? Both classes already exist, so I can't use a Rename or Move or Copy, etc... I can't just do a Find & Replace, because imports would break, and that would find the words "Apple" in places I didn't want to change.

It's almost like I'm looking for a Replace refactoring....


--
Gordon Tyler
Software Developer, R&D
Sitraka (now part of Quest Software)
"Performance is Mission Critical"

0
Comment actions Permalink

I don't think the step 3 will work since after renaming back to Apple all
classes will start using Apple and nobody will use Orange

--

Best regards,
Eugene Zhuravlev
JetBrains, Inc, http://www.intellij.com
"Develop with pleasure!"


"Simon Jarvis" <simonj@westworld.demon.co.uk> wrote in message
news:ar3arl$tlm$1@is.intellij.net...

1. Copy Orange to OrangeX
2. Unsafe delete Orange
3. Rename Apple to Orange and back to Apple (This will handle the imports)
4. Rename OrangeX to Orange

>

"Ian Zabel" <ian@ezabel.com> wrote in message
news:1439407.1037373964325.JavaMail.jrun@is.intellij.net...

Is there already a feature in IDEA to replace all usages of a specific

class with another class? (I don't mean a Rename)
>

For example, if I have two classes, one called Apple and on called

Orange,

and I want to replace all usages of Apple with Orange (and update

imports),

how would I accomplish this? Both classes already exist, so I can't use a
Rename or Move or Copy, etc... I can't just do a Find & Replace, because
imports would break, and that would find the words "Apple" in places I
didn't want to change.
>

It's almost like I'm looking for a Replace refactoring....

>
>


0
Comment actions Permalink

ooooh I want that! That would be very nice.

0
Comment actions Permalink

You are correct. I misread the original question. My example will replace
Orange with Apple.

"Eugene Zhuravlev" <jeka@intellij.com> wrote in message
news:ar3eac$4m4$1@is.intellij.net...

I don't think the step 3 will work since after renaming back to Apple all
classes will start using Apple and nobody will use Orange

>

--

>

Best regards,
Eugene Zhuravlev
JetBrains, Inc, http://www.intellij.com
"Develop with pleasure!"

>
>

"Simon Jarvis" <simonj@westworld.demon.co.uk> wrote in message
news:ar3arl$tlm$1@is.intellij.net...

1. Copy Orange to OrangeX
2. Unsafe delete Orange
3. Rename Apple to Orange and back to Apple (This will handle the

imports)

4. Rename OrangeX to Orange

>

"Ian Zabel" <ian@ezabel.com> wrote in message
news:1439407.1037373964325.JavaMail.jrun@is.intellij.net...

Is there already a feature in IDEA to replace all usages of a specific

class with another class? (I don't mean a Rename)
>

For example, if I have two classes, one called Apple and on called

Orange,

and I want to replace all usages of Apple with Orange (and update

imports),

how would I accomplish this? Both classes already exist, so I can't use

a

Rename or Move or Copy, etc... I can't just do a Find & Replace, because
imports would break, and that would find the words "Apple" in places I
didn't want to change.
>

It's almost like I'm looking for a Replace refactoring....

>
>

>
>


0

Please sign in to leave a comment.