
h2(#CodeGenerationTutorial). Code Generation tutorial

The direct OCL to Java Code generator was a very experimental functionality for the Juno release. It has been substantially rewritten for Kepler. Optimisations will be activated for Luna.

In this tutorial we will continue the OCLinEcore tutorial and show how to get a direct Java representation of the Ecore model avoiding the need for run-time compilation.

h3. Load OCLinEcore Tutorial Example Project

All the material for this tutorial is available as part of the OCLinEcore Example project that you
may load by selecting *New* then *Example...* using the right button context menu of the Project Explorer. This
should give the *New Example* dialog in which you can select the OCL (OCL Constraint Language) Plugins and the OCLinEcore Tutorial.

h3. Direct code

In "Generating Java Code":#OCLinEcoreTutorial-genmodel we saw how to create a genmodel and how to generate code from it that realizes OCL as text strings in the Java implementation files. These text string are lazily compiled at run-time.

The easiest way to insert the GenAnnotation that enforces direct Java is to change the project property or workspace preference as described in "Code Generation Mode":#CodeGenerationMode. So change the setting to *Generate Java code in xxxBodies classes*.

Now open *Tutorial.genmodel*, select the root resource and invoke *Generate Model Code* to (re)generate the Java code. This will take somewhat longer as additional work items show that the OCL is being compiled and that Acceleo templates are generating additional Java code.

When generation has completed, open *MANIFEST.MF* and specify the missing dependency on *org.eclipse.ocl.examples.library* to make the errors should go away.

That is all there is to it. Your model code is now 100% Java; no OCL parsing is needed at run-time. 

h3. Using a GenAnnotation

Changing the default genmodel setting is a little dangerous since the change will affect any other genmodel activities you perform. It is therefore advisable to reset the setting to its default and use a GenAnnotation to embed the setting in the genmodel.

The easiest way to create the GenAnnotation that ensure direct code generation regardless of workspace or project preferences, is to paste the following three lines into your genmodel just above the @foreignModel@ or @genpackages@ element.

bc.. 
  <genAnnotations source="http://www.eclipse.org/OCL/GenModel">
    <details key="Use Delegates" value="false"/>
  </genAnnotations>
p. 

Of course, if you want to enforce delegation you should set the @value@ to @true@.

If you don't like cutting and pasting into XMI files, you can achieve the same effect with the GenModel editor by:

* Enable *Show Annotations* in the genmodel editors *Generator* toolbar menu.
* Invoke *Annotate* from the right button context menu of the genmodel root element
* Use the *Properties View* to set the GenAnnotation source to @http://www.eclipse.org/OCL/GenModel@
* Invoke *Add Detail* from the right button context menu of the GenAnnotation
* Use the *Properties View* to set the Detail key to @Use Delegates@
* Use the *Properties View* to set the Detail value to @false@

!{width:70%}images/4400-genannotation.png(GenAnnotation)!

A further "Use Null Annotations" GenAnnotation may be used to control whether @NonNull and @Nullable annotations are emitted in the generated code.

bc.. 
  <genAnnotations source="http://www.eclipse.org/OCL/GenModel">
    <details key="Use Delegates" value="false"/>
    <details key="Use Null Annotations" value="true"/>
  </genAnnotations>
p. 

