Java Reference
In-Depth Information
M2M from a source model into a Java model, followed by Java generation using
dedicated templates. The alternative is to pass the source model to a set of tem-
plates designed to output Java code. In the former, the logical mapping from one
model to the other takes place in the mappings of QVT; in the latter, the logic
resides in Xpand and Xtend code throughout the templates and extension files.
Sometimes one approach is superior to the other; as with the two following
examples, both are feasible when transforming our Domain-Neutral Component
(DNC) models to Java Persistence API (JPA) code.
BEST PRACTICE
This might seen obvious, but it's typically easier to first write code that
works and templify it than to try to work in a template environment from
the beginning.This applies to all text output formats, not just Java.
7.3.1 Using Java Model and Dedicated Template
In this section, we develop the template used to generate Java code from our Java
EMF Model (JEM) instance. We use a single template, which you can see in its
entirety next, followed by the Xtend utilities used by the template.
The
Main
definition takes a collection of
JavaPackage
elements. As you
can see in the corresponding workflow file used to invoke the template, the
source model can contain multiple root elements, which explains the use of
Collection
. The package definition is invoked for each
JavaPackage
, which
expands each
JavaClass
that is not marked with a “
library
” annotation.
Subpackages are then processed recursively. The
jemUtil.ext
file contains the
functions used throughout to construct fully qualified class, package, and path
strings.
«
IMPORT
java»
«
EXTENSION
templates::java::jemUtil»
«
DEFINE
Main
FOR
Collection[java::JavaPackage]»
«
EXPAND
package
FOREACH this
.typeSelect(JavaPackage)»
«
ENDDEFINE
»
«
DEFINE
package
FOR
java::JavaPackage-»
«
EXPAND
class
FOREACH
javaClasses.select(c |
c.eAnnotations.first().source != 'library')»
«
EXPAND
package
FOREACH
eSubpackages.typeSelect(JavaPackage)»
«
ENDDEFINE
»
Search WWH ::
Custom Search