Inject New Methods and Properties into Classes During Runtime

前端 未结 5 1133
日久生厌
日久生厌 2020-12-06 12:22

Is there any way we can inject new methods and properties into classes during run-time.

http://nurkiewicz.blogspot.com/2009/09/injecting-methods-at-runtime-to-java.h

5条回答
  •  庸人自扰
    2020-12-06 13:07

    You can do some quite funky things with AOP, although genuine modification of classes at runtime is a pretty hairy technique that needs a lot of classloading magic and sleight of hand.

    What is easier is using AOP techniques to generate a subclass of your target class and to introduce new methods into this instead, what AOP called a "mixin" or "introduction". See here to read how Spring AOP does it, although this may be quite lame compared to what you're actually trying to achieve.

提交回复
热议问题