Introduction: The Listener API
Code generation for ADM is run by using com.neeve.tools.AdmCodeGenerator
class. It is possible to supply listener to AdmCodeGenerator
instance to subscribe to events that are fired at certain points in code generation run. The events are given in AdmCodeGenerator.CodeGenerateEventType
enum which is defined as shown below.
Event listener can be given to AdmCodeGenerator
instance directly through CODEGEN_EVENT_LISTENERS
parameter. Maven plugin exposes it through codegenListenerClassName
configuration parameter. This parameter accepts full qualified name of the class that implements listener interface.Listener interface is defined in AdmCodeGenerator.CodegenListener
as shown below. So, while AdmCodeGenerator can accept multiple listeners, maven plugin accepts only one class name 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).
The event data is passed in AdmCodeGenerator.CodeGenerateEvent
class instance. the interesting methods from event objects are given below.
Example Usage: 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).
In pom.xml we may use this listener as follows: