Monday, October 30, 2006

Reverse Engineering of UML Class and Sequence Diagrams from Java

About a year ago I had to stop work on an interesting project. There were just too many projects going on at one time, not enough resources to work on them all, and the other projects were more important from a political point of view. The project involved our B2B application making a call to a service provided by a 3rd party outside the firewall. One of the developers had built some great Java code to do this right before we stopped work.

I recently picked that project back up again and needed to dig into that code, understand how it worked, and create some design documentation for the overall project. I also had just recently made a conscious effort to upgrade my object modeling tool from Rational XDE to Rational Software Architect (RSA).

I decided I would start by reverse engineering the Java code to create a class diagram. I had done this numerous times as a Rational Rose and Rational XDE user in the past so I had no worries. I managed to import the source code and began fumbling through the menu options looking for reverse engineering features. I got a sinking feeling at first that someone at Rational had taken the reverse engineering features away. I found numerous transformations for forward engineering from UML class diagrams into something else (like Java). It was not obvious, however, what to do if I already had the Java code and wanted to go in the reverse direction. I had a sinking feeling inside. Had the Model Driven Development / Model Driven Architecture adherents at Rational lost their minds? Had they taken those reverse engineering features away on purpose? Was this a heavy handed means of forcing the whole world to adopt a “forward engineering only” view of the application development process?

Luckily, I was able to determine that Rational had not lost its collective mind. Yes, it was indeed possible to reverse engineer code into a diagram and as I got more into it, I decided it was actually an improvement.

I highly recommend a meaty white paper I found on developerWorks called The new IBM Rational design and construction products for Rose and XDE users” by William T. Smith, a Product Manager supporting model driven development at IBM Rational brand software. From it I discovered that many reverse engineering capabilities were available in RSA (as well as Rational Software Modeler (RSM)) and they appear to be significant improvements over what I was used to in Rose and XDE. There is a great deal of new terminology to get used to, however.

Here is a quick summary of what I learned about reverse engineering using RSA

  • I can select Java classes of interest with my mouse and quickly reverse engineer a class diagram by “visualizing” a “Topic Diagram”. This diagram can be synchronized with code as the code is modified.
  • I can “harvest” classes from the topic diagram and paste them into a separate UML diagram.
  • I can mix “pure” UML classes and harvested classes on the same diagram and the tool will mark classes reverse engineered from code with a glyph in the upper left hand corner.
  • There is a new “Browse Diagram” capability which allows the user to explore existing code by quickly generating class diagrams “on the fly” showing classes related to a selected class. Selecting a different class on this diagram will generated yet another diagram on the fly with all classes related to that class. This quick navigation from one diagram to another is a great way to “get to know” code someone else wrote. You can control which kinds of relationships are included when the class diagrams are created and how many levels (up to 3) removed from the selected class in the center of the diagram. I made great use of this on my project.
  • (Drum roll please!) I can reverse engineer Java method bodies into UML Sequence Diagrams by “visualizing” a “Static Method Sequence Diagram.” This type of program understanding tool has long been missing from UML modeling tools. It will be a windfall to any poor soul who must maintain/modify poorly documented Java code created by someone else who is perhaps no longer around. The only downside is that if the method makes any calls to low-level Java classes like “Integer” those method calls will clutter the generated diagram. But.. think of the time you’ll save!

Together, these reverse engineering capabilities will just about take away the last excuses for not having design documentation match the “as implemented” state of the Java code. It really is easy to create both class and sequence diagrams and rapidly document both the static and dynamic behavior of “as is” Java software.

(Dec 2007) English not your native language? I've begun making podcasts of popular posts and they are available at http://artsciita.podbean.com/. Listen online at that URL, with the MP3 player below, or subscribe to the podcast using the RSS feed and listen with your favorite MP3 player.



Copyright © 2006 by Philip Hartman - All Rights Reserved
The postings on this site are my own and don't necessarily represent IBM's positions, strategies, or opinions.

6 comments:

Selrahc's Gold said...

Congratulation on figuring this out. Bill's paper is certainly very good at that. One more advantage of our visualisation approach as opposed to the "reverse engineering" of previous tools is that by using the visualised classes, your code and model can not get out of sync! Unless, of course, you harvest, but that capability is more to drive the next iteration...

You should also take a look at the new transformation capabilities (both way) that will be part of the next version of RSA. The open beta is now available on the IBM web site.

shauny said...

I read your article with much interest - have been working with UML and RR for some years now, and always found challenges using reverse engineering (I now work for a UML execution company - much easier!). Just thought I'd say - fascinating article - I've never managed it without large java "snippets" all over the place!

Regards

Shaun Conn
Technical Director, E2E UK
Rouse Business Centre, 55 Station Road, Beaconsfield, Buckinghamshire HP9 1QL
M: +44 (0) 7799 062 620 P: +44 (0) 1494 731 816 F: +44 (0) 1494 731 802

E2E - Simply integrated. http://www.e2e.ch

ghostrocket said...

Hi -

A google search for "sequence diagram reverse engineering" led me to your blog. I'm looking for a tool that does exactly what you describe [actually netbeans has this built in now] BUT easily elimantes calls to Integer or String or what have you. if I could just ignore pretty much anything in the java.* library, i would have a perfect and readable sequence diagram. Any thoughts on a tool that will allow you filter that out?

dontaskmywhyitisblank said...

I'm working on an open source application for generating sequence diagrams directly from java application runtime.
If you're interrested, you can take a lookat it here: http://jtracert.googlecode.com/

Unknown said...

See http://www.reversejava.com for a dynamic reverse engineering application which generates UML Sequence diagram and view of Participating Class diagram from any Java Application at runtime
All you have to do is just run your application and sit back. Reverse Java runs in background tracing all the activities happening inside your application and creates UML diagram for you.

Unknown said...

See http://www.reversejava.com for a dynamic reverse engineering application which generates UML Sequence diagram and view of Participating Class diagram from any Java Application at runtime
All you have to do is just run your application and sit back. Reverse Java runs in background tracing all the activities happening inside your application and creates UML diagram for you.