Java agent bytecode instrumentation with asm
Skill kjuhwa/skills-hub/skills/backend/java-agent-bytecode-instrumentation-with-asm
Build a JVM agent using ASM's ClassFileTransformer to inject profiling/monitoring hooks into application classes via pluggable visitor chainsFrom its SKILL.md
npx -y skills add kjuhwa/skills-hub --skill java-agent-bytecode-instrumentation-with-asmAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- 0 stars0 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
SKILL.md
2.2 KB, 396 tokens by cl100k_base, as published. Nobody here has run it
Java Agent Bytecode Instrumentation with ASM
Apply this skill when you need to observe or modify JVM behavior without touching application source — APM tracing, security policy enforcement, test coverage, feature flag injection.
Pattern
- Package as a
-javaagent:<jar>premain JAR withPremain-ClassinMANIFEST.MFandCan-Retransform-Classes: true. - Register a
ClassFileTransformerwhosetransform(...)dispatches per-target-class to composable ASMClassVisitorchains (one visitor per concern: field add, method wrap, try/catch injection). - Gate each visitor with a class-name / annotation / interface filter — don't visit everything; it's expensive and fragile.
- Handle JDK-dynamic classes separately (lambdas, method handles). Scouter uses a dedicated
LambdaFormTransformer. - Expose a config (flags, allow-list, hook-level) so unsafe hooks (async, lambda) can be disabled in production if they conflict with other agents.
Evidence
scouter.agent.java/src/main/java/scouter/agent/AgentTransformer.java— main dispatchscouter.agent.java/src/main/java/scouter/agent/asm/AddFieldASM.java— visitor examplescouter.agent.java/src/main/java/scouter/agent/LambdaFormTransformer.java— lambda handlingscouter.agent.java/readme.md
Pitfalls
- Lambda forms and synthetic classes bypass normal hooks. Always add a lambda-form transformer.
- Spring proxy classes conflict with naive class-name matchers — use runtime interface checks.
- Multi-agent conflicts: if your hooks wrap the same methods as another agent, order matters. Document compat.
Related knowledge
asm-based-classloader-instrumentation-caveats(pitfall)trace-context-async-execution-propagation(pitfall)
What ships with it
Read from the repository
Just SKILL.md. No reference files, no scripts.