The Code Generator

The com.neeve.tools.AdmCodeGenerator class, included in Talon ADM module (nvx-rumi-adm-<version.jar), is the main code generator that facilitates code generation. The AdmCodeGenerator creates classes from a model, organizing them into a namespace-qualified directory structure based on a specified root directory. Optional parameters can be provided.

Talon also offers plugin for build tools, such as Maven, to integrate code generation with the build lifecycle of Maven based applications.

Encoding Types

All classes created by the code generator ensure efficient serialization for storage and network transfer. Talon supports various serialization formats, known as encoding types. The code generator suppirts a parameter to specify the desired encoding type for which the classes are to be generated.

The following are the encoding types currently supported by the ADM code generator:

  • Json: Javascript Object Notation serialization; not very efficient but highly readable

  • Protobuf: Google's protobuf serialization format, which can be useful for portability purposes providing a reasonable balance between performance and interoperability. The generated classes for this encoding type internally use the classes generated by the Google Protobuf IDL compiler.

  • Xbuf2: Talon's protobuf implementation. The classes generated by this encoding type support zero garbage operation, do not use the Google Protobuf IDL compiler and are 100% wire compatible with Google's protobuf wire format.

Deprecated Encoding Types

The Xbuf encoding format, which was a precursor to the Xbuf2 encoding format has been deprecated. Users of the Xbuf encoding format should migrate to the Xbuf2 encoding format. See Choosing an Encoding Type for more information.

See Choosing an Encoding Type for more information on choosing an appropriate encoding.

Mixing Encoding Types

You can mix different encoding types across models at runtime, provided each model has a unique factory id ((since the factory id is used to determine which generated factory to use to decode a message). However, it is not legal to mix and match encodings within a model via import. For example, if you generate one model with Protobuf, it is not possible to use types generated using that model in a model generated with Xbuf2.

Schema Location

The XML schema for the ADM language (for the input models) - xadml.xsd - can be found at the root of nvx-rumi-adm-<version>.jar

Generated Class Namespace

Each model is associated with a namespace. This namespace is the default package for generated classes from a code generator. Users can specify a different namespace when invoking the code generator, allowing generation of sources with varied encodings of the same model by rerunning the generator with distinct namespaces.

Imported Model Resolution

To handle multi-module and multi-project builds, the ADM XML Parser, and Maven Build plugin search for model imports on the classpath using the imported model's fully qualified namespace. The model xml (and .proto for Xbuf2/Protobuf) will be copied to the generated source and target classes folder in fully qualified form for inclusion in the project's jar.

Import resolution searches both current classpath and OS file system when resolving an import. Recommended is to use relative classpath strings whenever possible. Consider the following example project with two models, model.xml imports other_model.xml:

The recommended way to define import would be one of the following:

The reason why this works is that the result of running code generation would be:

The requirement is that code generation first runs for other_model.xml so that it can be found in classes folder at the time of running model.xml.

The following modes of import model resolution are provided for advanced use cases but are discouraged.

Generated Protobuf IDLs

The Xbuf2 and Protobuf code generators generate the following Protobof IDLs (.proto file).

  • Main IDL

    • This IDL is generated from the contents of the input model

  • Platform Bundled IDLs

    • descriptor.proto

    • AdmTypes.proto

Main IDL

The Main IDL is generated for use by the user as well as for internal use by the Protobuf code generator. The Xbuf2 code generator generates it solely for the user and does not use it internally.

The Main IDL is output in the generated source folder using the model's fully qualified namespace.

Platform Bundled IDLs

The code generator bundles the following IDLs in the packaged jars to support custom options used by ADM to enrich handling of enums, and to define additional data types used by ADM.

descriptor.proto

google/protobuf/descriptor.proto

Defines options available in Protobuf

AdmTypes.proto

com/neeve/adm/types/protobuf/AdmTypes.proto

Defines custom enum options and additional ADM data types

Build Tool Integration

Talon currently supports the use of following code generators

  • com.neeve.tools.AdmCodeGenerator

  • Maven Plugins (layered on the AdmCodeGenerator)

The following describes how these can be run with various build tools.

Java

ANT

Maven

When working with Maven, the following are the various options on how one can use the ADM code generator

  • Using the Maven Exec Plugin

  • Using the Maven X Platform Plugins

    • The ADM Plugin

    • The Platform Plugin

Using the Maven Exec Plugin

Using the Maven ADM Plugin

Plugin Goal
Description

generate, adm-generate

Generates code to a generated sources folder which would be included in the built jar.

generateTest, adm-generateTest

Generates code to a generated test sources folder which would be included in the built test jar

Using the Maven Platform Plugin

If your project is using the nvx-platform-bom and you want to generate code with the same version of the platform you are using, you may also use the nvx-platform-maven-plugin. The advantage of this approach is that you can use the same version of the plugin as the platform bom. Because maven BOMs don't cover plugin versions, using the nvx-adm-maven-plugin would mean that the nvx-adm-maven-plugin version would have to be specified separately.

Gradle, Ivy & Others

To create classes using the ADM code generator with build tools like Gradle, Ivy & others, invoke com.neeve.tools.AdmCodeGenerator using the appropriate mechanisms offered by these tools.

Code Generator Options

The following options are available for the ADM code generation:

Command Line
Maven Plugins (ADM & Platform)
Description
Default

-f, --file

modelFile

The input file specified either as OS path or URL

-

-m, --modelsdir

modelsDirectory

Base directory to use when searching for imported models that are not found on the project / generator's classpath.

-

-o, --outdir

projectOutputDirectory

Base output directory for the generated files.

For maven plugin defaults to target/generated-[test]-sources/nvx-adm

-

-c, --classesdir

classesOutputDirectory

Classes output folder (to which generated resoures should be copied). May be specified multiple times to copy to multiple directories.

-

-e, --encoding

encodingType

Encoding type of content underlying the generated classes (Xbuf | Protobuf | Json)

Protobuf

-x, --xpcompat

protoXbufGenerationCompatibility

Wire compatibility between protobuf and xbuf generated classes (None | Xbuf | Protobuf

Protobuf

-y, --emptyifnullarray

generateArrayGetterEmptyIfNull

Instructs the code generator return empty arrays instead of null for unset array fields

false

-n, --namespace

namespace

Namespace override of model parsed from the input file (overrides namespace in model file if supplied)

-

-p, --protodir

N/A

An additonal directory in which to search for imported .proto files.

-

-d, --directive

directives

A key=value pair specifying a code generation directive. (May be specified multiple times).

See Directives below.

-

-b, --buildinfo

buildInfo

String with build-time information such as project version, timestamp or machine. This is added to the AdmGenerated annotation of generated classes.

-

-i, --incremental

incrementalBuild

Trigger incremental code generation - run only if something changed since last run

false

(true for maven plugin)

N/A

useBasicDeltaDetection

When running an incremental build, basic delta detection indicates that model's are rebuilt based on whether source model timestamp. With advanced delta detection dependencies are examined as well.

false

-u, --bundledir

modelBundleOutputDirectory

Directory to which to output model XML and IDL files if applicable

-

N/A

generateModelBundle

Indicates whether models with all their dependencies and IDLs should be output to modelBundleOutputDirectory

false

N/A

codegenListenerClassName

Class name of external listener to receive events from code generator. See ADM Code Generation Events.

-

N/A

codegenListenerProperties

Additional properties to pass to the constructed code gen listener.

-

Code Generator Directives

Some advanced properties can be passed to the code generator as directives. The following is a list of supported directives:

Directive
Description
Default

requireExplicitCollectionKeys

Directive indicating that map collections' contained entities must define an explicit field field to store the key for the entity when it is in the map. When this directive is false, it is possible that as the model evolves the implicitly generated key field could change and cause existing keys to be ignored on upgrade.

💡 It is recommend that new projects set this directive to true.

SINCE 3.11

false

generateEmbeddedEntityInterfaces

Directive indicating that the generator should create interfaces for embedded entities. This can be disabled for applications with stringent performance requirements to reduce the overhead associated with multi-morphic vtable lookups.

true

generateEmbeddedEntitiesNonFinal

When this directive is set to true the generated entity class is not declared as final nor are its accessors. This feature can be useful for applications that need to mock embedded entities in test frameworks such as CGLIB, but is not recommended for production use for performance reasons.

SINCE 3.8.189

false

generateDefaultGetters

Whether or not to generate default getters that accept a value to return when the field is not set. Not typically recommended

false

generateThrowOnUnsetGetters

Whether or not to generate getXXXOrThrow() or accessors that will throw an ERogFieldNotSetException when the field has not been set. This provides an alternative to calling hasXXX for a field to test if the field is unset. Usage of this directive is not recommended; hasXXX is the recommended approach to testing if a field is not set. Exception throwing is more expensive, and the generated getXXXOrThrow method introduces extra invocation overhead and a larger code size.

false

generateRequiredFieldValidators

Whether or not validation logic is generated in the types validators for required fields. Enabling this leads large generated code size, and validation checks are expensive, so this is not recommended for performance sensitive applications.

false

generateFluentSetters

Directive indicating that fluent style setters should be generated for fields. This can be enabled to generate fluent accessors on generated types. This can be useful for writing concise test code, but is more overhead, so it's usage is not typically recommended.

false

generateAllStringsPoolable

Directive indicating that all Strings fields in the model should be generated as poolable types regardless of the value of the field's poolable attribute.

false

pooledStringFieldTypeNameSuffixPolicy

Can specify None, Always or OnConflict to instruct the code generator as to how to handle naming conflicts that arise from a pooled string field type name generated from a field name are suffixed to avoid a name clash.

"None"

pooledStringFieldTypeNameSuffix

Specifies the suffice to use to resolve pooled string type name conflicts with Always or OnConflict suffixing policies.

"String"

generateProtobufClasses

Specified that protobuf classes should be generated using the protoc code generator in addition to the encoding type specific generated classes. This directive only applies to Xbuf and Xbuf2 encoding types.

"String"

Code Generation Events

ADM code generation is run by using com.neeve.tools.AdmCodeGenerator class. It is possible to supply a listener to the AdmCodeGenerator instance to subscribe to events that are fired at certain points in code generation run. The events are in the AdmCodeGenerator.CodeGenerateEventType enumeration as follows.

An event listener can be supplied via the CODEGEN_EVENT_LISTENERS parameter to an instance of AdmCodeGenerator. The Listener interface is defined in AdmCodeGenerator as follows:

The Maven plugin exposes this capability via the codegenListenerClassName configuration parameter. This parameter accepts the fully qualified name of the class that implements the listener interface. While the AdmCodeGenerator can accept multiple listeners, the Maven plugin accepts only one class and will create only one instance of that listener class per execution. The class shpuld be in project's build classpath (either in project being built or in one of its dependencies).

Each event is dispatched with an instance of AdmCodeGenerator.CodeGenerateEvent that contains the data for the event. The following are the key methods in this class

Sample: Running Additional Model Validations

Developers may tap into listener mechanism to perform additional model validations as given in example below. The example demonstrates how to use the listener mechanism to enforce globally unique field ids (meaning both the model for which code is generated and all its imports recursively).

The listener is provided to the code generator as follows:

Incremental Code Regeneration

The Maven ADM/Platform Plugin and ADM Code generator take a source model's last modified timestamp or checksum into account. Code generation will be skipped if:

  • The model file has been changed since the last build.

  • Any import model file has changed (checked recursively in imports of imports...)

  • Any input option for code generation has changed such as encoding type, namespace, directives etc.

The incremental code generation works by tracking above given changes in an XML file which may be found in output dir. The file has a name with a pattern like this:

.${model_filename}.xml_${md5checksum}.metadata. model_name is the name of the model file for which code was generated. md5checksum is a signature calculated from input options given to code generator so that if any of them changes, the resulting filename no longer represents same code generation. Stored in this file are input options given to code generator and list of models with a number that would be different every time model file is persisted to disk. These files do not go into the jar and may be deleted at any time (which they usually do when a clean build is triggered).

Last updated