-
Notifications
You must be signed in to change notification settings - Fork 2
Plugin Development
Plugins that modify the generated classes. These can be plugins for example, adding fields and methods to the class, making the class java.io.Serializable, or adding specific annotations to the class or it's methods.
All Class Generator Plugins implement the ClassGeneratorPlugin interface:
public interface ClassModifyingPlugin extends Plugin {
/**
* Modifies the generation of a class.
*
* @param classBuilder A builder for generating the class
* @param classDefinition The definition of the class from the json schema document
* @param pluginContext Access to any data the plugin might need
*
* @return The modified class's {@link TypeSpec} builder
*/
TypeSpec.Builder generateWith(final TypeSpec.Builder classBuilder,
final ClassDefinition classDefinition,
final PluginContext pluginContext);
}
Plugins that modify return types and parameters of generated classes. For example these plugins can change the types of nullable properties in a class to java.util.Optional, support for java.util.UUID as return types and constructor parameters, or support for java.time.ZonedDateTime as return types and constructor parameters.
All Type Name Plugins implement the TypeNamePlugin interface:
public interface TypeModifyingPlugin extends Plugin {
/**
* Modifies the TypeName (used as constructor parameters and getter return types) of
* generated classes in some way.
*
* @param typeName The type name to be modified
* @param definition The FieldDefinition of the type to be modified
* @param pluginContext The {@link PluginContext}
*
* @return The modified type name
*/
TypeName modifyTypeName(
final TypeName typeName,
final Definition definition,
final PluginContext pluginContext);
}
Plugins that produce a root field name for the root schema from a file. The plugin can decide to parse the schema for a description field or parse the schema filename to create an appropriate name for the schema. Note: the field name is converted to produce the Class name at generation time, by upper casing the first character of the field name.
All Name Generatable Plugins implement the NameGeneratablePlugin interface:
public interface NameGeneratablePlugin extends Plugin {
/**
* Generates a schema field name for the root schema from the {@link Schema} and schema
* filename.
*
* @param schema the {@link Schema} to produce a field name for
* @param schemaFilename the schema filename to produce a field name for
* @return the root field name
*/
String rootFieldNameFrom(final Schema schema,
final String schemaFilename,
final PluginContext pluginContext);
}
Plugins can be constructed by the generator through a no argument constructor or an annotated factory method. The factory method must be static, be annotated with uk.gov.justice.generation.pojo.plugin.FactoryMethod and return an instance of the plugin. For example:
@FactoryMethod
public static CustomReturnTypePlugin create() {
...
return new CustomReturnTypePlugin(customReturnTypeMapper);
}
Plugins can be constructed that are incompatible with each other, for example the MakeClassSerializablePlugin and the SupportJavaOptionalsPlugin. To avoid both plugins being used together override the checkCompatibilityWith method from the Plugin interface.
public interface Plugin {
default void checkCompatibilityWith(final List<String> pluginNames) {}
}
This can be implemented to throw an IncompatiblePluginException, for example:
@Override
public void checkCompatibilityWith(final List<String> pluginNames) {
final String incompatiblePluginClassName = SupportJavaOptionalsPlugin.class.getName();
if (pluginNames.contains(incompatiblePluginClassName)) {
throw new IncompatiblePluginException(format(
"Plugin '%s' is incompatible with plugin '%s' and should not be run together",
getClass().getName(),
incompatiblePluginClassName));
}
}