Documentos de Académico
Documentos de Profesional
Documentos de Cultura
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.
4.0 Set the Project Properties that affect Code Generation (optional)
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.)
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
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.
• 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.
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
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.
You can set the values for all of these settings via the Style tab of the Java Project
Specification.
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.
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.
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.
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.
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.
1. Open your model and display the component diagram that contains the
packages and components for which you want to generate Java source.
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.
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.
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