CSE 70, Fall 2008

Software Engineering

Lab 11 Instructions


Refactoring in Eclipse

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.

Renaming.

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.

  1. In the package explorer select 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.
  2. Eclipse will present you with a preview dialog, which will show you the changes to all the classes affected by this refactoring. Review them all and click finish.
  3. Notice that every reference to 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.

Extracting.

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.

  1. In 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()]);
    }
     
  2. Eclipse automatically generated a method that only ever uses one of the parameters to get its size. Use the refactoring tools to change the methods signature so the code can look more like:
    paramVector.add(newMethod(paramVector.size(), rest));
    
  3. In the package explorer select 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.
  4. Eclipse will present you with a review dialog containing all the methods of the new class, look over these and click next.
  5. Eclipse will present you with a preview dialog, which will show you the changes to all the classes affected by this refactoring. Review them all and click finish.
  6. Notice that Eclipse has generated a brand new class and replaced all references to the static variables in IRCClient with ones to your new class.
  7. In the 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.
  8. Tryout some of the other tools in the refactoring menu, make sure not to commit anything unless you are sure that it is necessary and that it will not break your code. If you are planning on committing, it is a very good idea to discuss your plan with the entire team.

Valid XHTML 1.0 Strict Valid CSS!