tag:blogger.com,1999:blog-3237092529299307504.post1332827031939867456..comments2024-03-19T05:58:41.543+01:00Comments on My daily Java: Make agents, not frameworksRafaelhttp://www.blogger.com/profile/06973690867431113468noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-3237092529299307504.post-71815282375158371512015-05-06T14:07:14.159+02:002015-05-06T14:07:14.159+02:00Thank you, I appreciate the feedback.
Yes, you ca...Thank you, I appreciate the feedback.<br /><br />Yes, you can intercept constructor calls just the same way as method calls. However, make sure to invoke the super constructor every time BEFORE you intercept the call. You can also do this afterwards, but then you cannot access the "this" instance in your interceptor.Rafaelhttps://www.blogger.com/profile/06973690867431113468noreply@blogger.comtag:blogger.com,1999:blog-3237092529299307504.post-32007476187421496842015-05-06T13:57:13.996+02:002015-05-06T13:57:13.996+02:00Awesome article! I really enjoyed reading it.
Do ...Awesome article! I really enjoyed reading it.<br /><br />Do you know if it is possible to use the same concept (ByteBuddyAgent.installOnOpenJDK) to intercept all new object creations. This way, I could do dependency injection in beans which are not managed by Spring.<br /><br />I know this can also be done using Spring and AOP but then you always have to declare an agent which makes development, unit testing e.d. much harder. (I know you can by default specify an agent in Intellij but still...). I really like the ByteBuddyAgent where you just say: ByteBuddyAgent.installOnOpenJDK<br /><br />Congrats and keep up the good work!<br />RonaldAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-3237092529299307504.post-61387167878421261512015-04-16T20:22:31.420+02:002015-04-16T20:22:31.420+02:00Thanks!
Byte Buddy renames the original method an...Thanks!<br /><br />Byte Buddy renames the original method and also copies the debugging information. The user does therefore not notice the instrumentation when setting breakpoint. The only trace is left by an additional synthetic method frame.<br /><br />By this approach it is possible to invoke the original method conditionally or multiple times.<br /><br />For using an agent in a unit test, Byte Buddy ships with a programmatic attacher, simply call ByteBuddyAgent.installOnOpenJDK().Rafaelhttps://www.blogger.com/profile/06973690867431113468noreply@blogger.comtag:blogger.com,1999:blog-3237092529299307504.post-87943202953890098022015-04-16T17:52:12.235+02:002015-04-16T17:52:12.235+02:00Great topic. I'm just getting into writing so...Great topic. I'm just getting into writing some class manipulation code. I have a few questions:<br /><br />1) When using AgentBuilder, is Byte-Buddy replacing the Service class bytecode with proxy class bytecode, or is injecting the logger code directly into the method?<br /><br />2) How does this effect debugging? I could see manipulation of bytecode interfering with the bytecode->source line mappings.<br /><br />3) How do you register the Agents when running unit tests? Do you hook into a specific Maven lifecycle? I could see this being tricky when running JUnit tests directly from eclipse. You'd have to register the agent jar in the Run Configurations > Arguments, but if you're modifying agent code you'd have to rebuild the jar.<br /><br />I'll poke around the Byte-Buddy code base, and maybe I can answer some of these questions myself.<br /><br />Overall great post. I initially was steering away from using Agents in my current project, but now I may take another look.Andy Glassmanhttps://www.blogger.com/profile/05455189906401279688noreply@blogger.comtag:blogger.com,1999:blog-3237092529299307504.post-25367944333649309552015-01-14T23:01:53.466+01:002015-01-14T23:01:53.466+01:00Thanks, this is very interesting and sounds useful...Thanks, this is very interesting and sounds useful, especially for DSL builders. I'll make a mental bookmark for future reference.Anonymousnoreply@blogger.com