Q: Is there a "change field type" refactoring?

is there such refactoring? I could not find one...
I thought I've seen a reference to an intention that offers to do this
some time ago, but can't seem to find it and in any case, it is not picked up
by the current settings.

Is there an RFE for one?

Anyone thinks this is useful enough for an RFE? I certainly find that over time
this refactoring gets used a bit (when new superclasses are being discovered as
a result of refactoring).

I could envisage two cases:
1. change to a super class of the current type;
2. change to a different type.

Cheers,

Bonny

14 comments
Comment actions Permalink

There is plugin for it:

'Type Refactoring' Feb 9, 2006 Code Editing

0
Comment actions Permalink

Why is something so basic not standard functionality of IDEA?

S.

0
Comment actions Permalink

The first case is covered by "Use interface where possible" refactoring. The second case was being implemented back in 4.5, but was not finished.

0
Comment actions Permalink

The first case is covered by the "Weaken Variable Type" refactoring in Refactor-J 2.0. That refactoring will also weaken local variables, method parameters, and method return types. Refactor-J is a commercial plugin, and adds twenty refactorings and code assists to IDEA. It's available at http://www.sixthandredriver.com .

Sixth and Red River Software
"Code with Grace and Verve"

0
Comment actions Permalink

Eugene,

The first case would not cover changing type to a super class, rather than an
interface will it?

Are you looking to complete this unfinished refactoring? Perhaps for 6.5?

Cheers,

Bonny

0
Comment actions Permalink

The first case would not cover changing type to a
super class, rather than an
interface will it?

It will


Are you looking to complete this unfinished
refactoring? Perhaps for 6.5?

We currently have no plan to rejuvenate it, but things may change.

0
Comment actions Permalink

I'm pretty sure no other IDE in any other language has implemented anything like this (beyond the IDEA extensions already discussed). Given that, it's hard to see it as "basic".

--Dave Griffith

0
Comment actions Permalink

Dave,
probably Stefan means something more moderate, than the global analysis you and me envision?

0
Comment actions Permalink

Yeah. The most common case where I miss 'Change field type' kind of refactoring is when I am writing a new class and I have just added a String foo; which actually had to be a Long foo; Now I just delete that field and getter/setter pair and start over. But it would be nice to be able to just change the type through a refactoring.

This is really the most basic case. I understand it is much more complicated when the field you change is in use by other classes.

S.

0
Comment actions Permalink

+1

Yeah. The most common case where I miss 'Change field type' kind of
refactoring is when I am writing a new class and I have just added a
String foo; which actually had to be a Long foo; Now I just delete
that field and getter/setter pair and start over. But it would be nice
to be able to just change the type through a refactoring.

This is really the most basic case. I understand it is much more
complicated when the field you change is in use by other classes.

S.



0
Comment actions Permalink

There is plugin for it:

'Type Refactoring' Feb 9, 2006 Code Editing


I wrote this plug-in because from time to time I want to change the type of
a variable within its allowed bounds (super classes/subclasses of the
current type) keeping the functionality exactly as is (just like a
refactoring should do). The existing feature "Refactor|Use Interface Where
Possible" does something (slightly) different:
- it just allows to change the type to super classes/interfaces
- it does not work on one variable, but on all instances of this type.

Unfortunately, this plug-in only covers the local variable and field cases,
not the parameter or method return value cases. Since I have no further time
to add these missing cases, I hope Sixth and Red River will add the missing
features in the next version of Refactor-J.

Tom

0
Comment actions Permalink

I don't think, you are talking about a refactoring, because changing a
variable type from String to Long is not possible without breaking code
(unless you just use methods from Object).

In your situation the existing quick-fix should work.

Tom

0
Comment actions Permalink

In case of breaking code I would actually like something like this:

You tell IDEA to change the type of a field.

IDEA also looks at your code as a whole to see if a simple change is possible.

If a simple change is possible then IDEA does the usual preview. You say Refactor this and you're done. IDEA has changed the field and any setters/getters/constructors that use it.

If a simple change is NOT possible then IDEA will ask you 'This refactoring requires manual steps. Do you want me to refactor as much as possible and give you a TASK LIST of places where this code was broken?'

IDEA then again changes the field/getters/setters/constructors but also shows you a list of places where this code is in use. You can quickly flip through this list and fix the rest of the occurrences.

Manual refactoring is something I don't mind at all. In this case it would make a lot of sense, since the other option is to hit 'Make Project' several times and respond to the compile errors. I think it would be much nicer if IDEA could already present you the locations that need special attention.

S.

0

Please sign in to leave a comment.