Skip to content
This repository has been archived by the owner on May 26, 2020. It is now read-only.

Provided Plugins

allanmckenzie edited this page Sep 19, 2017 · 13 revisions

ClassModifyingPlugin

AddFieldsAndMethodsToClassPlugin

Plugin that adds fields, a constructor and getter methods to a class's Type Specification. Without this plugin each class will be generated with no fields nor getter methods and with an empty constructor. For this reason this class is added by default; although it is possible to override it should you need to change it's behaviour.

GenerateBuilderForClassPlugin

Adds a builder for the class as an static inner class and a static method for accessing the builder. For example, a class MyClass specified with one property myProperty would be generated thusly:

  public class MyClass {

       private final String myProperty;

       public MyClass(final String myProperty) {
           this.myProperty = myProperty;
       }

       public String getMyProperty() {
           return myProperty;
       }

       public static Builder myClass() {
           return new MyClass.Builder();
       }

       public static class Builder {

           private String myProperty;

           public Builder withMyProperty(final String myProperty) {
               this.myProperty = myProperty;
               return this;
           }

           public MyClass build() {
               return new MyClass(myProperty);
           }
       }
   }

AddAdditionalPropertiesToClassPlugin

This plugin allows pojos to support additional properties in your json file. The json schema document has the property 'additionalProperties' on an Object.

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "myProperty": {
    "type": "string"
  },
  "additionalProperties": true
}

If this is set to true then it would be possible to have a json document that fulfils the schema, but would fail to be parsed into the java pojo. This plugin adds a java.util.Map as a field in your pojo with getters and setters for access. This is then parsable to and from json:

public class MyPojo {
    private final UUID myProperty;
    private final Map<String, Object> additionalProperties = new HashMap<>();

public MyPojo(final UUID myProperty) { this.myProperty = myProperty; }

public UUID getMyProperty() { return myProperty; }

@JsonAnyGetter public Map<String, Object> getAdditionalProperties() { return additionalProperties; }

@JsonAnySetter public void setAdditionalProperty(final String name, final Object value) { additionalProperties.put(name, value); } }

AddHashcodeAndEqualsPlugin

Add java Object.equals(Object) and Object.hashCode() to the generated class.
The hashCode and equals methods use Objects.hashCode(Object) and Objects.equals(Object, Object) under the hood.

NB: this plugin will not add Object.equals(Object) nor Object.hashCode() if the class definition contains no fields

MakeClassSerializablePlugin

A class plugin that adds Serializable interface and serialVersionUID to the generated class.

Note: the serialVersionUID is always set to 1.

For example:

  public class MyClass implements Serializable {

       private static final long serialVersionUID = 1L;

       private final String myProperty;

       public MyClass(final String myProperty) {
           this.myProperty = myProperty;
       }

       public String getMyProperty() {
           return myProperty;
       }
  }

TypeModifyingPlugin

SupportJavaOptionalsPlugin

A type modifying plugin for setting all properties which are not marked as required in the json schema document to be wrapped in a java Optional.

For example:

  public class MyClass {

       private final Optional<String> myProperty;

       public MyClass(final Optional<String> myProperty) {
           this.myProperty = myProperty;
       }

       public Optional<String> getMyProperty() {
           return myProperty;
       }
  }

SupportUuidsPlugin

Adds support for using {@see UUID} as return types and constructor parameters in the generated class.

To Use:

The uuid should be specified as a reference in your json schema file:

      "myProperty": {
               "$ref": "#/definitions/UUID"
       },
       "definitions": {
               "UUID": {
                   "type": "string",
                   "pattern": "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$",
               }
       }

The name UUID in the definition is important as this is how we specify that the types should be of type UUID.

This will generate the following code:

         public class MyClass {

               private final UUID myProperty;

               public MyClass(final UUID myProperty) {
                   this.myProperty = myProperty;
               }

               public UUID getMyProperty() {
                   return myProperty;
               }
         }

SupportZonedDateTimePlugin

Adds support for using {@see ZonedDateTime} as return types and constructor parameters in the generated class.

To Use: The ZonedDateTime should be specified as a reference in your json schema file:

           "myProperty": {
               "$ref": "#/definitions/ZonedDateTime"
           },
           "definitions": {
               "ZonedDateTime": {
                   "type": "string",
                   "pattern": "^[1|2][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$"
               }
           }

The name ZonedDateTime in the definition is important as this is how we specify that the types should be of type ZonedDateTime.

This will generate the following code:

       public class MyClass {

               private final ZonedDateTime myProperty;

               public MyClass(final ZonedDateTime myProperty) {
                   this.myProperty = myProperty;
               }

               public ZonedDateTime getMyProperty() {
                   return myProperty;
               }
       }

NameGeneratablePlugin

FieldNameFromFileNameGeneratorPlugin

Clone this wiki locally