8

We're using @JacksonAnnotationsInside and would like to inject a property from the classes using the meta annotation.

i.e. we have a meta annotation with @JsonTypeInfo() and would like to inject the defaultImpl via the aggregating annotation.

Here is the annotation I'm trying to use:

@Inherited
@JacksonAnnotationsInside
@Retention(RetentionPolicy.RUNTIME)
@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="@class") //, defaultImpl=defaultType())
public @interface PolymorphismSupport {
    //@AliasFor("defaultImpl") ...
    Class<?> defaultType() default Object.class;
}
dev_feed
  • 689
  • 2
  • 7
  • 25

1 Answers1

3

The AliasFor like support is not available in Jackson.But as an workaround we can modify the consumption of metadata supplied by annotation by extending JacksonAnnotationIntrospector.

What you are trying to achieve can be done by providing an custom JacksonAnnotationIntrospector which will supply the default implementation from the PolymorphismSupport annotation.

@Inherited
@JacksonAnnotationsInside
@Retention(RetentionPolicy.RUNTIME)
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@class")
public @interface PolymorphismSupport {

    Class<?> defaultType() default Object.class;

}

public class CustomAnnotationIntrospector extends JacksonAnnotationIntrospector {

    @Override
    protected TypeResolverBuilder<?> _findTypeResolver(MapperConfig<?> config, Annotated ann, JavaType baseType) {
        TypeResolverBuilder<?> b = super._findTypeResolver(config, ann, baseType);
        PolymorphismSupport support = _findAnnotation(ann, PolymorphismSupport.class);
        if (null != b && null != support) {
            b.defaultImpl(support.defaultType());
        }
        return b;
    }
}


public class CustomObjectMapper extends ObjectMapper {

    public CustomObjectMapper() {
        setAnnotationIntrospector(new CustomAnnotationIntrospector());
    }
}

The only downside with this approach is that you have to register the introspector into the object mapper when initializing.

Raghu
  • 909
  • 7
  • 23
  • Thanks, that should work! We already had to extend the object mapper for various other reasons, so this will fit in with that paradigm. The JacksonAnnotationIntrospector should come in handy later on, too. – dev_feed Jun 11 '18 at 12:57
  • @Raghu What about other annotations(eg: `@JsonFormat(pattern="p")@interface Example {String jsonPattern() default "";}`)? – xmcx Mar 14 '22 at 07:50
  • @西门吹雪 Sorry didn't get you? – Raghu Mar 14 '22 at 09:36
  • @Raghu I mean add other annotation and method (like: `JsonFormat` + `String dateFormat() default ""`) at `PolymorphismSupport`(or create a new annotation). Or I ask a new question. – xmcx Mar 14 '22 at 09:53
  • @西门吹雪No need for another annotation. `JacksonAnnotationIntrospector` should be able to handle it. If you want a solution for your specific use case you can raise a new question and I'll happily answer it. – Raghu Mar 14 '22 at 13:34
  • @Raghu I have solved it, override the method related to annotation in `JacksonAnnotationIntrospector`. eg, In this question is `_findTypeResolver`, for `JsonFormat` is `findFormat`. – xmcx Mar 17 '22 at 08:37