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

Chapter 14: Java Code Generation and Reverse Engineering

If the Edit XML Deployment Descriptors check box is checked, Rose will display an editor, which can be used to edit the XML deployment descriptors.

10.

Click Save All to begin the deployment process.

When Rose is finished, a summary screen will be displayed.

Reverse Engineering

When you reverse engineer Java code, Rose will read the classes, relationships, attributes, and operations from the code and generate the appropriate model elements.

In this section, we'll go through each of the steps needed to reverse engineer Java code. The information in your code will be imported directly into the Rose model that is currently open.

1.

If you are using J2EE, JFC, or another Java foundation, create a new model and select the appropriate framework.

2.

Select Tools → Java → Reverse Engineer Java. Rose will display the Java Reverse Engineer window, as shown in Figure 14.21. Using this window, you can select the files you want to reverse engineer.

If you instead see a message that the ClassPath environment variable is not set, there is another step that you must take first. Create an environment variable called ClassPath, and set this variable to the directory or directories where the Java class files are stored.

Figure 14.21: Java Reverse Engineer window

3.

From the directory tree structure, select the directory that contains the files you want to reverse engineer. As you change directories, the available files will appear in the list box in the upper−right

490

Chapter 14: Java Code Generation and Reverse Engineering

area of the window, as we saw in Figure 14.21.

4.

Select the files you wish to reverse engineer and press Add. The files will now appear in the lower section of the window.

5.

Once all needed files have been added to the bottom section of the window, click to select the files you wish to reverse engineer (or press the Select All button to reverse engineer them all) and then click the Reverse button. If there are any errors, they will be written to the log window.

You can reverse engineer .java files, .class files, .jar files, or .cab files. When Rose reverse engineers Java code, it places the new classes in the browser, but does not include them on any Class diagrams.

Java constructs are reverse engineered using the mapping discussed throughout this chapter. For example, interfaces are modeled as classes with a stereotype of Interface. Here, we will briefly review the mapping for reverse engineering.

Rose will look for classes in the source code, and will reverse engineer the attributes and operations of each class. For each operation, the operation code itself will not be included in the model, but the operation signature, including the parameters and return type, will be. Rose will also look for and model any "static," "final," "abstract," "native," or "synchronized" keywords for the operation. Finally, Rose will model the visibility of the operation as public, private, protected, or package.

Rose will also look for interfaces, and will reverse engineer them as classes with an Interface stereotype. It will establish a realize relationship between the implementation class and the interface.

If an attribute is a primitive data type, such as an integer or string, Rose will simply model it as an attribute of the reverse−engineered class. If, however, the attribute's type is another class in the Rose model, Rose will model it as a relationship between the two classes. For example, if the Flight class has an attribute called PassengerName, which has a type of Passenger, Rose will create an association between Flight and Passenger with a role called PassengerName.

For each attribute, Rose will examine any initial value, the attribute visibility (public, private, protected, or package), and any "static," "final," "volatile," or "transient" keywords for the attribute.

Summary

In this chapter, we took a look at how various Rose model elements are implemented in Java. Using the code−generation properties for classes, packages, attributes, operations, associations, aggregations, and other model elements, you have a great deal of control over what gets generated.

We examined the steps needed to generate code from a Rose model, and to reverse engineer Java code into a Rose model. Again, these are the steps you need to follow to generate code:

1.

Create components.

2.

491

Chapter 14: Java Code Generation and Reverse Engineering

Assign Java classes to components.

3.

Set code−generation properties.

4.

Select Tools → Check Model.

5.

Select Tools → Java → Syntax Check.

6.

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

7.

Select Tools → Java → Code Generation.

The steps you'll need to reverse engineer Java code are:

1.

Load the appropriate framework.

2.

Select Tools → Java → Reverse Engineer Java.

3.

Select the file(s) to reverse engineer, and press Reverse.

In the next chapter, we'll take a look at code generation and reverse engineering with Rose and Visual Basic. The Visual Basic add−in includes standard VB constructs such as class modules, forms, interfaces, and ADO objects. The wizard−driven user interface provides a quick and easy way to set your code−generation properties, generate VB code, and reverse engineer VB code. All of these features can help you build robust applications that stay consistent with the object model.

492

Chapter 15: Visual Basic Code Generation and

Reverse Engineering

In this chapter, we discuss how to generate Visual Basic code from your Rational Rose model and how to reverse engineer Visual Basic code into a Rose model.

To generate code, you will need to follow these steps:

1.

Create components (see Chapter 10, "Component View").

2.

Assign classes to components (see Chapter 10).

3.

Set the code−generation properties.

4.

Select a class or component to generate on a Class or Component diagram.

5.

Select Tools → Visual Basic → Update Code to begin the Code−Generation Wizard.

6.

Select Tools → Visual Basic → Browse Visual Basic Source to view the generated code.

We'll discuss the code−generation properties that can be set, and take a close look at how each Rose model element is implemented in the code.

Rose will use a lot of information in the model to generate code. For example, it will look at the multiplicity, role names, containment, and other details of each relationship. It will look at the attributes, operations, visibility, and other details of each class. From all of the information you enter using the specification windows for the various model elements, Rose will gather what it needs to generate code.

To reverse engineer code, you will need to follow these steps:

1.

Create a component and assign a Visual Basic project to the component.

2.

Select Tools → Visual Basic → Update Model from Code.

3.

Select the component(s) and class(es) to update.

4.

Select Finish to complete the process.

493