开发者

How to refactor thousands of lines of Java code? Is there any tool available?

In our application we have two or three classes which contains the entire Java Swing application logic. These two or th开发者_如何学JAVAree classes contain around 7k lines of code.

Now I have been assigned the task to refactor this Java code.

How do I start? Is there any tool available that will do the refactoring or at least guide us?


I'd recommend Eclipse - the brilliant Java IDE for the editing and refactoring. It has several tools for refactoring. An excellent tutorial on how to do it with Eclipse is located at:

http://www.cs.umanitoba.ca/~eclipse/13-Refactoring.pdf

There's a brililant article on the power of refactoring with Eclipse, if you're not yet convinced, at:

http://www.eclipse.org/articles/article.php?file=Article-Unleashing-the-Power-of-Refactoring/index.html

And finally another article on how to refactor in Eclipse, including techniques and tools, is available at:

http://www.ibm.com/developerworks/opensource/library/os-ecref/

There's also another stackoverflow question on strategies for refactoring Java code that you may be interested in:

https://stackoverflow.com/questions/128498/what-are-the-best-code-refactoring-strategies

Hope that helps, good luck!


I assume that you are trying to break up these large classes into smaller ones. The most common way to do this is with the Extract Class refactoring. It just happens that this is a major topic in my PhD thesis work.

One of the hard parts is deciding what goes into the new classes. There are two publicly available tools that I know of that help - ExtC (my tool) and JDeodorant. Both are Eclipse plug-ins, and I would classify both as being prototypes. If you want to try to use my tool, I'll be glad to help.

Once you decide what should go into the new class, you have to do the actual work of separating the class into others. Eclipse's Extract Class refactoring is misnamed and isn't really helpful. IntelliJ's IDEA is much better, but still has some bugs. JDeodorant can also perform the split, but it also has some bugs.


IntelliJ has all the smarts for understanding Java code and provides excellent refactorings. And now there is a free and open source version.


Eclipse has some built-in refactoring tools. You could refactor method's signatures, extract interfaces and classes, pull methods up and down in the hierarchy tree, move packages ... and all that just by two clicks.

Also, you could start with a Martin Fowler book "Refactoring: Improving the Design of Existing Code".


As refactoring code relies primarily on the developer (assisted by tooling), your IDE is a very important tool when it comes to refactoring.

Both Eclipse and IntelliJ IDEA have plenty of refactoring support.

For an overview, checkout:

  • http://www.jetbrains.com/idea/features/refactoring.html
  • http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.jdt.doc.user/concepts/concept-refactoring.htm


I have created my own refactoring tool that tries and group together methods that use the same set of variables. It is very much an early prototype. It is only available as a Windows Eclipse plugin.

Variable Usage Eclipse Plugin

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜