Eclipse has a project called EMF (Eclipse Modelling Framework) cool for drawing UML2 Diagrams. But my goal in this post is to draw UML2 class diagrams and create the java skeleton for the classes.
In the application architecture is recommended separate the domain model from services, business logic etc. EMF gives you the tool to create class diagrams and to generate the code with them.
We can install EMF plugins in our Eclipse installation, or download the Modelling Eclipse bound. I recommend the second choice because is cleaner, at least to test.
I left in the following link the final product of this post: Eclipse UML2 to code project.
An now !! the show must start¡¡:Create a new project: File > New > Project… > Empty EMF Project
In the model folder we creates: New > Other… > Ecore Tools > Ecore Diagram.
We draw our model using the palette, and Properties view:
Is very intuitive to draw the diagram. In the association properties we put the numeral and mark “Is Containment” to do the composition association.
Now we are going to create a transition file .genmodel type. Select .ecore file from the diagram we want generate, and right button New > Other… > Eclipse Modeling Framework > EMF Generator Model.
In the file .genmodel we can generate the code as follows:
and …. !!! BOILA ¡¡¡ we have three packages:
Clicking in the last image you can see how ends the src folder:
- classdiagram: Containing the model with interfaces in order to decouple the design.
- classdiagram.impl: Model and Factory implementation to generate the classes.
- classdiagram.util: AdapterFactory implementation.
Furthermore the implementation of the model, it generates code to implement Dependency injection with factories. The code has the JavaDoc comments, you will not forget write them.
Puts all the getters/setters defined in the class diagram.
Every methods annotated with @generated, if you don’t wish the rewrote in the future you must delete the annotation.