Inject New Methods and Properties into Classes During Runtime

前端 未结 5 1145
日久生厌
日久生厌 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 12:57

    Is it possible by just doing using Java?

    Quite so, the "only" thing you have to do is define an instrumentation agent which supplies an appropriate ClassFileTransformer, and you'll have to use reflection to invoke the added methods. Odds are this isn't what you want to do, though, but it's doable and there's a well-defined interface for it. If you want to modify existing methods you may be interested in something like AspectJ.

提交回复
热议问题