Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java bytecode manipulation

Similar presentations


Presentation on theme: "Java bytecode manipulation"— Presentation transcript:

1 Java bytecode manipulation
with ObjectWeb ASM

2 Java has the highest paying jobs
Source: Previous lightning talk (circular reference)

3 Java Bytecode Manipulation
Programming in hexadecimals Java Bytecode Manipulation

4 Not Really! The Java class file format is a bit convoluted So it is not fun to do it!

5 Introducing ObjectWeb ASM
Eliminating headaches Introducing ObjectWeb ASM

6 What is it? A framework to manipulate Java class files
ASM implements load-time bytecode weaving

7 Why use ASM? Easy to get started Uses an event driven approach
Has an code generator Easy to get started

8 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

9 Yet another pattern The Visitor Pattern

10  Visitor pattern is the separation of an algorithm from an object structure on which it operates on.

11 Why use the pattern? It allows the capture of class data incrementally Avoids the repeated creation and destruction of short-lived objects

12 Visitors in ASM ClassVisitor ClassReader ClassWriter ClassNode

13 Typical flow Input ClassReader ClassWriter Output Visiting event
producer ClassWriter consumer Output

14 Typical flow Load class Read class Modify class

15 Example final ClassReader classReader = new ClassReader(classByteArray); final ClassNode classNode = new ClassNode(); classReader.accept(classNode, 0); final MethodNode methodNode = findMethod(classNode, methodInfo);

16 Introducing the ASMifier
The Code Generator

17 The code generator ASM also has a code generator ASMifier
It converts complied java code into its equivalent ASM function calls

18 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

19 Light weight library

20 ASM is lightweight Its runtime JAR file is only about 50 KB By using the visitor pattern, its memory footprint is also kept low

21 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

22 Uses of bytecode manipulation
Yes, it has uses Uses of bytecode manipulation

23 ReflectASM vs Java’s Reflection
Credits: ReflectASM vs Java’s Reflection

24 Other possible utilities
Call trace generation New language semantics implementation

25 By the way, what is ASM? No official meaning

26 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

27 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)


Download ppt "Java bytecode manipulation"

Similar presentations


Ads by Google