Beyond the formatting tools in the previous lab, Eclipse is capable of some automatic refactoring. because this lab will require you to make many changes to the code that you will not want to commit, it is highly recommended you check out your code into a new project before proceeding.
The following steps will lead you through the steps of renaming a class in Eclipse. We will be changing IRCActionHandler, so remember to checkout your code into a new project.
IRCActionHandler, then in the menu bar click
Refactor->Rename.... In the dialog rename it to IRCCommandHandler or whatever
you like (since this won't be committed). Before clicking Next, make sure that Update References, Update similarly named methods, and Update textual occurrences in comments and strings are all selected.
IRCActionHandler has been changed. This works just as well with variables and methods; note also renaming can be done rapidly using Alt+Shift+R. In IRCMessageHandler.java try renaming handle to handleMessage and in IRCConnection.java try renaming _name to _realName.
Eclipse can automatically extract code into a new method or even a whole new class.
IRCMessage.parseParams, use the extract method tool to make the inner statement look something like this:
private static String[] parseParams(String paramString) {
Pattern paramPattern = Pattern.compile("^ (" + Grammar.MIDDLE_RE + ")");
Matcher paramMatcher = paramPattern.matcher(paramString);
Vector paramVector = new Vector();
while (paramVector.size() < 14 && paramMatcher.find()) {
paramVector.add(paramMatcher.group(1));
paramMatcher.region(paramMatcher.end(), paramMatcher.regionEnd());
}
if (!paramMatcher.hitEnd()) {
String rest = paramString.substring(paramMatcher.regionStart());
assert (rest.matches(" :?" + Grammar.TRAILING_RE));
String trailing = newMethod(paramVector, rest);
paramVector.add(trailing);
}
return (String[]) paramVector.toArray(new String[paramVector.size()]);
}
paramVector.add(newMethod(paramVector.size(), rest));
IRCClient, then in the menu bar click Refactor->Extract Superclass.... Name the superclass what you like, and have it implement/contain: the command map, the connection, all the static strings, all relevant getters/setters, setupConnection, connect, and quit. Have it declare abstract: notify, eval, evalcommandcall, and initcommands. Hit next.
IRCClient with ones to your new class.
IRCClient find the eval method. Let us go ahead and place this method up into the superclass. To do this find the Pull Up option under the refactoring menu.
Try some of the other tools in the refactoring menu, such as Extract Interface, Extract Superclass, Pull Up, and Push Down. If you're not sure what a certain refactoring does, try looking it up in Martin Fowler's catalog of refactorings. As you learn about refactorings, consider where you might be able to use these advantageously in your project!