Writing Java agents for fun and not so much profit
summary
Shows how to build a working Java agent that rewrites bytecode at load time using java.lang.instrument and implements ClassFileTransformer, uses Javassist to insert Java snippets rather than raw opcodes, and demonstrates a real agent that injects connection/read timeouts into multiple HTTP client libraries (demo proves timeouts fail or succeed as expected). Covers static vs dynamic attach (attach API is com.sun and breaks for same-JVM attach on Java 9+), modular classloader pitfalls, and an integration test harness that launches a JVM with the agent and captures output alongside the JaCoCo agent for coverage.
AI-written summary · about
chapters (14)
- 0:00Start
- 3:17Static agents
- 4:32Dynamic agents
- 6:25Class transformation
- 8:14Java bytecode
- 9:37Bytecode generation libraries
- 10:09Bytecode generation with Javassist
- 11:22Agent examples
- 13:32Mind the classloader
- 16:09OSGI - Weaving Hooks
- 17:08The challenges
- 18:34Bootstrapping the test
- 20:19OSGi integration testing
- 26:13When to use Java agents
related talks
01h 01mThe definite guide to Java agents
39mThe Java Agent: Modifying Bytecode at Runtime to Protect Against Log4J
45mMaking Java more dynamic
41mInstrument to Remove: Using Java agents for fun and profit
59mTools & Tips to Modernize Your Java code with AI
50mCode Coverage Instrumentation
24mAn Experiment in Continuous Deployment of JVM applications
55mLiving in the Matrix with Bytecode Manipulation
