0

I am trying to de/seralize framework objects (no source code access) into JSON using jackson 2.

class Item {
    public Item(Long id) {}
}

I found this Add annotation to a parameter on a new method created with Javassist but this solution is based on JavaAssist and does not fully apply :(

The underlying issue is the lack of DefaultConstructors which can be solved using the @JsonCreator annotation together with a matching @JsonProperty annotation for the parameter.

@JsonCreator
class Item {
    public Item(@JsonProperty("id") Long id) {}
}

I managed to achieve this for one of the many item subclasses using a mixin class.

public abstract class ItemChildMixin {
    @JsonCreator
    public ItemChildMixin(@JsonProperty("objId") final Long objId) {
    }
}

However, writing mixin classes for all the relevant objects with almost the same content seems the wrong approach, so I started looking at aspects.

Adding the Annotation to the classes in the Item's hierarchy was easy:

aspect DeclareJsonCreatorAspect {
    declare @constructor: Item+.new(Long): @JsonCreator;
}

However, I cannot seem to find a way to add an annotation to the constructor parameters using Aspects! Aspectj in Action as well as google did not provide an answer yet. Is this at all possible?

Community
  • 1
  • 1
U.V.
  • 572
  • 4
  • 11

1 Answers1

0

Currently AFAIK AspectJ (currently v1.8.4) is unable to deal with annotations on method parameters, be it in pointcuts or in ITD (inter-type definition) statements.

I am sorry that I do not have any better news, but this is the status quo. If you have a chance to declare whole methods via ITD you can influence the full signature, but adding parameter annotations on existing methods is impossible today. You might also be able to also declare default constructors via ITD, if that helps. I am pretty sure there is a way to achieve what you want, just maybe not the way you imagine.

kriegaex
  • 63,017
  • 15
  • 111
  • 202