Uml to Java Code

Automatic creation of complete class diagram from Java project

Out of the fact I am not sure to have all the classes of a project into one class diagram is a good idea because the result is unreadable with lot of classes, you can do that for instance with my tool BoUML. After you download/install/run it :

  • create a new project
  • select Java in the global menu Languages
  • for the first directory dialog appearing press the button cancel (you do not have a java catalog) then for the second directory dialog select the root directory containing all the sources of Java you want to model, then wait for end
  • in the browser on the left in any of the created class view or in a new one you create yourself do a right mouse click and choose New class diagram and double click on it to open it
  • probably you need to hide details of classes in the diagram to limit its size (you can do that later but better to do that right now in case you have lot of classes), in that case do a right mouse click into the diagram or on it into the browser to edit the drawing settings and set to yes the settings hide classes attributes and hide classes operations then confirm (button ok)
  • use the button binocular on the top (near print button), change kind to class then use buttons search then mark them then close
  • into the open diagram (shown into the right part of the window) do a right mouse click and choose add marked elements placing classes in random position then redo a right mouse click and choose automatic layout (you can also move the classes by yourself of course)

As you can see all the relations between classes are drawn, not only the generalization/realization. If you want only them without having for instance to hide all other relations one by one by hand you can develop a plug-out marking all the classes and generalization/realization of the model, then changing the procedure I given :

  • when you edit the drawing settings also go into the second tab and set to no the setting draw all relations
  • rather than to use the browser search (binocular button) to select all he classes use your plug-out

Anyway, again, to show all the classes into one diagram is a bad idea except if you have few.

In the page documentation you have the reference manual and (old) video tutorials including the two ones dedicated to Java and an other one about to write a plug-out


Note you can also use Doxygen to make your diagram without using an UML modeler

UML to Java code generation tool

As for me the greatest UML tool is ArgoUML. It is very powerful. Based on Java.
It constantly improved and become more and more useful tool.

Mapping UML diagram to Java code conversion

There is no standardized way of converting UML to Java, but I can tell you what is correct and not correct based on the UML and Java semantics.

An association between classes C1 and C2 may be implemented by a property in class C1, or by a property in class C2 or by properties in both classes. If an association does not have an arrowhead, all three options are possible and it is not defined which of these three options is the best. If an association is an arrow pointing from C1 to C2, then the first option is the best, the second option is incorrect and the third option is allowed. I have checked your Java code and it complies to these rules.

If class C1 has a property P implementing an association between class C1 and class C2 and the association has a multiplicity of 0..1 at the side of C2, then P should have type C2 and C1 should have a constructor that does not initialize P. Your Java code is incorrect, because you should not initialize Person.School.

If the multiplicity is * or 0..*, then P should be some kind of collection of C2. Class C1 should have a constructor that either does not initialize P or that can initialize P with an empty collection. The latter is the case in your Java code.

UML to Java code - Multiplicity Indicator and capitalized variable

What Geert said. Anyhow:

  • Ask the author of the UML why he capitalized AccountCounts. As you assumed it's a static local attribute.
  • Multiplicity can be expressed in many ways. If you have low multiplicities you can instantiate a var1, var2, etc. Or you use lists as you did.
  • The names you marked are names of the association (which I never found very useful for my modeling). They probably should be role names but they aren't. If so they need to be near the far end (here to the right) and have a visibility indicator (++, -, etc.) in front.

Java code which creates UML diagrams

Take a look at http://en.wikipedia.org/wiki/List_of_Unified_Modeling_Language_tools.

Some of the tools listed are in Java, some of them are open source and some of them are able to create UML diagrams in the XMI format.

If you want something light-weight, then Doxygen together with Graphviz can both reverse-engineer Java source code and automatically generate class diagrams with UML_LOOK

Java UML Diagrams to Classes

You forgot to do this:

public class Bear extends Animal {
// ...
}

I would recommend that you add constructors:

public class Animal {
private String name;
private int age;

public Animal(String name, int age) {
this.name = name;
this.age = age;
}

public String getName() [ return this.name; }
public int getAge() { return this.age; }
}

public class Bear extends Animal {
private int weight;

public Bear(String name, int age, int weight) {
super(name, age);
this.weight = weight;
}

public int getWeight() { return this.weight; }
}

You can do this, because Bear IS-A Animal:

Animal b = new Bear("Smokey", 10, 300);
System.out.println(b.getName()); // prints "Smokey"


Related Topics



Leave a reply



Submit