11

In my code, I'm going to have lots of getters like this with the same set of annotations (one for Hibernate and others for Jackson):

@Transient
@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = BaseEntity.JSON_DATETIME_FORMAT)
public LocalDateTime getCreatedDateDT() {
    return DateTimeUtils.klabTimestampToLocalDateTime(getCreatedDate(), createdDateDT);
}

I want to minimize repetion by implementing my own annotation, which will mean all these four together, like this:

@TransientLocalDateTime
public LocalDateTime getCreatedDateDT() {
    return DateTimeUtils.klabTimestampToLocalDateTime(getCreatedDate(), createdDateDT);
}

How can I do it? Is this even possible?

UPDATE Thanks to Konstantin Yovkov, now I know, that I can combine all Jackson annotations in one, but that is because Jackson's annotation processor recognizes such a trick. I wonder if it is possible to make any annotation processor do that? It seems to me that it's not.

1 Answer 1

10

Jackson provides a meta-annotation (annotation used for annotating another annotation), called @JacksonAnnotationsInside, which is a:

Meta-annotation (annotations used on other annotations) used for indicating that instead of using target annotation (annotation annotated with this annotation), Jackson should use meta-annotations it has.

This can be useful in creating "combo-annotations" by having a container annotation, which needs to be annotated with this annotation as well as all annotations it 'contains'.

So, you should create an annotation like this one:

@Target(value = { ElementType.TYPE, ElementType.METHOD,
                  ElementType.PARAMETER, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside // <-- this one is mandatory
@Transient
@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = BaseEntity.JSON_DATETIME_FORMAT)
public @interface TransientLocalDateTime {    
}

and use is like:

@TransientLocalDateTime
public LocalDateTime getCreatedDateDT() {
    return DateTimeUtils.klabTimestampToLocalDateTime(getCreatedDate(), createdDateDT);
}
4
  • This is helpful, but can it be used for arbitrary annotations or for Jackson's annotations only? It seems @Transient is not allowed here. Commented Feb 5, 2016 at 12:31
  • 1
    I can be used for arbitrary annotations, but can only be processed by the Jackson annotation processor ... unless you write a custom annotation processor, but then I believe you could just introduce a brand new annotation (and not use @JacksonAnnotationsInside ), which is processed by your annotation processor. Commented Feb 5, 2016 at 12:37
  • Also, I get @TransientLocalDateTime not applicable to method error in my IDE. Commented Feb 5, 2016 at 12:37
  • 2
    For the latter error, just set it with @Target(value = { ElementType.TYPE, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD }) to be able to use it on every possible element. Commented Feb 5, 2016 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.