Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to include a custom annotation on generated classes #524

Closed
lgtout opened this issue Mar 9, 2016 · 14 comments
Closed

How to include a custom annotation on generated classes #524

lgtout opened this issue Mar 9, 2016 · 14 comments
Labels

Comments

@lgtout
Copy link

lgtout commented Mar 9, 2016

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!

@lgtout lgtout changed the title How to include an custom annotation on generated classes How to include a custom annotation on generated classes Mar 9, 2016
@joelittlejohn
Copy link
Owner

Hi, you can do this without modifying the source of the core project. If you implement a custom annotator you can simply include this on your classpath and activate it using the customAnnotator config option.

@lgtout
Copy link
Author

lgtout commented Mar 9, 2016

@joelittlejohn Thanks for the response. I've implemented a subclass of AbstractAnnotator. I want to apply the annotation at the class level. I've looked at a couple of the existing Annotator implementations, but they seem to be operating at the field level. Any suggestions for finding information on implementing class level annotations?

@joelittlejohn
Copy link
Owner

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

@lgtout
Copy link
Author

lgtout commented Mar 10, 2016

@joelittlejohn Perfect! Thanks.

@digitalbuddha
Copy link

@jaabio would it be possible to share your implementation I also use immutables and do a lot of manual fixing after generating my classes

@lgtout
Copy link
Author

lgtout commented Apr 20, 2016

@digitalbuddha sorry, i don't have access to that code anymore. and i only got as far as implementing the custom annotator. i didn't actually use the annotator because i had problems configuring my classpath. but other than than it was 2 or 3 lines of code. really simple.

@locofrank
Copy link

I'd love an example annotator -- I'm stuck trying to create one

@lgtout
Copy link
Author

lgtout commented Jan 10, 2017 via email

@ctrimble
Copy link
Collaborator

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!

@locofrank
Copy link

locofrank commented Jan 10, 2017 via email

@locofrank
Copy link

locofrank commented Jan 10, 2017 via email

@locofrank
Copy link

locofrank commented Jan 10, 2017 via email

@DiggidyDale
Copy link

@locofrank I was wondering if you show an example of your build.gradle as I am having the same issue but can't seem to get it to work

@krajkumar303
Copy link

krajkumar303 commented Aug 11, 2020

I have provided my approach on the below issue. #758

Add the jar as a dependency to the plugin which will resolve the classNotfound error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants