Java Code Generation from Class Diagram

VP-UML supports generating Java source code from UML class model, and keeping the code and model synchronized. In this tutorial we will develop a simple class diagram, generate code from it, modify the code and update the changes back to UML class model.

May 10, 2010
User Rating: / 9
Views: 6,039
PDF Link Add comments
Edition: Professional or above (Edition comparison)

  1. Start VP-UML in a new workspace.
  2. Create a class diagram and name it as Employee. You can create a class diagram through the Diagram Navigator.
    new class diagram
  3. Select Class from diagram toolbar and click on class diagram to create a class. Name it as Person.
    create class
  4. Create attributes in it. Right click on Person and select Add Attribute from the popup menu. Enter name : String to name the attribute as name and set type as String.
    attr added to person
  5. Repeat step 4 to create 2 more attributes:
    Name Type
    age int
    address String
  6. Now, create a sub-class from Person. Move the mouse pointer to class Person and press on the resource icon Generalization -> Class.
    select generalization
  7. Drag downwards and release the mouse button. Name the class as Employee.
    create employee
  8. Add the following attributes to the Employee class:
    Name Type
    employeeID String
    role String
  9. Create a class Department next to Employee class, like this:
    department created
  10. Add the following attributes to the Department class:
    Name Type
    deptID String
    name String
  11. Associate Department with Employee. Move the mouse pointer to class Department and press on the resource icon Association -> Class.
    move to asso resource
  12. Drag to Employee class and release the mouse button.
    association created
  13. Name the roles of both ends of association. Double click at the end of association to name it.
    roles set
  14. Double click at the association end again and specify the multiplicity of both ends. Set the Department end to be 1 and the Employee end to be 1..*.
    set multiplicity
  15. Now comes code generation. Select Tools > Java Round-trip > Generate Code... from the main menu.
    gen code
  16. Specify the source path and click OK.
    gen code dialog
  17. Open the generated code files.
    employee class
  18. Let's edit the code file. After that, we will update the changes back to model. Add a static operation createEmployee() in the Employee class. It returns and Employee object.
    added oper
  19. Save the file and go back to VP-UML.
  20. Select Tools > Java Round-trip > Reverse Code... from the main menu.
  21. In the Reverse Code dialog box, uncheck Reverse source on demand and click OK. This option will cause an index tree to created instead of reversing files directly. Since we want to update the class model directly, we just uncheck it.
    reverse code
  22. You can see that the operation createEmployee() is created in Employee class.
    changes updated

Rate this Article

Click on one of the stars below to rate this article from 1 (lowest) to 5 (highest).

You may be interested in

  1. Writing Effective Use Case
  2. UML Package Diagram
    Tagged:
  3. Draw Entity Relationship Diagram (ERD)
    Tagged:
  4. Produce UML Use Cases from Business Process Diagram (BPD)
  5. Draw UML Sequence Diagram with Keyboard
Tags of this article:

Comments (1)

written by osith on August 9, 2010

its very easy to generate java code and this one is very use full for me thanks.

Write comment

Rating

Comment

Enter the calculation result

security code