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

Provided Plugins

Shaun Francis edited this page Sep 20, 2017 · 13 revisions

ClassModifyingPlugin

AddFieldsAndMethodsToClassPlugin

Plugin that adds fields, a constructor and get methods to a class's Type Specification. Without this plugin each class will be generated with no fields nor get 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 behavior.

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);
    }
}

However, as you can see, to parse this into java pojos requires the Jackson annotations @JsonAnySetter and @JsonAnyGetter

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;
       }
  }

CustomReturnTypePlugin

Adds support for using UUID or other custom types as return types and constructor parameters in the generated class.

To Use:

The java.util.UUID should be specified as the reference name in your json schema file:

      "myProperty": {
               "$ref": "#/definitions/java.util.UUID"
       },
       "definitions": {
               "java.util.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 java.util.UUID in the definition is important as this will be converted to the type of the field.

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;
               }
         }

NameGeneratablePlugin

RootFieldNameGeneratorPlugin

Generate the root class name from a schema filename.

The filename extension is removed, any prepending '.' are removed from the filename and then all '-' are removed and the first letter after each '-' is capitalized.

For example:

context.something-has-happened.json

becomes:

somethingHasHappened

Clone this wiki locally