Use cases for bytecode manipulation Code examples with Javassist How to brew a Java agent speakerdeck.com/antonarhipov github.com/zeroturnaround/callspy
Javassist: generate proxies Bytecode manipulation i.e. create a subclass at runtime that intercepts all method invocations java.lang.reflect.Proxy is not enough Hibernate proxies Spring AOP EJB proxies CDI proxies
Instrumentation inst) • Executed when the agent attaches to the running JVM • META-INF/MANIFEST.MF is required • Requires support for loading agents in JVM • Allows adding the code to JVM post-factum
VirtualMachine.attach("2177");! ! //get system properties in the target VM! Properties props = vm.getSystemProperties();! ! //load agent into the VM! vm.loadAgent("agent.jar", "arg1=x,arg2=y");! ! //detach from VM! vm.detach();! http://docs.oracle.com/javase/7/docs/jdk/api/attach/spec/com/sun/tools/attach/VirtualMachine.html @antonarhipov Prague, 24th October 2014