CSE 70, Spring 2010

Software Engineering

Lab Instructions


Refactoring in Eclipse

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.

Renaming

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.

  1. In the package explorer select 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.
  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 every reference to 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.

Extracting

Eclipse can automatically extract code into a new method or even a whole new class.

  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.
  9. */?>

Exploring further

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!


Valid XHTML 1.0 Strict Valid CSS!