Generate Java source from UML class diagram in SDE for NetBeans

You can perform round-trip engineering in SDE for NetBeans, to keep Java source code and class model in sync. In this tutorial, we will see how to create a class model in SDE for NetBeans, and eventually generating source code from model.

February 22, 2010
User Rating: / 4
Views: 8,054
PDF Link Add comments
Edition: Professional or above (Edition comparison)

  1. Create a Java project Express Courier in NetBeans.
    create java project
  2. Right click on the project node in Projects window, and select Open SDE EE-NB from the popup menu.
    open sdenb
  3. SDE-NB is started. In Diagram Navigator (of SDE), right click on Class Diagram and select New Class Diagram from the popup menu.
    new class diagram
  4. A new diagram is created. You asked to enter a package header on top of the diagram. Enter myapp and press Enter.
    package header
  5. You are asked to provide the diagram name. Enter Domain Model and press Enter.
    enter diagram name
  6. Click on the down arrow button near the shape selection Class in diagram toolbar, and select Interface.
    select interface
  7. Click on the diagram to create an interface class and name it as IMailDelivery.
    create interface
  8. Create operations in IMailDelivery. Right click on the class IMailDelivery and select Add > Operation from the popup menu.
    add oper to interface
  9. Enter setState(state : char) : void to create a public operation stateState with parameter state and return void.
    naming set state oper
  10. Press Enter to create another operation. Name it printShipmentInfo() : void. Click on diagram to confirm editing.
  11. We need to create a class for local delivery which inherits IMailDelivery. Move the mouse pointer over interface IMailDelivery, press on the resource icon Realization -> Class and drag downwards to create a new class.
    realize class
  12. Name the class as LocalDelivery and press Enter to confirm.
    create local delivery class
  13. As the LocalDelivery class is implementing the interface IMailDelivery, we need to implement the operations defined in IMailDelivery. Right click on class LocalDelivery and select Related Element > Realize all Interfaces from the popup menu.
    realize interface
  14. You can see that operations setState and printShipmentInfo are both inherited.
    interface realized
  15. It is time to add attributes to classes. Right click on class LocalDelivery and select Add > Attribute from the popup menu.
  16. Enter state : char to name the attribute as state, and set the type as char.
    add attr state
  17. Press Enter to proceed to the next attribute. Enter postage : double as attribute name and type.
  18. Again, press Enter and create attribute shipmentNum : int.
    general attrs created
  19. We need to have two constants for representing states. Press Enter to continue creating attribute. Name the attribute STATE_INIT : char = 'a'. This means to create an attribute named STATE_INIT, which is in char type and have 'a' as default value. Press Enter to create another constant STATE_DELIVERED : char = 'b'. Up to now, the class should look like:
    basic states consts created
  20. In order to declare both STATE_ attribute as constants, select STATE_INIT, press the Ctrl key and select STATE_DELIVERED to make a multiple selection. Right click on them and select Model Element Propertes > Scope > classifier from the popup menu. By doing so, both attributes will be static (in code level) and are underlined.
    set consts scope
  21. Again, right click on the two attributes and select Code Detail > final from the popup menu. Click on the diagram to confirm editing. The class should now become:
    consts created
  22. We need to create a new class Mail with association from class LocalDelivery. Move the mouse pointer to the class LocalDelivery. Press on the resource icon Aggregation -> Class.
    to create aggregated class
  23. Drag to the right of the class LocalDelivery and release the mouse button. Name the new class Mail.
    mail class created
  24. Follow the previous steps to create attributes in class Mail.
    Class Attributes Operations
    Mail fromPerson : String
    fromContact : String
    toPerson : String
    toContact : String
    mailType : int
    -
  25. We need to describe the attribute mailType by listing the possible values. Select mailType.
  26. Activate the documentation pane. If you cannot find it on the screen, or if you had closed that, open it by selecting View > SDE EE-NB View > Documentation from the main menu. In the documentation pane, enter the following:
    Possible types:
    1 - Flat
    2 - Letter
    3 - Postcard
    4 - Parcel

    document attribute
    The documentation for classes, attributes, operations and parameters will become comments in source code to be generated.
  27. Up to now, the diagram should look like:
    complete diagram
  28. Select File > Save SDE EE-NB Project from the main menu to save.
  29. Now comes the code generation. Select the menu Update Code on Diagram Navigator.
    updated code
  30. Check the Projects window. You should see a list of generated file. You can open them to fill in the code body.
    code generated
  31. This is the end of the tutorial. Instead of closing NetBeans now, you may try something more by editing the code like to add, rename or delete class, attributes and operations, and click the Update UML Model button on toolbar, and observe the changes that will make in the class model. Enjoy!

Rate this Article

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

Comments (6)

written by Patrice on April 20, 2010

Hi,

I´ve just done this tutorial and it works fine until step 29, I can´t choose the menue for the "update-code"-entry. Is it possible that this future doesn´t work in the community edition of SDE ?

written by ? on April 20, 2010

This feature is available in Professional Edition or above

written by NOTO Hirosi on May 9, 2010

Hello! I am now following the tutorial, enjoying functional and beautiful panes on SDE EE-NB. I have a question in Step 21.
I wonder what the meaning of this step is. It seems that nothing happens or changes on the screen except that we have again confirmed Step 20.

Best regards,

May 9, 2010
NOTO Hirosi

written by Jick on May 10, 2010

Hi NOTO Hirosi,

Step 20, in terms of generated code, cause the attribute to be "static"
Step 21 cause the attribute to be "final" (constant)

There is no visual difference by performing step 21. But if you do not do that, the generated attribute will remain non final.

Best regards,
Jick

written by Robyn on May 26, 2010

Once I have saved a UML diagram and closed out...how do I reopen in Netbeans to make changes to my UML. I can open the project, but am not able to reopen as a diagram. Help!!

Thanks,
Robyn

written by ? on May 26, 2010

Please open the diagram navigator to re-open diagram. Please refer to step 3

Write comment

Rating

Comment

Enter the calculation result

security code