Stupid refactoring question

I have a class like this:

public class VariableManager extends HashMap {
//empty
}

What's the easiest way to replace all usages of VariableManager with
HashMap ?

I used search & replace, but i think there might be a refactoring to do
this.

/Kreiger



Attachment(s):
signature.asc
8 comments
Comment actions Permalink

I have read in the Irida new features that this is handled by the safe
delete refatoring, but haven't tried it.
So just try a safe delete on VariableManager (using a current Irida build).

Christoffer "Kreiger" Hammarström schrieb:

I have a class like this:

public class VariableManager extends HashMap {
//empty
}

What's the easiest way to replace all usages of VariableManager with
HashMap ?

I used search & replace, but i think there might be a refactoring to do
this.

/Kreiger

0
Comment actions Permalink

Christoffer "Kreiger" Hammarström wrote:

What's the easiest way to replace all usages of VariableManager with
HashMap ?


"Safe Delete" doesn't work here, I think. And I don't know if it's the
easiest way, but here's how I would do it with refactorings:

On VariableManager:
1. "Replace Constructor with Factory Method..."

2. "Use interface where possible..." to change all declarations to HashMap

3. Change the generated factory method to: return new HashMap()

4. "Inline..." on the factory method.

5. "Safe Delete..." VariableManager.

Search and replace sounds easier actually. Anybody know a better way?

Bas

0
Comment actions Permalink

And even that won't get all of them, if you have casts/instanceof/class-literal expressions that use VariableManager. I think search-and-replace might actually be the way to go.

Reminds me, though, that I need an inspection for classes that extend concrete Collection or Map classes. Replace inheritance with delegation as a quickfix.


--Dave Griffith

0
Comment actions Permalink

Bas, you are right.
I was talking about
IDEADEV-159: "Safe Delete should be able to remove class from class hierarchy"
From the description it reads like it should do exactly what Christoffer wanted:
http://www.jetbrains.net/jira/browse/IDEADEV-159

But it only works if there are no other references to VariableManager than
"extends VariableManager" in which case it changes it to "extends HashMap".

I think it should at least handle default constructor usages, too.
If that were implemented "Use interface where possible" and "Safe Delete..."
would do the job without the need for "Replace Constructor" plus later "Inline".

Bas Leijdekkers schrieb:

Christoffer "Kreiger" Hammarström wrote:

>> What's the easiest way to replace all usages of VariableManager with
>> HashMap ?


"Safe Delete" doesn't work here, I think. And I don't know if it's the
easiest way, but here's how I would do it with refactorings:

On VariableManager:
1. "Replace Constructor with Factory Method..."

2. "Use interface where possible..." to change all declarations to HashMap

3. Change the generated factory method to: return new HashMap()

4. "Inline..." on the factory method.

5. "Safe Delete..." VariableManager.

Search and replace sounds easier actually. Anybody know a better way?

Bas

0
Comment actions Permalink

Bas Leijdekkers wrote:

>

Search and replace sounds easier actually. Anybody know a better way?

>

As you said: brute force (Search and Replace)

1/ Rename "VariableManager" to "Foo01234"
2/ Replace All in path "Foo01234" by "HashMap"
3/ Go back to HashMap.java, and change it back to "Foo01234"
4/ safe delete Foo01234

It works, but it's ugly. I'll go and wash my hands.

Alain

0
Comment actions Permalink

Alain Ravet wrote:

Bas Leijdekkers wrote:

>>
>> Search and replace sounds easier actually. Anybody know a better way?
>>


As you said: brute force (Search and Replace)

1/ Rename "VariableManager" to "Foo01234"
2/ Replace All in path "Foo01234" by "HashMap"
3/ Go back to HashMap.java, and change it back to "Foo01234"
4/ safe delete Foo01234

Hey! That ruined my Foo0123456789 class! ;)

/Kreiger





Attachment(s):
signature.asc
0
Comment actions Permalink

Bas Leijdekkers wrote:

Christoffer "Kreiger" Hammarström wrote:

>> What's the easiest way to replace all usages of VariableManager with
>> HashMap ?


"Safe Delete" doesn't work here, I think. And I don't know if it's the
easiest way, but here's how I would do it with refactorings:

On VariableManager:
1. "Replace Constructor with Factory Method..."

2. "Use interface where possible..." to change all declarations to HashMap

3. Change the generated factory method to: return new HashMap()

4. "Inline..." on the factory method.

5. "Safe Delete..." VariableManager.

Search and replace sounds easier actually. Anybody know a better way?

Bas


I actually used "Use interface where possible" to change declarations to
use java.util.Map, but that was only half of it.

Maybe there should be a "Use superclass where possible" refactoring? :)

/Kreiger




Attachment(s):
signature.asc
0
Comment actions Permalink

I think Migrate is very suitable for this case. Click "Tools -> Migrate...",and make VariableManager migrate to java.util.HashMap,and it is very easy.

0

Please sign in to leave a comment.