CS-489 Enterprise Architect 7.1 Code Generation Hints: Java

by Dr. Durant

This document is based on prior work of Dr. Welch and Dr. Durant. See also:

Here are some things I learned while doing code generation in Enterprise Architect. I hope you find this useful. This is written as a tutorial outline. I recommend that you go through these features with a small test project (perhaps Order, Item, and CardNumber classes -- be sure to do something with a containment relationship) before applying it to a real project.

  1. Start by opening a new project in EA and adding a couple of classes to the default class view.
  2. Select Tools:Options
    • browse through some of the available options
    • select Source Code Engineering:Java
    • browse through the options
    • Select Collection Classes
    • Enter a container type you'd like to use for the various aggregation options. You probably want to use an interface like List<#TYPE#> so that you can choose a particular implementation later when you write the code. Note that #TYPE# is needed as is the placeholder for the contained type.
  3. Start Eclipse and create a new Java project for the EA model you're building. Don't create any classes.
  4. Go back to EA.
  5. Settings:Local Paths. Create a Java path for the source code directory for your new project. Give it a meaningful ID like "srcRoot". Make it point to the src directory of the Eclipse project you just created. Setting paths this way makes it easy to move projects across computers.
  6. In your class diagram, select all classes, right click, Code Generation | Generate Selected Elements.
  7. Go back to Eclipse. Highlight "src" in the Package workspace (normally left pane). Hit F5 to get Eclipse to recognize the generated source.
  8. Make some edits in Eclipse. Add at least 1 class. Add at least 1 method to any class. Perhaps delete a method, etc.
  9. Go back to EA. Select all your classes and right-click, Code Generation | Synchronize Selected Elements. (If you know you've only made changes to 1 or a small number of classes, you can select just them.) Notice that various details have been filled in by the synchronize - e.g., constructors added during generation.
  10. For any new classes that you created in Eclipse, right click on the diagram and Import from source file(s) | Java. (It may be easier to create the clases and do the initial generate from EA, but both methods do work.)
    To add an attribute/method from EA, remember to first synchronize, then add/modify in EA, and then generate.
  11. If you want to put your classes in to a package structure, add a "Package" to the class diagram and give it an appropriate name. You'll need to create each package level separately (e.g., "edu.msoe.logic" requires creation of 3 packages). Drag and drop in the project browser to set up the right hierarchy among the package elements (e.g., "edu" contains "msoe" contains "logic"). Drag the desired classes into the appropriate package in the Project Browser. To generate code, right-click on the root package in the Project Browser (usually docked in the upper right of EA), select Code Generation | Generate Package Source Code. Enable "Include all Child Packages". Enable "Auto Generate Files" and select your src root for your Eclipse project.
  12. In Eclipse, again select "src" and hit F5 (Refresh) to see the classes in the new package. To clean up previous .java files delete them from Eclipse.
  13. Remember to synchronize from EA after making any changes in Eclipse. Back up your code from time to time.
  14. Note that documentation is 2-way between EA and Eclipse. Your notes for methods, for example, go into JavaDoc comments and vice versa.
  15. Caveats: imports are not automatically generated by EA. You'll need to add them in Eclipse.