Java bytecode manipulation with ObjectWeb ASM http://asm.ow2.org/current/asm-transformations.pdf
Java has the highest paying jobs Source: Previous lightning talk (circular reference)
Java Bytecode Manipulation Programming in hexadecimals Java Bytecode Manipulation
Not Really! The Java class file format is a bit convoluted So it is not fun to do it!
Introducing ObjectWeb ASM Eliminating headaches Introducing ObjectWeb ASM
What is it? A framework to manipulate Java class files ASM implements load-time bytecode weaving
Why use ASM? Easy to get started Uses an event driven approach Has an code generator Easy to get started
In particular, ASM uses the visitor pattern Getting started ASM uses an event driven model to hide the complexity of Java’s bytecode In particular, ASM uses the visitor pattern
Yet another pattern The Visitor Pattern
Visitor pattern is the separation of an algorithm from an object structure on which it operates on.
Why use the pattern? It allows the capture of class data incrementally Avoids the repeated creation and destruction of short-lived objects
Visitors in ASM ClassVisitor ClassReader ClassWriter ClassNode
Typical flow Input ClassReader ClassWriter Output Visiting event producer ClassWriter consumer Output
Typical flow Load class Read class Modify class
Example final ClassReader classReader = new ClassReader(classByteArray); final ClassNode classNode = new ClassNode(); classReader.accept(classNode, 0); final MethodNode methodNode = findMethod(classNode, methodInfo);
Introducing the ASMifier The Code Generator
The code generator ASM also has a code generator ASMifier It converts complied java code into its equivalent ASM function calls
Why use ASM? Easy to get started Light Weight Uses an event driven approach Has an code generator Easy to get started Very small runtime JAR file Low memory footprint Light Weight
Light weight library
ASM is lightweight Its runtime JAR file is only about 50 KB By using the visitor pattern, its memory footprint is also kept low
Why use ASM? Easy to get started Light Weight Powerful Uses an event driven approach Has an code generator Easy to get started Very small runtime JAR file Low memory footprint Many possible utilities Light Weight Powerful
Uses of bytecode manipulation Yes, it has uses Uses of bytecode manipulation
ReflectASM vs Java’s Reflection Credits: https://github.com/EsotericSoftware/reflectasm ReflectASM vs Java’s Reflection
Other possible utilities Call trace generation New language semantics implementation
By the way, what is ASM? No official meaning
In conclusion Java bytecode manipulation is not all that scary with ASM ASM is lightweight and incurs little performance overhead Bytecode manipulation is very powerful with lots of uses
PUMA Key Points It is easy to get started Audience: fellow CS3281/2 students Objective: Inform about a tool to manipulate java’s bytecode easily PUNCH: Everyone is a Java programmer, but not all know how to manipulate java’s bytecode. WIIFY: Learn how to use ASM to manipulate bytecode easily. Call to action: Know about this tool and consider using it next time Know: You don’t need to program in hexadecimals to manipulate bytecode Believe: It is easy with ASM Do: Start experimentation with it Key Points It is easy to get started Don’t need to use a hex editor to manipulate class files ASMifier ASM uses the visitor pattern It is how ASM manipulates and changes behaviour of classes without modifying them It is fast and lightweight It has many uses High performance reflections Debugging information (call trace)