Reverse Engineering: Creating UML diagrams with existing code

Posted on Monday, March 16, 2009 by Anuj Mehta

Very often it happens that whenever a new person joins the project the seniors gave him/her the design documents of the project and in most of the cases they tell “Hey there has been lot of changes in the original design and the class and sequence diagram that you see in this document is obsolete” and the poor chap has go through the arduous task of going through the source code to understand the design. This is a common problem in every project and but fortunately the reverse engineering feature of Netbeans comes to our rescue using which we can create UML diagrams of an existing code. Let’s look at a small example

I had defined an abstract class Person


abstract public class Person
{
protected String name;
protected boolean haveBrain;

Person(String name)
{
this.name = name;
}
protected abstract void display();
}



There are 2 classes SoftwareEngineer and Farmer which extends the class Person


public class SoftwareEngg extends Person
{
SoftwareEngg(String name)
{
super(name);
haveBrain = false;
}

@Override
protected void display()
{
System.out.println("Hi dude I am a Software engineer");
System.out.println("Name: " + name);
System.out.println("Do I have brain? " + haveBrain);
}
}




public class Farmer extends Person
{
Farmer(String name)
{
super(name);
haveBrain = true;
}
@Override
protected void display()
{
System.out.println("Ram ram bhaiya!");
System.out.println("Name: " + name);
System.out.println("Do I have brain? " + haveBrain);
}
}



Also I had created a Factory class that returns an object of Person


public class PersonFactory
{
public static Person getPerson(int ch, String name)
{
switch(ch)
{
case 1: return new SoftwareEngg(name);
case 2: return new Farmer(name);
}
return new Farmer(name);
}
}



Finally there is a class called PersonImpl that has the main method


public class PersonImpl
{
public static void main(String[] args)
{
Person per = PersonFactory.getPerson(1, "TheDude");
per.display();
per = PersonFactory.getPerson(2, "Ramlal");
per.display();
}
}



Steps for generating UML diagrams

1. I am using Netbeans 6.5. Go to Tools --> Plugins and in the Available plug-ins search for “uml” and install the UML package and restart the IDE.

2. Once the IDE restarts right click on the Project node (in the Project navigation tree) and select “Reverse Engineer” option. A new node -Model will be created and you will see something like this in output/log window

Begin processing Reverse Engineering


Parsing 5 elements

Analyzing attributes & operations for 5 symbols

Resolving 5 attribute types

Integrating 5 elements

Building the query cache.

================================
Task Successful (total time: 2 seconds)


3. Now the project navigator looks something like this




4. Now right click on sub node reverseengg (“reverseengg” is the package name of my original project) and select “Create Diagram From Selected Elements…”, a new dialog will pop up and you can select any diagram. For this case I will select Class diagram and boom! we get a nice class diagram showing the relation between various classes


5 Responses to "Reverse Engineering: Creating UML diagrams with existing code":

Sudeep Nadig says:

Dear,
Hiya, My name is Sudeep Nadig from Mumbai, India. So that I need your help for using UML diagrams for Use cases, Sequence cases and Class cases for project name is "Librarian from the process of borrowing books and media." I am currently using ArgoUML software application here.!! I am handicapped of hearing impaired that i m doing as trainee Software Engineer in Xoriant Co.
I have requirement to show you here: -
Requirements:

1. Search Media Without login
2. Login for the user
3. Categorize Users:(Media) - a) Technical.. b) Non Technical c) New Arrivals
4. After selecting the books pop ups windows showing title, description,author, issue time, comments , rating
5. display of comment, rating button
6. System based role identification
7. limiting the no of books a user can borrow
8. number of hold depending on the copies of a book available
9. display of detailed location of the book on click
10. scanning the barcode. Also manual entry for admin
11. issue of reciept containing: a ) Unique ID b)Title c) date of issue d) date of return
12. User history track form latest to oldest a) number of books per page b) reverse order display of list
13. Update of new arrivals
14. rules a) fine of Rs 5/- for delay so late b) lost book tag as lost penalize the user c) Borrowing rules (i) Technical books - 45 days - manager and 30 days - developer (ii) non technical books for double days
15. Developer can borrow 3 books , Manager cab borrow 5 books at a time.

I need your help this requirement for my project early as possible before submit next this Friday..
If you have problem then suggest to inform me

Thanks and Regards,
Sudeep
Xoriant Solution Ltd,Mumbai

Anuj Mehta says:

Hi Sudeep

This post is about generating UML diagrams from existing code via reverse engineering. In your case u r in initial design phase. Refer to any good book on UML probably UML distilled should be a good one to start with. You already had all the conditions hence it should not be difficult for you to create UML diagrams based on that

Sudeep Nadig says:

Hi Anuj,
So Please can you do create diagrams for use case, sequence cases and Class Cases until ur done and copy to doc file and send me by email as possible. there is my requirements re important for project that y need your help me. Please can u do favour it for me !!! So that our office will submit my assignments and project by monday.
Please contact me via email id Scorpion0085@gmail.com

Anonymous says:

Who knows where to download XRumer 5.0 Palladium?
Help, please. All recommend this program to effectively advertise on the Internet, this is the best program!

Unknown says:

Thank you so much, your code is really helpful.

java barcode plugin