Enterprise Architect (EA) is great tool to model classic UML structural and behavioral diagrams. It is a multi-purpose tool that supports a variety of programming languages like Java, C#, Php or Visual Basic. In the article Teaching EA Standard Java I explained how to use all Java classes and interfaces of standard and enterprise Java.I will continue with articles on Enterprise Architect when trying to use some Java specific feature like exception or annotions. This time I will explain, how to model exception in EA and create valid Java when using the code generation feature of EA.

Throwing exceptions is Java specific and not common for other programming languages. EA can just support all UML standards for modelling and code generation. To create valid code with elements that are not part of the UML standard EA uses “tagged values”. To show a small example on how to use java exception in EA I created

  • a Page class (without attributes or methods) in package de.mekaso.webproject.data
  • a class PageNotFoundException (that extends Exception) in package de.mekaso.webproject.exception
  • an interface ContentService in package de.mekaso.webproject.service

The complete class model looks like this:

the class model
the class model

The service interface will get one method public Page findPageById(String pageId) throws PageNotFoundException;

To create the operation, enter the values just like on this picture (chose the types of the parameter and the return type from the EA context menu):

EA: add the method findPageById

So far we are in safe-mode and still in UML standard. To support a java exception we need to use the EA “tagged value” feature. Now select the method and add the advanced “tagged value” feature like this:

add throws PageNotFoundException as a “tagged value” in EA

In detail you have select the Tagged Values tab, select the Operation findPageById and click the Add tagged value button. In the opened dialog enter throws as tag and PageNotFoundException as value.

That’s how EA handles all non-standard features like Java exceptions or annotations (I will show how to handle annotations in a later post on this blog). Right now EA is able to create the method signature just as we want it, however when generating code from this model you will notice, that the exception class is not mentioned in the import statements of the generated class. EA cannot know, that it has to import a class for the tagged value we just entered. To let EA generate code for a valid Java class we need to modify the code generation for the service interface. Select the class and open the context menu and select Code Engineering – Generate Code…

customize the code generation for the service interface

Now we can add the missing import statement for EAs code generation:

add the missing exception class

That’s it. Now EA will generate valid Java code for us when creating service interfaces that throw some exceptions.

package de.mekaso.webproject.service;

import de.mekaso.webproject.data.Page;
import de.mekaso.webproject.exception.PageNotFoundException;

/**
 * @author Meik
 * @version 1.0
 * @created 03-Okt-2016 18:57:09
 */
public interface ContentService {

  /**
   * 
   * @param id
   * @exception PageNotFoundException
   */
  public Page findPageById(String id)
    throws PageNotFoundException;

}

 

By Meik Kaufmann

I am a certified oracle enterprise architect and like to share my knowledge and experience that I gain in the projects I am working on.

Leave a Reply

Your email address will not be published. Required fields are marked *