-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Comments
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. |
@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? |
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); |
@joelittlejohn Perfect! Thanks. |
@jaabio would it be possible to share your implementation I also use immutables and do a lot of manual fixing after generating my classes |
@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. |
I'd love an example annotator -- I'm stuck trying to create one |
I'm sorry, I no longer have access to that code :(
…On Tue, Jan 10, 2017 at 6:56 AM Frank Stanbach Jr. ***@***.***> wrote:
I'd love an example annotator -- I'm stuck trying to create one
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#524 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAWQ_wjtqJsg-1Xf-zkkTA0VtBh4Ewfdks5rQ5wzgaJpZM4HtNYj>
.
|
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.
I built this in its own module and then included it in my configuration:
This was enough to get custom annotations on the java.util.Set fields. Hope this helps! |
Thank you - it took me a minute to realize that this has to be created
outside of my project and then included somehow.
…On Tue, Jan 10, 2017 at 10:53 AM, Christian Trimble < ***@***.***> wrote:
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!
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#524 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AV3BthiWdm6N4CQP8QAMyvXIUP9FOfjNks5rQ9O7gaJpZM4HtNYj>
.
|
I've created my Annotator (just a test version) and now I'm getting class
not found from jspnschema2pojo.
how did you modify the gradle file to add your annotator to the classpath?
where did you add your jar?
Thanks,
Frank
On Tue, Jan 10, 2017 at 10:59 AM, Frank Stanbach <fstanbach@locoroll.com>
wrote:
… Thank you - it took me a minute to realize that this has to be created
outside of my project and then included somehow.
On Tue, Jan 10, 2017 at 10:53 AM, Christian Trimble <
***@***.***> wrote:
> 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!
>
> —
> You are receiving this because you commented.
> Reply to this email directly, view it on GitHub
> <#524 (comment)>,
> or mute the thread
> <https://github.com/notifications/unsubscribe-auth/AV3BthiWdm6N4CQP8QAMyvXIUP9FOfjNks5rQ9O7gaJpZM4HtNYj>
> .
>
|
solved that by putting this in build.gradle
classpath files("${project.rootDir}/LocoAnnotator.jar")
On Tue, Jan 10, 2017 at 1:58 PM, Frank Stanbach <fstanbach@locoroll.com>
wrote:
… I've created my Annotator (just a test version) and now I'm getting class
not found from jspnschema2pojo.
how did you modify the gradle file to add your annotator to the classpath?
where did you add your jar?
Thanks,
Frank
On Tue, Jan 10, 2017 at 10:59 AM, Frank Stanbach ***@***.***>
wrote:
> Thank you - it took me a minute to realize that this has to be created
> outside of my project and then included somehow.
>
> On Tue, Jan 10, 2017 at 10:53 AM, Christian Trimble <
> ***@***.***> wrote:
>
>> 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!
>>
>> —
>> You are receiving this because you commented.
>> Reply to this email directly, view it on GitHub
>> <#524 (comment)>,
>> or mute the thread
>> <https://github.com/notifications/unsubscribe-auth/AV3BthiWdm6N4CQP8QAMyvXIUP9FOfjNks5rQ9O7gaJpZM4HtNYj>
>> .
>>
>
>
|
@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 |
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. |
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!
The text was updated successfully, but these errors were encountered: