Sep 25, 2012

How to generate UML Diagrams from Java code in Eclipse

UML diagrams compliment inline documentation ( javadoc ) and allow to better explore / understand a design. Moreover, you can print and bring them to table to discuss a design.

In this post, we will install and use the ObjectAid plugin for Eclipse to produce jUnit lib class diagrams. Then, we will be able to generate UML diagrams by simply dragging and dropping classes into the editor. We can further manipulate the diagram by selecting which references, operations or attributes to display. 
  • Open Eclipse and go to Help > Install New Software
  • Click on add to add a new repository
  • Enter name ObjectAid UML Explorer
  • Enter Location http://www.objectaid.net/update

Next, select the ObjectAid Class Diagram plugin - it is free - and click Next. The Sequence Diagram one requires a paid license.


Click Finish to confirm your choices and start the instalation process.
  • Click Ok on the security warning
  • Select Restart Now after the installation completes to restart Eclipse.

To create a new UML diagram we start the ObjectAid wizard with File > New > Other... and start typing in the textbox Class Diagram to filter the desired wizard. Click Next, and enter a directory and name for the diagram.


Drop java source or compiled class files into the visual UML editor to start populating the diagram. Right click on a generated class to bring up a context menu and adjust visibility, operations attributes, etc as you like.
Below, we see the Assert class from the jUnit library with all operations and fileds hidden.


From the context menu, we can add implementations and associations for a selected class. In the following screen, we add the interface Test implemented by the TestCase class.


This is how part of the jUnit UML class diagram look after adding some more classes.


To auto layout the diagram right click anywhere within the editor and select Layout Diagram. From the same menu, you can export the diagram to an image ( gif png jpeg ) by clicking the Save As Image... menu item




Have fun with the plugin, leave a comment if you like :)




21 comments:

  1. Gonna try this out on my project im taking over. Thanks a bunch!

    ReplyDelete
  2. Sorry mate, you are pursuing a pointless goal, UML documentation is a poster child for failed projects. Ask yourself how many teams at Apple / Google. Just to break the tension the answer is.... 'None that matter'

    ReplyDelete
  3. You have a point there.

    Well maybe in some cases UML skills needed signals over-engineering, over-design and bureaucracy, ending with unstable artifacts never following on the original it does everything UML design.
    But it could be useful when presenting an exisiting design / hierarchy to someone fresh in the project.

    ReplyDelete
  4. Snarking about UML is like snarking about agile or unit testing. They're all tools, and can be very useful in the right context but aren't the final state.

    I've found UML a good stenography for technical discussions between IT people designing complex interactions. It can help clarify design assumptions when building software requiring multi-party coodination.

    OTOH, it's nearly useless for conversations with business or non-technical people as more time is spent explaining the depictions than using them.

    Tools like this help keep UML in its sweet spot; code shorthand. Thanks for the article.

    ReplyDelete
  5. Yes, UML is nice to get a quick overview of the status quo of your model. If it can be extracted from code for ad-hoc documentation purposes, that's very nice. It should never be re-used or persisted, though. No design hardly ever lasts long enough as to justify drawing large and formal UML diagrams.

    ReplyDelete
  6. Hi there, awesome site. I thought the topics you posted on were very interesting. I tried to add your RSS to my feed reader and it a few. take a look at it, hopefully I can add you and follow.






    Java Training Courses

    ReplyDelete
  7. Thanks, I needed a tool like this. :)

    ReplyDelete
  8. wel donw boss .. really helpful..

    ReplyDelete
  9. Nice, but if UML is to big... when save... return this error... Unhandled event loop exception No more handles :(

    ReplyDelete
  10. Hm, maybe submit a bug via http://www.objectaid.com/contact.
    I looked up their FAQ and this seems to be the only way to submit bugs - no open issue tracker.

    ReplyDelete
    Replies
    1. its works good, thanks for your detailed steps provided here

      Delete
  11. Nice tutorial
    Thanks so much

    ReplyDelete
  12. the repository is www.objectaid.net/update

    ReplyDelete

Real Time Web Analytics