question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to include a custom annotation on generated classes

See original GitHub issue

I’d like to add the immutables annotation (@Value.Immutable) to generated classes. Are there any other options to do so besides modifying the jsonschema2pojo source and implementing a custom Annotator? Thanks!

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:14 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
ctrimblecommented, Jan 10, 2017

This is an older example, but here is a custom type annotator I was using to deserialize java.util.Set as java.util.LinkedHashSet, before this feature was merged into the project.

import java.util.LinkedHashSet;
import java.util.Set;

import org.jsonschema2pojo.AbstractAnnotator;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JFieldVar;

public class CustomTypeAnnotator extends AbstractAnnotator {

  @Override
  public void propertyField( JFieldVar field, JDefinedClass clazz, String propertyName, JsonNode propertyNode ) {
    super.propertyField(field, clazz, propertyName, propertyNode);

    if( field.type().erasure().equals(field.type().owner().ref(Set.class)) ) {
      field.annotate(JsonDeserialize.class)
        .param("as", LinkedHashSet.class);
    }
  }
}

I built this in its own module and then included it in my configuration:

        <plugin>
          <groupId>org.jsonschema2pojo</groupId>
          <artifactId>jsonschema2pojo-maven-plugin</artifactId>
          <version>0.4.4</version>
          <dependencies>
            <dependency>
              <groupId>ANNOTATOR_GROUP_ID</groupId>
              <artifactId>ANNOTATOR_ARTIFACT_ID</artifactId>
              <version>ANNOTATOR_VERSION</version>
            </dependency>
           ...
          </dependencies>
          <configuration>
            <customAnnotator>FULLY_QUALIFIED_ANNOTATOR_NAME</customAnnotator>
            ...
          </configuration>
          ...
        </plugin>

This was enough to get custom annotations on the java.util.Set fields. Hope this helps!

1reaction
joelittlejohncommented, Mar 9, 2016

Either of these methods could be used to add a class level annotation:

void propertyOrder(JDefinedClass clazz, JsonNode propertiesNode);
void propertyInclusion(JDefinedClass clazz, JsonNode schema);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Creating a Custom Annotation in Java - Baeldung
We're going to create three custom annotations with the goal of serializing an object into a JSON string. We'll use the first one...
Read more >
Java: Creating and Using Custom Annotations - Medium
Here is an example to demonstrate the use of custom annotation. It is about 2 classes, Car and Engine. Suppose we have a...
Read more >
Can I use custom annotations to classify Java classes?
Use annotations to make a map of value->class(es)? Sure that's possible. Just add an annotation handler and have it accumulate in a multimap, ......
Read more >
Customize Java Annotation with Examples - GeeksforGeeks
Using the same fashion, we create our second annotation to mark the fields that we are going to include in the generated JSON...
Read more >
Using Custom Annotation Processors - Apache NetBeans
The annotation processor used as the example generates a parent class for the annotated class. The generated parent class also contains a method...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found