Adding your own code to a Gen2J application

Contents

What you can do
How to add your code to the generated application
Code file format
Example code file

Gen2J will generate all the standard "plumbing" for an application, leaving you free to concentrate on the specific business logic. This section describes how to add your own code to the generated application in order to implement your own specific requirements.

What you can do

Gen2J allows you to

Add code for your own methods

For any operation you have defined yourself in the one of the original classes in the input UML model, you can define the Java code which will be merged into the corresponding method of the class created by Gen2J.

Override code for methods created by Gen2J

For any method created by Gen2J, you can provide your own code to replace Gen2J's code.

The procedure is the same whether you are providing code for one of your own methods or one created by Gen2J.

How to add your code to the generated application

  1. Create an XML code file in your project directory for the code, for example code.xml

  2. For each method for which you want to provide code, add a method element, with the appropriate elements to define which method it refers to - see the “Code file format” section.

  3. Write the Java code as the content of each method element, as it would appear between the curly braces around the method body in a Java file.

  4. Set the code.import.file property for the Ant build. You can do this on the Ant command line:

    ant -Dcode.import.file=code.xml

    or you can add a property element to the project build.xml (refer to the Ant documentation for instructions on how to do this).

  5. Generate and build the application as described in the “Getting Started” section

Code file format

The code import file is an XML file. Its elements are described informally below, in order from the highest level to the lowest. All attributes are required unless they are marked optional.

ImportCode

This is the top-level element for the code XML.

Package

For each generated package in which there are classes where you want to provide code, there should be one of these elements.

Attribute: name - the fully qualified name of the package

Class

For each generated class in which there are methods where you want to provide code, there should be one of these elements, nested within the Package element for the package to which the class belongs.

Attribute: name - the name of the class (not qualified)

Method

For each method for which you want to provide code, there should be one of these elements, nested within the Class element for the class to which the method belongs.

Attribute: name - the name of the method

Attribute (optional): parmTypes - a space separated list of the parameter types. The names must be fully qualified class names.

The body of this element is the code for the method.

Example code file

The example below is taken from the Ecomm2J sample application.


          <?xml version="1.0" encoding="utf-8"?>

            <ImportCode>
              <Package name="com.ashridgetech.ecomm2j.customers.bean">
                
                <Class name="CustomerBean">
                  <Method name="getTotalOrderValue">
                    return 23.0;
                  </Method>
                </Class>
                
              </Package>
            </ImportCode>