Weaving in toString() implementation with AspectJ

前端 未结 1 1692
滥情空心
滥情空心 2021-01-12 02:06

Trying to weave in a default toString() method for a large number of DTOs, using compile-time weaving only. The goal is to return a JSON representation using the Jackson lib

1条回答
  •  生来不讨喜
    2021-01-12 02:20

    I tried your scenario and could replicate the behavior, I also tried combinations of @DeclareMixin instead of @DeclareParent and could not get that to work either. What worked for me though is to use native aspectj this way:

    public aspect JsonToStringAspect {
        private interface JsonToString {}
        declare parents: com.mycompany.dto.* implements JsonToString;
    
        public String JsonToString.toString() {
            return "Overridden String through JsonToStringAspect";
        }
    }
    

    I am guessing that this may not be feasible using @AspectJ and may be possible only through native aspects.

    0 讨论(0)
提交回复
热议问题