Chapter 17 Code Engineering

Real-time Round Trip Engineering

Creating Class Diagram with Real-time Code Generation

Experience of real-time code generation

Experience of real-time code reverse engineering

Code Engine Set Configuration

Opening Configuration dialog of code engine set

Language option for Code Engineering

Code Generation configuration

Selecting class for code engineering

Deselecting class for code engineering

Code Reverse Engineering configuration

Defining source paths

Defining classpaths

Code-Model synchronization Engineering

Code generation

Language syntax checking

Code reverse engineering

Reversing code to model after code generation

Reversing code with existing Java project

Other supports of Code Engineering

Simple method for Code Generation

Simple method for Language Syntax Checking

Simple method for Code Reverse Engineering


 

Chapter 17  Code Engineering

 

Code Engineering is one of main features in VP-UML. Code Engineering support both code generation and code reverse engineering. VP-UML Professional Edition supports both real-time code round trip engineering and code-model synchronization engineering of Java Programming Language. Real-time code round trip engineering is an engine for the reflecting between codes and models in real-time. It works on the Class Diagram. Code-model synchronization engineering is an engine for the implementation of whole UML project.

 

 

 

Real-time Round Trip Engineering

 

The real-time round trip engineering works with the Class Diagram. Either you create a Class Diagram with real-time code generation or not, you can use the real-time round trip engine.

 

In the VP-UML, code engineering is targeted on Class for generation and reverse engineering of the source code. The means of round trip engineering is an iterative process of code generation and code reverse engineering. Real-time round trip engineering means the change of diagram is reflected to the code automatically. Likely, the change of code is also reflected to the diagram at once. User needs to create a class diagram with real-time code generation for the use of real-time round trip engineering.

 

 

 

Creating Class Diagram with Real-time Code Generation

 

1.      Click on File on the menu bar. The file menu appears.

2.      Click on New diagram on the file menu. A cascading menu appears.

 

Figure 17.1 Using menu to create a new class diagram with real time code generation

 

 

3.      Click on New Class Diagram with Real Time Code Generation on the cascading menu. A configuration dialog is shown in case you are first time to create.

 

Figure 17.2 Dialog for configure of code generation

 

 

 

 

 

 

4.      In the configuration dialog, you need to specific the Working Directory which is used to store the generated source file.

5.      After you had set the working directory, a blank new Class Diagram appears

 

Figure 17.3 The Class Diagram of Real-time code generation.

 

 

 

 

Alternative methods for creating class diagram, please refer to Chapter 6 – Working with Class Diagrams

 

 

 


If you have an existing class diagram, you can switch it to have real-time code generation. In the popup menu of diagram pane, you can check the Real Time Generate Code to enable the real time code generation. In addition, you can check the Show Code Pane to show the code pane for editing code.

 

 

 

 

 

 

In the real-time class diagram, the source code of the class is shown when you select a class in the diagram. After you have selected the class, the change of content of the class in the diagram will be reflected to the code at once. For example, rename of the class, changes attributes and operations of the class, etc.

 

 

 

Experience of real-time code generation

 

1.      In the class diagram, create a Class.

2.      At the time of the creation of the class, this class is already selected. So, the source code of this class is generated.

 

 

 

Figure 17.4 Showing code when class is selected.

 

3.      Then user can edit the class in the class specification belong to your analyze of your system.

 

Figure 17.5 Simple Class Diagram with real-time code generation

 

 

 

 

 

 

About how to edit the class in the class specification, please refer to Chapter 6 – Class Diagram

 

 

 

Likely, editing source code will also reflected to the class in the diagram. After you have edited the code, VP-UML will process the code and then the mode will be updated. If the code contains some invalid syntax of the programming language, then the class will be locked. Once the class is locked, VP-UML will not allow user to make changes to that class. The lock will be released until the code is being edited again and the syntax is correct.

 

 

 

Experience of real-time code reverse engineering

 

1.      In the class diagram, create a Class.

2.      At the time of the creation of the class, this class is already selected. So, the source code of this class is generated.

Figure 17.6 Showing code when class is selected

 

 

 

3.      Then user can edit the code of the class in the code pane.

Figure 17.7 Editing source code of the class

 

 

4.      After the code is edited, and click on the class again. The class will be updated.

Figure 17.8 Update class after code is edited

 

 


 

Code Engine Set Configuration

 

Code engine set is a repository for code engineering. It stores two things. One is the source code file(s) and class model(s) for code generation and reverse engineering. Another is the options for the language generation and reverse engineering. Both real-time round-trip engineering and code-model synchronization engineering will use this repository as the base. Selected source code(s) and model(s) in repository will be used in code-model synchronization engineering only. Options of code engine set will be used in both real-time round trip and code-model synchronization engineering.

 

User need to configure the engine set to add or remove the selected class for code generation, add or remove the selected source code(s) for reverse engineering and model syntax checking.

 

 

Opening Configuration dialog of code engine set

 

1.      Click on Code on the menu bar. The code menu appears.

2.      Click on Java on the code menu. A cascading menu appears.

3.      Click on Option on the cascading menu. A configuration dialog is shown.

 

Figure 17.9 Opening code engine set configuration dialog.

 

In the configuration dialog, user can configure three different things. Language options, code generation configure and code reverse engineering configure.

 


Language option for Code Engineering

 

For code generation, each programming language has its own option for code generation.

 

Figure 17.10 Java language options

 

In the Java language option, there have two main types of options. First is Default type. UML class is allowed to have an attribute with attribute type. Similarly, operation and parameter also allowed doing that. So, this default data type will be used when the type of attribute, operation, or parameter is missing in code generation.  Second is Code Styles. Option of code style includes code generation and reverse of documentation and generation of brace type. In addition there has a preview area. This preview area will provide a preview of the generated code when using the current options.


 

Option Name

Description

Java code editor path

Set the path of Java source code editor. VP-UML will bring up the editor when you want to edit the source code of current selected class

Default attribute type

In code generation of class, if this class has attribute(s), which miss the attribute type, code engineering will use this default attribute type to perform the code generation.

Default operation return type

In code generation of class, if this class has operation(s), which miss the operation return type, code engineering will use this default operation return type to perform the code generation.

Default parameter type

In code generation of class, if this class has parameter(s), which miss the parameter type, code engineering will use this default parameter type to perform the code generation.

Header description

The header description will be generated to the source code as source Javadoc comment.

Update JavaDoc in code generation

Enable the code generation to update the existing comment in the source code.

Update model documentation in code reverse engineering

Enable the code reverse engineering to update the documentation in model with reversed source code (Source Javadoc is not support in code reverse engineering).

Automatic import statement generation

Support import statement in code generation

Brace type of Class

The coding style of open brace and close brace of class in source code

Brace type of operation

The coding style of open brace and close brace of operation in source code

Brace type of inner-class

The coding style of open brace and close brace of inner-class in source code

 

 

 


 

Code Generation configuration

 

In the configuration of code generation, there has two parts. There are code generation options, and selection of generated class model. Code generation options are used for the code generation. Selection of generated class model is for selecting the model for code generation, and model syntax checking.

 

 

Figure 17.11 The configuration of code generation

 

 

Option Name

Description

Working directory

The directory for store the generated source code file.

Generate source file in real-time code generation

In the real-time generation, code generation is not need to write to be a file. If check this option, code generation will write to the source file also.

Generate Association

Check this option to support the code generation of association. An association will represent as an attribute in code.

Generate unnamed association ends

After check this option, the unnamed association ends will be supported to generate code as an attribute.

Reverse before generate

After check this option, the source code will be reversed to the model before generation the source code.

Handle type of element delete from model

The type of update code for this class which deleted attributes, operations, or parameters. About the type of update code, user can select Delete code, Comment code, and Ignore change.

Delete code means the code of that model will be deleted if the model is deleted. Comment Code means the code of that model will be commented if the model is deleted. Ignore change means the code of that model will not be updated if the model is deleted.

Use space(s) to replace tab

In code generation, a indentation is generation with a tab. User can check this option if user want to user space to replace tab.

Number of space(s) from tab

Specify the number of space for a tab.

 

 

 

 

Selecting class for code engineering

 

1.      Check to select class in the tree of Available Classes.

Figure 17.12 Selecting available class to generate code

 

2.      Click the  button to add the available classes to the tree of Classes to Generate.

Figure 17.13 Selected classes are added to be generated code

 

 

 

Once the class model is added to the tree of Classes to Generate, this means the classes in this tree is able to have code engineering.

 

 

 

Deselecting class for code engineering

 

1.      Check to select class in the tree of Classes to Generate.

Figure 17.14Check class(s) to not generate code

 

 

2.      Click the  button to remove the generation of classes to from the tree of Classes to Generate.

Figure 17.15 Selected classes are removed for generate code

 

 

 

After select the class models, both code generation and model syntax checking of code engineering can perform according to these selected class models. These will discuss in next section.

 

 

 

For real-time round trip engineering, all classes are under the tree of Classes to Generate which created in the real-time code generation class diagram.

 

 


Code Reverse Engineering configuration

 

In the configuration of code reverse engineering, there has three parts. There are code reverse engineering options, define source paths and define classpaths. Code reverse engineering options is the preference settings for code reverse engine. Source paths are required to define for code reverse engineering. The code reverse engineering is work depends on the defined source paths. Classpaths are required as library which used in the project of source paths.

 

Figure 17.16 The configuration of code reverse engineering

 

 

Option Name

Description

Look for classes in library

After user defined the classpaths of library, user can check this option to allow code reverse engine to solve data type with library.

Association

 

Reverse to attribute

Check this option for create an attribute in the class but not create the association even the attribute type for association is found.

Reverse to Association

Check this option for create association in model automatically if the type of the attribute which is a defined class or the class in the classpath. But the association attribute will create as an attribute in the class.

Reverse to Attribute and Association

Check this option for create association in model automatically if the type of the attribute which is a defined class or the class in the classpath. And the association attribute will create as an attribute in the class.

Merge model and code

During code reverse engineering, the model of the class model will be updated depends the change of the code. But if the user checks this option, class model will be merged with the change of the code. Merge of model means the attribute and operation will be created if reverse engine find that they are new. And it will not remove and attribute or operation in the class model if they deleted in the code.

Refresh model by code when re-open project

Code reverse engineering will perform when user re-open this VP-UML project.

Refresh model by code when application lose focus

Code reverse engineering will perform when application get the focus after it lose.

 

 

 

Defining source paths

 

Source path can be a path of directory or a path of a file. User can add, edit, and remove a source path. In addition, user can move up and move down a source path to change the order of source path in code reverse engineering. By default, working directory of code generation is one of source paths. And the source path of working directory is not allowed to remove.

 

Figure 17.17 Defining source paths

 

 

 


 

Adding a source path

 

1.      To add a new source path, click Add button. A file chooser appears.

Figure 17.18 Selecting source path

 

2. Select the path, click Open. Then the source path is added.

Figure 17.19 Selected source path is added

 

 

 


 

Removing a source path

 

1.      Click the existing source path which is wanted to delete.

Figure 17.20 Selecting source path to delete

 

2.      Click the Remove to remove it.

Figure 17.21 Removed the selected source path

 

 

 

 


 

Editing an existing source path

 

1.      Click the existing source path which is wanted to change.

Figure 17.22 Select existing source path

 

2.      Click the Edit. A file chooser appears.

3.      Select the new source paths for replace with the selected existing source path.

Figure 17.23 Select new source path for replace

 

4.      Click Open to confirm.

Figure 17.24 Existing source path is replaced


 

Moving up a source path

 

1.      Select the source path which wants to move up in the order of the source paths.

Figure 17.25 Selecting source path to move up

 

2.      Click on  to move up the source path.

Figure 17.26 Moved up for the selected source path

 

 

 


 

Moving down a source path

 

1.      Select the source path which wants to move down in the order of the source paths.

Figure 17.27 Selecting source path to move down

 

 

2.      Click on  to move down the source path.

Figure 17.28 Moved down for selected source path

 

 


 

Defining classpaths

 

Classpath can be a path of directory of a path of a file (Java byte code file or jar file). User can add, edit, and remove a classpath. In addition, user can move up and move down a classpath to change the order of the classpath for code reverse engineering. At the time of create code engine set, library of Java development kit will be added to this classpath.

 

Figure 17.29 Defining classpaths

 

 

 


 

Adding a classpath

 

1.      To add a new classpath, click Add button. A file chooser appears

Figure 17.30 Selecting classpath

 

2.      Select the path, click Open. Then the classpath is added.

Figure 17.31 Selected classpath is added

 

 


 

Removing a classpath

 

1.      Click the existing classpath which is wanted to delete.

Figure 17.32 Selecting classpath to delete

 

2.      Click the Remove to remove it.

Figure 17.33 Removed the selected classpath

 

 


 

Editing an existing classpath

 

1.      Click the existing classpath which is wanted to change.

Figure 17.34 Select existing classpath

 

2.      Click the Edit, a file chooser appears.

3.      Select the new classpath for replace with the selected existing classpath.

Figure 17.35 Select new classpath for replace

 

4.      Click Open to confirm.

Figure 17.36 Existing classpath is replaced

 


Moving up a classpath

 

1.      Select the classpath which wants to move up in the order of the classpaths.

Figure 17.37 Selecting classpath to move up

 

2.      Click on  to move up the classpath.

Figure 17.38 Moved up for the selected classpath

 

 


 

Moving down a classpath

 

1.      Select the classpath which wants to move down in the order of the classpaths

Figure 17.39 Selecting classpath to move down

 

2.      Click on  to move down the classpath.

Figure 17.40 Moved down for selected classpath

 

 

 


 

Code-Model synchronization Engineering

 

Besides real-time round trip engineering, VP-UML Professional Edition supports another code engineering, code-model synchronization engineering. Synchronization engineering is another type of round trip engineering. It is synchronization between code and model. Code means the source file(s) of code. And model means Class(s) in a VP-UML project. It works in three parts, Languages syntax checking, Code generation, and code reverse engineering. Language syntax checking is used for check the class with selected generation of the Programming Language. Code generation is used to generate the source code for selected class. Code reverse engineering is used to reverse the source code for selected class. Moreover, code reverse engineering supports to reverse an existing source of Java Language Project. Code-Model synchronization engineering support not only in the Class Diagram, but also it works in whole VP-UML project.

 

Code-model synchronization engineering is fully worked with the code engine set. User is required to configure the code engine set in advanced.

 

 

 

 

About the details of how to configure the code engine set, please refer to the previous section – Code Engine Set Configuration.

 


Code generation

 

In VP-UML Professional Edition, it supports to generation the source code of any class model. This means any class which creates in VP-UML is supported to have code generation, such as in sequence diagram. After the user define the class(es), and the code engine set is configured, code generation is ready to perform. The following will guide the user to know how to use code generation.

 

 

1.      Draw the class diagram

Figure 17.41 Class diagram is drawn

 

 

2.      Click on Code on the menu bar. The code menu appears.

3.      Click on Java on the code menu. A cascading menu appears.


4.      Click on Option on the cascading menu. A configuration dialog is shown.

 

Figure 17.42 Configured code engine set for code generation

           

5.      Click on OK to confirm the setting.

6.      Click on Code on the menu bar. The code menu appears.

7.      Click on Java on the code menu. A cascading menu appears.

Figure 17.43 Using menu to perform code generation

 

8.      Click on Code Generation on the cascading menu. A progress dialog is shown and the code generation will start automatically.

Figure 17.44 Generating the source of the selected class model

 

User can click on   to see the detail messages during code generation.

 

 

 

User can check the option Close Dialog when finished progress. Then the next time of the progress dialog will not show.

 

 

 

9.      When the progress completed, click on Close button to close the dialog.

Figure 17.45 Finishing code generation and messages detail are shown

 

 

Also, user can get a message when code generation is completed.

Figure 17.46 Message for completed of code generation

 

 

In addition, during code generation, any error and warning of code generation will show message in the message pane.

 

 

Figure 17.47 Occurring error during code generation

 

The most common warning and error messages come from the language syntax checking.

 

 

 

After the code generation is done, user can get the source code file(s) in the working directory (which specified in before).

 

Figure 17.48 Viewing source code file list

 

 

Figure 17.49 Viewing source code of class - Car

 

Figure 17.50 Viewing source code of class - Human

 

 

Figure 17.51 Viewing source code of class - Vehicle

 

 

Language syntax checking

 

Besides code generation and code reverse engineering, language syntax checking is also a part of the code engineering. Language syntax checking is a checking of model which depends on the programming language of the code generation. For example, language syntax checking will use Java Programming Language syntax to verify your class models. The code generation will perform only when the class model is valid to the syntax checking.

 

For the syntax of UML, there has some different with Java Language. For example, Java does not support multiple inheritances. Therefore, language syntax checking ensures the data of class model is valid for code generation of Java Language. The checking will validate the data which will only be used in code generation.

 

Language syntax checking can be performed separately. Moreover, language syntax checking also is performed before performing code generation. Same as code generation, language syntax checking will verify the class model which selected in code engine set. The following will guide the user to know how to use language syntax checking.

 

1.      Draw the class diagram

Figure 17.52 Class diagram is drawn

 

 

2.      Click on Code on the menu bar. The code menu appears.

3.      Click on Java on the code menu. A cascading menu appears.

4.      Click on Option on the cascading menu. A configuration dialog is shown.

Figure 17.53 Configured code engine set for language syntax checking

 

5.      Click on OK to confirm the setting.

6.      Click on Code on the menu bar. The code menu appears.

7.      Click on Java on the code menu. A cascading menu appears.

Figure 17.54 Using menu to perform language syntax checking

 

8.      Click on Language syntax checking on the cascading menu. A progress dialog is shown and the selected model in code engine set will be to be verified.

Figure 17.55 Syntax checking of the selected class model

 

 

User can click on   to see the detail messages during code generation.

 

 

 

User can check the option Close Dialog when finished progress. Then the next time of the progress dialog will not show.

 

 

 

9.      When the progress completed, click on Close button to close the dialog.

Figure 17.56 Completed language syntax checking and messages detail are shown.

 

 

Also, user can get a message when language syntax checking is completed.

Figure 17.57 Message for completed of language syntax checking

 

 

During language syntax checking, there will have some warning messages and error messages may be product. Warning message means that the data is invalid but it is not failure. Error message means that the data is failure and will affect the code generation.

 

 

Message type

Description

*

Information

Display when progress is completed.

*

Warning

Data or relationship of the class is invalid for code generation, but the situation can be ignored.

 

The case to get warning message:

*      A class has a generalization from an interface class.

*      A class has a realization from a non-interface class.

*      An interface has generalization from any class.

*      An interface has any realization.

*      An attribute type of attribute is missing.

*      A return type of operation is missing.

*      A parameter type of parameter is missing.

 

*

Error

Data or relationship of the class is invalid for code generation, and the situation can not be ignored.

 

Common case to get error message:

*      The name of the package, class, attribute, operation or parameter is invalid, such as, including white space.

*      Volatile the modifiers in Java language

 

Special case to get error message:

*      The class has generalization from a final class.

*      The initial value of attribute is missing in the interface class.

 

 

 

Code reverse engineering

 

In VP-UML Professional Edition, it supports two type of code reverse engineering. User can use code reverse engine to fully reverse his existing Java project. Or the user can use code reverse engine to reverse the source code which generated in the phase of code generation.

 

Figure 17.58 The java source file

 

Figure 17.59 The class diagram after reverse

 

Reversing code to model after code generation

 

After the code generation, user can edit the source code for his project. Then user can use code reverse engineering to update the class models in diagram of VP-UML project.

 

 

Assume that the user had generated the source code in the phase of code generation. About the details of code generation, please refer to Chapter 17 – Code Engineering ~ Code-Model Synchronization Engineering.

 

 

1.      Draw the class diagram.

Figure 17.60 Drawing class diagram for code generation

 

 


2.      Generate the source code of the class.

Figure 17.61 Source code is generated

 

3.      Edit the source code.

Figure 17.62 Edited source code of class – Car (Colored in red is the highlight of change only)

 

4.      Click on Code on the menu bar. The code menu appears.

5.      Click on Java on the code menu. A cascading menu appears.

Figure 17.63 Using menu to perform code reverse engineering

 

6.      Click on Code Reverse Engineering on the cascading menu. A progress dialog is shown and the code reverse engineering will start automatically.

Figure 17.64 Reversing the source of the select class model

 

7.      When the progress completed, click on Close button to close the dialog.

Figure 17.65 Finishing code reverse engineering and messages detail are shown

 

 

Also, user can get a message when code reverse engineering is completed.

Figure 17.66 Message for completed of code reverse engineering

 

8.      Click on Close button to close the progress dialog. And the class diagram is refreshing automatically.

Figure 17.67 Refreshing of class after code reverse engineering (Colored in red is highlight for change only)

 

 

 

The user can enable the option Refresh model by code when application loses focus. So that the model will auto refresh with the source code when VP-UML get back the focus after lose. About the details of how to enable the option, please refer to Chapter 17 – Code Engineering ~ Code Engine Set Configuration.

 

 

 

Reversing code with existing Java project

 

The user can reverse the source code of an existing Java project to become a class model in VP-UML project. Before use the code reverse engineering, user should specific the source paths of the existing Java project.

 

1.      Click on Code on the menu bar. The code menu appears.

2.      Click on Java on the code menu. A cascading menu appears.

3.      Click on Option on the cascading menu. A configuration dialog is shown.

Figure 17.68 Configured source paths in code engine set for code reverse engineering

 

4.      Click on OK to confirm the setting.

5.      Click on Code on the menu bar. The code menu appears.

6.      Click on Java on the code menu. A cascading menu appears.

Figure 17.69 Using menu to perform code reverse engineering

 

 

7.      Click on Code Reverse Engineering on the cascading menu. A progress dialog is shown and the code reverse engineering will start automatically.

Figure 17.70 Reversing the source file of the existing Java project

 

8.      When the progress completed, click on Close button to close the dialog.

Figure 17.71 Finishing code reverse engineering and messages detail are shown

 

 

Also, user can get a message when code reverse engineering is completed.

Figure 17.72 Message for completed of code reverse engineering

 

 

After the code reverse engineering, user can use the class model in the VP-UML project.

Figure 17.73 Viewing the class which created in code reverse engineering in the model tree view.

 

 

About the details of how to use the models in model tree view, please refer to Chapter 5 – Basic Editing.

 

 

 

 


 

Other supports of Code Engineering

 

User invokes the functions of code engineering (code generation, code reverse engineering, and language syntax checking) using the menu in the normal way. All invocation in menu is work as complete mode of operation. In fact, VP-UML supports other way for simple mode of code engineering.

 

 

Simple method for Code Generation

 

1.      Right click on a class. A popup menu appears.

Figure 17.74 Invoking code generation in popup menu

 

2.      Click on Generate code on the popup menu. A code generation options dialog appears.

Figure 17.75 Showing code generation options dialog

 

 

The value of the options comes from the setting of code engine set. The option can be set in the code engine set or in the code generation option dialog. About the use of options, please refer to Chapter 17 – Code Engineering ~ Code Engine Set Configuration

 

 

3.      Click on OK to generate the source code.

Figure 17.76 Generating the source code

 

4.      Click on Close to close the progress dialog.

Figure 17.77 Finishing code generation

 

The source code file is generated to the working directory. The location of working directory should be set in the code engine set before invoke the code generation.

 

 

User can select more than one class to generate the source code by using this simple method.

 

Because this method of code generation is simple mode of code generation, the synchronization of package is omitted. That means the file location of the generated source code file will not follow the change of package. User can synchronize the package when performing completed mode of code generation which invoke from the code menu.

 

Simple method for Language Syntax Checking

 

1.      Right click on a class. A popup menu appears.

Figure 17.78 Invoking language syntax checking in popup menu

 

2.      Click on Language syntax checking on the popup menu. A language syntax checking started.

Figure 17.79 Checking the model with language of code generation

 

3.      Click on Close to close the progress dialog.

Figure 17.80 Finishing check with language syntax

 

 

Simple method for Code Reverse Engineering

 

1.      Right click a class. A popup menu appears.

Figure 17.81 Invoking code reverse in popup menu

 

2.      Click on Reverse code on the popup menu. A code reverse options dialog appears

Figure 17.82 Showing code reverse engineering option dialog

 

 

3.      Click on OK to reverse the source code

Figure 17.83 Reversing the source code

 

4.      Click on Close to close the progress dialog.

Figure 17.84 Finishing code reverse engineering

 

The model will be updated after the source code the class had performed reverse engineering.