CSE 70, Fall 2008

Software Engineering

Lab 10 Instructions


Code generation in Eclipse

In addition to handy features like automatic code formatting, Eclipse can generate code for you. In this lab we'll explore how to configure and use its code-generation features.

Create a new Class.

The following steps will lead you through the steps of automatically generating a class in your ircclient package that implements the interface IRCMessageListener.

  1. In the Package Explorer, expand src and select the edu.ucsd.cse70.ircclient package.
  2. From the menu bar, select File->New->Class. In the "New Java Class" dialog, make sure the package is set to edu.ucsd.cse70.ircclient, and give the class any name you like. (You won't commit it to the repository.) Don't close the dialog just yet!
  3. In the "New Java Class" dialog, click the Add... button, and in the text field, begin typing the name of the interface you're implemeting (see above). Then select it and press "OK".
  4. In the "New Java Class" dialog again, press "Finish". Notice Eclipse adds methods from the interface, but without implementation.

Add some functionality to the class.

  1. Create 3 variables: message, connection, and user. Give each variable a type that seems appropriate. In keeping with good encapsulation, make them private. So that other classes can read and modify the values of these variables, we'll generate accessor methods.
  2. From the menu bar, select Source->Generate Getters and Setters..... In the resulting dialog, check all 3 variables and check the "Generate method comments" option before pressing "OK".
  3. Suppose we want a constructor that takes values for these variables. See if you can have Eclipse generate this for you.

Customize template settings.

Notice the TODO comment in the notify method? Find it painfully obvious? Let's tell Eclipse to stop generating those.

  1. From the menu bar, select Window->Preferences->Java->Code Style->Code Templates.
  2. There are 2 categories of templates: comments and code. Each template has a pattern (although some are empty) for what to generate. See if you can find the pattern that generated the TODO comment and remove it. Then try generating another class (hint: or at least another method) to verify it works.
  3. Now setup your templates so that when you create a new Java class, the comments at the top look like:
    package edu.ucsd.cse70.ircclient;
    
    /**
     * @author Tess
     * @version .1 10/20/2008
     */
    public class MYCLASS implements IRCMessageListener
    
  4. Try out different settings. See what you and your teammates like!

Valid XHTML 1.0 Strict Valid CSS!