Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Mastering UML with Rational Rose 2002.pdf
Скачиваний:
137
Добавлен:
02.05.2014
Размер:
9.68 Mб
Скачать

Chapter 14: Java Code Generation and Reverse Engineering

 

Include the complete file path in the generated code

 

 

for reference attributes.

 

IsNavigable

Indicates navigability

True

Generating Code

Once you have created the classes, relationships, attributes, operations, EJBs, servlets, and other Java elements in your Rose model, you can generate code. To generate code, follow these steps:

1.

Create the needed components.

2.

Assign the Java classes to the appropriate components.

3.

Set the code−generation properties.

4.

Select Tools → Check Model to check for language−independent model errors. Check the log window for any errors found, and resolve the errors.

5.

Select Tools → Java → Syntax Check to check for Java−specific problems. Again, check the log window for a list of errors found, and resolve the errors.

6.

Select the class(es) or component(s) to generate.

7.

Select Tools → Java → Code Generation.

8.

To view the generated code for a class, right−click the class on a diagram and select Java → Browse Java Source.

Generated Code

In the following sections, we'll take a look at the Java code generated for a class, attribute, and operation, and for the different types of relationships between classes. In each of these sections, we'll include some sample code to give you an idea of what will be generated from your Rose model.

Rose uses the information in the specifications of the model elements when generating code. For example, it will look at the different specifications for a class (visibility, attributes, operations, and so on) when generating code for the class.

Let's begin by looking at the code generated for a typical class.

462

Chapter 14: Java Code Generation and Reverse Engineering

Classes

A class in your object model will become a Java class when you generate code. All of the attributes, operations, and relationships of the class will be reflected in the generated code. The major elements generated for each class include:

The class name

The class visibility

A constructor for the class

Class documentation

Attributes

Operations

Relationships

Without a component mapping, each class in the model will generate one file with the .java extension. Each file will be named using the class name. For example, an Employee class will generate an Employee.java file. With a component mapping, the class (or classes) mapped to each component will be generated in a .java file for the component.

Much of the information in your Rose model will be used directly when generating code. For example, the attributes, operations, relationships, and class name of each class will directly affect the code generated. Other model properties, such as the documentation entered for the class, will not directly affect the code. These properties are created as comments in the generated code.

Table 14.8 lists the properties available in the class specification window and notes which properties will directly affect the code that is generated.

Table 14.8: Effect of Class Specifications on Generated Code

Property

Effect on Code

Name

Name in model will become class name.

Type

Directly affects the type of class created.

Stereotype

Directly affects the type of class created.

Export Control

Directly affects the class visibility.

Documentation

Appears as a comment.

463

 

Chapter 14: Java Code Generation and Reverse Engineering

 

 

 

Persistence

 

Affects whether DDL can be generated for the class.

Abstract

 

Creates an abstract class.

Formal Arguments

 

Formal arguments are included in the code for a parameterized class.

Operations

 

Generated in code.

Attributes

 

Generated in code.

Relationships

 

Generated in code.

Let's look at the code generated for the following class:

The following code is the Java file that was generated for this class.

//Source file: C:\\Flight.java

/* Copyright Notice */

/**

Comments for Flight Class */

public class Flight

{

private int FlightNumber;

public Flight()

{

}

/**

@roseuid 394461CB01F3 */

public boolean AddPassenger(int PassengerID)

{

}

}

Copyright Notice Section

The copyright notice section includes the following line of code:

/* Copyright Notice */

By default, there is no copyright notice generated for your code. If, however, you'd like to add a copyright notice to all files, you can change the code−generation properties. Select Tools → Options from the menu, then select the Java tab. Select Module Specification from the drop−down list box to display the module specification code−generation properties. Change the CopyrightNotice field to include any copyright information. If information is entered, it will be generated.

You can override the default for a specific component by changing the value in the Copyright field in the component specification window or in the CopyrightNotice property on the Java tab of the component's

464

Chapter 14: Java Code Generation and Reverse Engineering

standard specification window.

Attributes

Aside from the class itself, Rose will generate the attributes for the class. For each attribute, Rose will include information about the attribute visibility, data type, and default value in the code. Let's look again at the code generated for the Flight class.

//Source file: C:\\Flight.java

/* Copyright Notice */

/**

Comments for Flight Class */

public class Flight

{

private int FlightNumber;

public Flight()

{

}

/**

@roseuid 394461CB01F3 */

public boolean AddPassenger(int PassengerID)

{

}

}

As you can see, the code includes the attribute visibility and data type. If the default value is set, it will also be included in the generated code.

To apply one of the modifiers, such as the transient modifier, to an attribute, set the appropriate property to True. In this case, you set the transient property to True, and the attribute generates with the modifier, as in the following code:

//Source file: C:\\Flight.java

/* Copyright Notice */

/**

Comments for Flight Class */

public class Flight

{

private transient int FlightNumber;

public Flight()

{

}

/**

@roseuid 394461CB01F3 */

465