You are on page 1of 9

Round Trip Engineering

Code generation

After the completion of design stage, complete class diagrams or sometimes known as
the design class diagrams would be generated within a particular model. The general
prerequisite for code generation to be done would require at least a design class
diagram. Rational Rose is a very powerful CASE tool that capable of generating
codes from a particular language based on the predefined semantics. Typically, they
are six steps by steps guidelines would be followed during code generation.

1.0 Assign Java Classes to Java Components in Your Model

2.0 Check Syntax (optional)

3.0 Check the Classpath

4.0 Set the Project Properties that affect Code Generation (optional)

5.0 Backup Your Source

6.0 Generate Java Source Code from Your Model


6.1 Generating Java source from a Class Diagram
6.2 Generating Java source from a Component Diagram

7.0 View (browse) and extend the Generated Source

1.0 Assign Java Classes to Java Components

A component may be anything perhaps the generated codes as well. Rational Rose
Java models .java files as components. Every .java file in Rose will be treated as a
component as well. Therefore, upon completion of code generation, the .java file
would be seen in the component view as a physical component.

1) Rose can automatically generate the .java physical components for you when
multiple classes are being code generated
2) To do this, the menu option from Tools > Option > Notation > Default
Language must be JAVA enabled

Page 1 of 9
Round Trip Engineering

Most of the time, Rose would not be able to produce a single .java file that
consists of multiple classes. If you would like to have multiple classes to reside in
a single .java file, a component must be created and all the related classes should
be placed inside (assign) and upon code generation, all the classes would appear in
the physical .java file. (Note, too, that if you place any Java classes in your newly
created package, Rose would create a mirror image in Component view. It is then
used to create directory or Java packages.)

2.0 Check Syntax (optional)

You can choose to check the syntax of your model components before you try
generating code. Note, though, that syntax checking is done automatically for you
when you generate code. Rose Java’s Syntax checking is based on Java code
semantics

Page 2 of 9
Round Trip Engineering

3.0 Check the Classpath

Classpath is the address pointing to the directory of JAVA compiler. Before code
generation, the classpath must be properly set. Rose Java uses this classpath when
you generate code or reverse engineer existing Java source. All you have to do for
checking the classpath is to navigate to Tools > Java/J2EE > Project
Specification and by clicking the New / Insert button which looks something like
and a button like will appear. Again, click on the dotted button and an
option dialog box would appear

Select Directory and most of the time, the JDK API library would reside in the
following address.

Click on OK and followed by checking the two option below and you are done for
checking the classpath.

When declaring Classpaths, consider:

• Where are your files? Specifically, what are the fully qualified path names?

• What are the package or import statements in the source? You must have a
Classpath entry for the leftmost directory in a package/import statement.

During reverse engineering, Rose converts each package/import statement in your


source generated by Rose into a path in your file system, then makes certain a

Page 3 of 9
Round Trip Engineering

fully qualified path to the class file exists on the Classpath tab (creating it if
necessary).

For reverse engineering, Rose Java requires a Classpath to the JDK class library.
For example, depending on the version of the JDK you’re using, you’ll need a
CLASSPATH setting to classes.zip, rt.jar, or other appropriate library file. (Check
your JDK documentation for current names and locations.). A sample path to the
rt.jar file in JDK 1.4.1 would be x:\jdk1.2.1\bin\jre\rt.jar. For the picture above,
the author has placed the JDK in Program Files folder under C:\ drive. Therefore,
if your JDK is placed in your Program Files folder, the CLASSPATH would be
C:\jdk1.4.1\bin\jre\rt.jar

Page 4 of 9
Round Trip Engineering

4.0 Set the Project Properties that affect Code Generation (optional)

The following properties affect code generation. You set the values for these
settings via the Code Generation tab of the Rose Java Project Specification.

· IDE – Determines which Java environment or IDE you are working in. The
default is Internal Editor and indicates you’re working in a standard JDK
environment. The other options are: IBM VisualAge for Java, Visual Café,
Forte for Java, and JBuilder.

· Stop on Error – When enabled, Rose stops generating Java code at the first
error it encounters. By default, this property is not set, thus allowing code
generation to proceed even when there may be errors. Errors recognized by
Rose are written to the Log.

Page 5 of 9
Round Trip Engineering

· Create Missing Directories – When the Create Missing Directories setting is


enabled (the default), Rose creates any undefined directories that are
referenced as packages in a Rose model when you generate Java code.

· Automatic Synchronization Mode – When this feature is enabled, Rose


automatically initiates code generation any time you create, delete, rename, or
modify a Java element in your library. By default, this feature is off.

In addition, you can determine the format for the code you generate and control
how comments are created with these properties:

· Indentation – You can specify whether to use spaces or tabs and the number of
spaces or tabs to use when formatting code. The default is three spaces.

· Opening Braces – By default, opening braces start on a new line for both
classes and method declarations.

· Comment Style – Rose generates comment text from your model


documentation fields according to the style you select: The Rose default,
standard Java comments, or Javadoc. If you select Javadoc, Rose Java
generates the Javadoc tags you select. You can use the Javadoc compiler to
generate HTML documentation. For more details, see Comments and Javadoc.

You can set the values for all of these settings via the Style tab of the Java Project
Specification.

5.0 Backup your source

When you generate code to an existing .java file, Rose creates a backup of your
current source using the file extension .~jav. However, if you intend to round-trip
engineer your Java source, be sure to back it up before you begin. If you generate
code from a model more than once before looking for a backup, the .~jav file that
Rose creates will not contain your original code.

6.0 Generate Java Source Code from Your Model

Code generation can be done with two approaches such as generating JAVA
source code from class diagram or from component diagram. You can select one
or more classes or one or more components (logical or physical) then click Tools
> Java/J2EE > Generate Java

Page 6 of 9
Round Trip Engineering

If this is the first time you’ve generated code for a model element, a mapping dialog
appears enabling you to map packages and components to your Rose Java classpath
settings. If there are errors or warnings, a message will alert you and you can view the
messages in the Rose Log window (click View > Log). Once code generation is
complete, the .java files and related directory structure are in place. You can also view
the newly generated java source from within Rose.

Note: You can enable Automatic Synchronization Mode to automatically initiate


code generation any time you create or modify a Java element in your model. This is
set on the Style tab on the Java Project Specification (Tools > Java > Project
Specification).

6.1 Generating Java Source from a Class Diagram

From a class diagram, you can generate Java source from classes or from
logical packages. Before you begin, be sure you understand how Rose Java

Page 7 of 9
Round Trip Engineering

generates code for classes based on how they’re assigned to components. (For
details, see Code Generation in Rose Java.)
Follow these steps to generate Java source from classes in a class diagram in
Rose:

1. Open your model and display the class diagram that contains the
classes and packages for which you want to generate Java source.

2. Select one or more classes or packages in the diagram.

3. Optionally, use the syntax checker before generating your Java source.
Although Rose checks syntax as part of code generation, checking
your model before generating could save you time.

4. Click Tools > Java/J2EE > Generate Code.

5. If you have not mapped a package or component to an entry in your


classpath settings (for example, this is the first time you’ve generated
code for a component or package), the Component Mapping dialog
appears allowing you to set up the appropriate associations.

6. Check the Rose Log window to view the results of the Java generation,
including any errors that occurred.

7. Correct any errors and repeat step 4 until no errors are returned.

8. View and edit the generated code using the Internal Editor.

6.2 Generating Java source from a Component Diagram

1. Open your model and display the component diagram that contains the
packages and components for which you want to generate Java source.

2. Select one or more packages and components in the diagram.

3. Optionally, use the syntax checker before generating your Java source..
Although Rose checks syntax as part of code generation, checking
your model before generating could save you time.

4. Click Tools > Java/J2EE > Generate Code.

5. If you have not mapped a package or component to a directory in your


classpath to receive the generated source files (for example, this is the
first time you have generated code for a component or package), the
Component Mapping dialog box appears allowing you to set up the
appropriate associations.

6. Check the Rose Log window to view the results of the Java generation,
including any errors that occurred.

Page 8 of 9
Round Trip Engineering

7. Correct any errors and then repeat step 4 until no errors are returned.

8. View and edit the generated code using the Internal Editor.

7.0 View (browse) and Extend the Generated Source

After generating Java, you may want to view (browse) the generated source (right-
click on a class and click Java/J2EE > Edit Code) and create the actual functionality
for your application or applet.
Rose provides an Internal Editor for viewing and editing your .java files.
You can modify the generated source from within the editor. To update your model
with the changes, you need to reverse engineer the .java file back into your model.

Page 9 of 9