Advanced Software Development Homework 1

Fall 2003 CSG 260

(you can download the text version ( here by right clicking)

Whenever you have questions about course material, please send e-mail to:

The teaching assistant is to be determined.

Due date:

THEME: Reverse Engineering of Java programs using AspectJ ( and $ASD/CCS_AspectJ_Usage.html)
Learn basics of XML by studying Demeter example: Data type definitions (DTDs), documents and Java code generation from XML DTDs.

On CCS Northeastern machines,

On the WWW,
DemJ =
DJ =
D =

The directory containing administrative information and homeworks is: $ASD (using the UNIX shell syntax).

Use the following header for your homework submissions and put it at the top:

Course number: CSG 260
Account name:
Assignment number:

Do NOT include your student id number in any work you submit because some of your work may be put on the web.

Read chapters 1, 3 and 4 of the AP book.
Read: chapter 1: A Quick Tour of Java in the Java book by Arnold/Gosling or
the approximately equivalent information in your favorite Java book.
You may want to buy and read one of the AspectJ books that are available. See

Skim read the following:

About UML class diagrams at
About XML:
(XML = eXtensible Markup Language is a W3C Recommendation,i.e., it is a standard.)

Read about the Law of Demeter:
You can play with yourself.
You can play with your own toys (but you can't take them apart),
You can play with toys that were given to you.
And you can play with toys you've made yourself.
For more information:

If you detect some incorrect statements in those pages, please drop me a note.

Read about basic pointcuts and intertype declarations in your AspectJ document (either a book or the AspectJ website).


This homework is about reverse engineering of Java programs using UML.
Reverse engineering means to figure out the design from the program text. Reverse Engineering: "the process of analyzing a subject system to identify the system's components and their interrelationships and create representations of the system in another form or at a higher level of abstraction." (Source: Chikofsky and Cross)

Consider the Java program in directory

The files in directory gen with suffix *.java make up the program.

(Note: The files in gen have been generated by DemeterJ from the files and tree.beh. While you study the Java programs, you also learn how DemeterJ generates code. The file is very similar to an XML schema or an XML document type definition (DTD). The code generation process is similar to the code generation process described in JSR 31 by SUN. This Demeter-like code generation capability is a part of the Java 2 Platform (Web Services Pack)
The file tree.input that will be used later to create a Java object is similar to an XML document. So we have the following correspondences:

XML Document type definition or schema :: (class dictionary)
XML document :: tree.input (sentence)
Java program processing XML document :: tree.beh (behavior file)

The following file gives you information on the code generation process:

Start your reverse engineering effort with file
Most of the organization of this file is described in file

Answer the following questions:

1. Consider the UML class diagram underlying the Java program.
(For information on UML, see

Turn in a picture of the UML class diagram. Use your favorite drawing tool to create it and turn in the file that your drawing tool (maybe a pen) produces.

Your UML class diagram will contain directed associations.A directed association looks like:

--------                   --------
|  A   |                1  |  B   |
|      | ----------------->|      |
--------                x  --------

A is the source class, B the target class and x is the name of the role.
In the Java implementation, A and B are classes and A has an instance variable x of class B.

Association is a synonym for relation. The directed association above presents a binary relation. You think of it as a set of pairs, namely (A-object,B-object) pairs. A natural way to implement such a relation is to store the B-object which is
associated with a given A-object in the instance variable x of the A-object.

The name "association" comes from the OMT method. It has found its way into the UML vocabulary.

Do not put methods and their signatures into your UML class diagram.

2. Describe the behavior of the program in English.
Try to summarize the program as much as possible. Do not just translate the program into English especially when you explain the methods in the Java program. Focus on explaining the methods
and the methods they call.

Here is a start: When
cd gen
cd classes
java Main < ../../tree.input
cd ../..

runs, the Java virtual machine executes the byte code in file Main.class. This code will execute function Main.main which creates a tree object from file tree.input in variable tree using a table-driven parser whose functioning will not be explained here; the parser is automatically generated by a parser generator (it is free):
In the following we only explain the behavior of sum_labels(). The behavior is implemented through a traversal which visits all Label-objects and a SummingVisitor ...

The output produced by the program is in file out.


Now reverse-engineer the program in
using AspectJ instead of UML. The goal is to have a running AspectJ program that untangles code that is given to you. Turn in your AspectJ program. There are several ways to write the AspectJ program. Turn in a paragraph about the design of your AspectJ program. The AspectJ program combined with the base program must have exactly the same behavior as the enhanced program.

Consider directory $ASD/hw/1/graph/base/gen and directory $ASD/hw/1/graph/enhanced/gen. Directory base/gen contains a basic program created by DemeterJ and directory enhanced/gen contains an enhanced program, also generated by DemeterJ, obtained by modifying the basic program. In this exercise we want to express the modification of the basic program in AspectJ without ever touching the basic program. We want to change the basic program by ADDITION of *.java files and not by MOFIFYING existing *.java files. I have started this process of enhancement in base/gen. To compile and run the program I use file compile which contains

ajc *.java
java Main < ../class-graph.input
Note that I use the AspectJ compiler to compile the Java programs and the reason is that there is one file that is not a plain Java program: contains the text:
aspect behavior2 {
   int Cd_graph.countInhRels2() {
     CountingVisitor v0 = new CountingVisitor();
     // allInh(v0);
     // return v0.get_return_val();
     return 0;

   pointcut MainCall() : call (static public void Main.main(..));
   after() : MainCall() {
    System.out.println("result = " +
You can view this aspect as editing instructions which add a little bit of the code in enhanced/gen (but not in /base/gen) to base/gen. Your task is to use AspectJ to add all the code in enhanced/gen.


You can answer the questions by studying the files of the form
*.cd and *.beh. In addition, you should study the generated Java code (in the gen directory) although it might be confusing. Also keep in mind that there are several ways to generate the code and you see here only one way. For example in the JAXB approach by SUN mentioned earlier there is a separate file that allows you to control the details of code generation. DemeterJ uses a fixed method.

You are asked to wade through a tremendous amount of Java code:
  for binary-tree: a few thousand lines of Java code generated from 67 lines in tree.beh and

When you study the Java code keep in mind that it must be very regular since it is produced from a small amount of information. Also keep in mind that not all generated code is actually used by the simple application programs. You have to focus only on the code which is used.


Take a look at:

and get a basic understanding of the package.

Find the unknowns in the following Java program: The program prints the city where an employee lives. You should find the UNKNOWNs by using knowledge from your Java book and by what is in the DJ package documentation. Only after you have found all the UNKNOWNs you may want to run the Java program.

class Main {
  static public void main(String args[]) throws Exception {

    // Build object
    Employee e = new Employee(
      new UNKNOWN1(
        new UNKNOWN2(
          new UNKNOWN3(
            new String("Boston")))),
      new UNKNOWN4(),
      new Date());

    ClassGraph cg = buildClassGraph();
    System.out.println("City where Employee lives");
    System.out.println("City where Employee lives: UNKNOWN5 ");


  public static ClassGraph UNKNOWN9() {
   ClassGraph cg=new  ClassGraph(true,false);
   // true: include all fields 
   // false: do NOT include all non-void no-argument methods
  System.out.println("The UNKNOWN10 version is: " + cg.getVersion());
  System.out.println("UNKNOWN11" + "=============================");
  System.out.println("end class graph " + "=============================");
  return cg;


class UNKNOWN12 {
  UNKNOWN13(PersonalInfo personalInfo_, Date d, Date a) {
    personalInfo = UNKNOWN14;
    departure = UNKNOWN15;
    arrival = UNKNOWN16;
  PersonalInfo personalInfo;
  Date arrival;
  Date departure;
  String get_city(ClassGraph cg) {
    return (String) cg.UNKNOWN17(this,
    "from Employee through City to java.lang.String");

class PersonalInfo {
  Address address; 
  PersonalInfo(Address address_) {
    address = address_;

class Address {
  Address(City city_) {
    city = city_;
  City city;

class City {
  City(String name_) {
    name = name_;
  String name;

class Date {

The DJ version is: DJ version 0.8.2
The class graph is=============================
Address = <city> City extends java.lang.Object.
City = <name> java.lang.String extends java.lang.Object.
java.lang.Object : City | Address | java.lang.String | Date | PersonalInfo | Employee | Main common .
java.lang.String = extends java.lang.Object implements, java.lang.Comparable. : java.lang.String common .
java.lang.Comparable : java.lang.String common .
Date = extends java.lang.Object.
Employee = <personalInfo> PersonalInfo <arrival> Date <departure> Date extends java.lang.Object.
PersonalInfo = <address> Address extends java.lang.Object.
Main = extends java.lang.Object.
java.util.Collection = <elements> java.lang.Object. = <elements> java.lang.Object.
end class graph =============================
City where Employee lives
City where Employee lives: bad way

To provide the answers,
use the file $ASD/hw/1/UNKNOWNs

An UNKNOWN may be multiple words.


Compile and run the Java programs in binary-tree and graph on your PC or workstation at work. Just compile the files in the gen directory. Don't regenerate them using DemeterJ.

Please put the rt.jar file into your class path. See:

If you use the CCS .software file, use: CLASSPATH=.:/proj/demsys/demjava/rt.jar

Turn in the output produced produced by running the binary tree program and the output of the enhanced graph program you compiled.
Write a grammar for atoms:

using the grammar notation you learned in your Principles of Programming Languages or similar course. This is the offical Chemical Markup Language (CML): Turn in your grammar.

Read the Jan/Feb 2001 MIT Technology Review article about the 10 most promissing technologies that will change the world. Read the entry: Untangling Code (see below for a free copy):

Free copy: For educational personal use only by Northeastern students.

Answer the following question: How is Aspect-Oriented Programming called at Northeastern University according to the article?

Please follow the following rules:

Bring your hw solutions to class or put them into the mail box of the teaching assistant.

Please submit only once when you are done or on the due date,submit a partial solution.

You may turn in your hw for full credit up to two working days late. This means, if it is due on Thursday, you can turn it in the following Monday. If it is due on day x, it means 4.30 pm on that day.

For late homework we will deduct 20% per day late, unless you present a very good reason to the teaching assistant.

The Java programs used in this homework have been generated by DemeterJ.


1. What kind of preparation do I need for this course?
Experience with object-oriented design and programming is an absolute prerequisite.
Get an account on Northeastern machines if you don't have one already and you want one. You can get access to the necessary information on the WWW. For all work you can use any machine of your choice which runs the latest version of Java. But you are responsible for installing the necessary software on your own machine.

2. What am I going to do if I join the class late?
All messages which I send to the class are archived in $ASD/archive/. If you join the class late, you can read that archive to get up-to-date.

3. How to download DJ and DemeterJ

The instructions for downloading DJ and DemeterJ are at:

4. Where can I find the instructions for DemeterJ?
DemeterJ is not needed for this homework, but if you are curious, the instructions are at URL:

5. How can I do my homework on my own computer? If you want to do your homework on your own computer, you need a Java development environment on your PC or workstation and Internet access. In each class usually several students use Windows or Linux to do their homework on their own machine.