Beyond the formatting tools we showed you last week, Eclipse is capable of performing refactoring automatically. In this lab we'll explore several refactoring features. Because this lab will require you to make many changes to the code that you won't want to commit, it is highly recommended that you CHECKOUT YOUR CODE INTO A NEW PROJECT.
The following steps will lead you through the steps of
renaming a class in Eclipse. We will be changing
IRCMessageListener,
so remember to checkout your code into a new project.
IRCMessageListener, then in
the menu bar click
Refactor->Rename.... In the dialog rename it to whatever
you like
(we wont be committing this). Before clicking next, make sure that Update
References,
Update similarly named methods, and Update textual occurrences in comments
and strings
are all selected.
IRCMessageListener has been
changed to the
name you gave it. This works just as well with variables and methods.
Rename window in IRCClient, and notify in
IRCMessageListener.
Eclipse can automatically refactor blocks of code into methods and
extract whole new classes. These steps will guide
you through editing IRCMessage and creating a new abstract
class that IRCClient implements.
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.