More refactorings

Some more refactorings would be nice:

In a single step it lets you turn naughtily exposed public variables
(and even default static finals) into private ones with optional
getters/setters.  But the tricky part is it converts all references
outside the class to use the getters/setters. Handles both static and
instance variables.

Convert a class with static methods to make some of them instance
methods.  It then converts all calls to those methods outside the
class to instance calls using a dummy variable named the same as the
class but starting with a lower case letter. This in general will not
compile, but it will get you most of the way there accurately.

I suppose, while you are at it, you should be able to convert instance
methods back to static and tidy up the calls to use Class name rather
in than object  reference.
--
Roedy Green Canadian Mind Products
http://mindprod.com
If we have learned one thing from the history of invention and discovery, it is that, in the long run � and often in the short one � the most daring prophecies seem laughably conservative.
~ Arthur C. Clarke (born: 1917-12-16 died: 2008-03-19 at age: 90)

2 comments
Comment actions Permalink

On 1/23/2010 10:55 AM, Roedy Green wrote:

Some more refactorings would be nice:

>

In a single step it lets you turn naughtily exposed public variables
(and even default static finals) into private ones with optional
getters/setters.  But the tricky part is it converts all references
outside the class to use the getters/setters. Handles both static and
instance variables.

This refactoring exists already, it's called "encapsulate fields".
>

Convert a class with static methods to make some of them instance
methods.  It then converts all calls to those methods outside the
class to instance calls using a dummy variable named the same as the
class but starting with a lower case letter. This in general will not
compile, but it will get you most of the way there accurately.

When I do this refactoring manually, I have a process which works fairly
well, and actually keeps the code compilable throughout...

For every method you want to refactor:

1. add a single statement "new MyClass()",
2. use Introduce Parameter on that statement.
3. Use "convert static method to instance".

After that, you can find usages of your constructor, and replace them
with fields or parameters as appropriate.

>

I suppose, while you are at it, you should be able to convert instance
methods back to static and tidy up the calls to use Class name rather
in than object  reference.

This is also easy as a multi-step refactoring, assuming no instance
member is accessed.

    1. select entire contents of method,
    2. use "Introduce Method". Select "static".
    3. use "inline method" on the original method.

If "static" is grayed out, the method uses some instance member.  You
can try to fix that and then attempt again.



0
Comment actions Permalink

Hello Roedy,

Some more refactorings would be nice:

In a single step it lets you turn naughtily exposed public variables
(and even default static finals) into private ones with optional
getters/setters.  But the tricky part is it converts all references
outside the class to use the getters/setters. Handles both static and
instance variables.


As already answered, this refactoring is called "Encapsulate Fields". It
exists since IDEA 3, or maybe even earlier.

Convert a class with static methods to make some of them instance
methods.  It then converts all calls to those methods outside the
class to instance calls using a dummy variable named the same as the
class but starting with a lower case letter. This in general will not
compile, but it will get you most of the way there accurately.


As a rule, we try to create refactorings that produce code that compiles.

I suppose, while you are at it, you should be able to convert instance
methods back to static and tidy up the calls to use Class name rather
in than object  reference.


This is called "Make Static", and also exists for quite a long time.

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0

Please sign in to leave a comment.