Recently, I gave a series 4 talks (consecutive saturdays about 2 to 2.5 hours) at CS department of Anna University
on Java Virtual Machine. The aim was to give understanding JVM at specification level
and not much on any specific implementation of it.
I did not create slides on my own. Also, I didn't stick only to slides - I did
mostly marker-pen-and-board as well.
Writing code using a Java assembler gives very good understanding. I used
jasmin assembler to show
various JVM instructions. Also, we can see JVM's bytecode verifier in action:
.class public Adder
; standard constructor
.method public <init>()V
.method public static main([Ljava/lang/String;)V
.limit stack 2
.limit locals 2
; nothing on stack, try using "iadd"iadd
java -jar jasmin.jar Adder.j
Exception in thread "main" java.lang.VerifyError: (class: Adder, method: main
signature: ([Ljava/lang/String;)V) Unable to pop operand off an empty stack
Both the libraries can be used to read/analyze existing .class files and generate/modify .class files.
class can be supplied with a .class file. BCELifier generates a Java program
to generate the given .class file. By looking at the generated program, we can learn how to use BCEL API.
Command to generate a Java source program using BCELifier for a given "HelloWorld.class" (The generated Java program can be compiled and run to (re-)create HelloWorld.class).
java -cp bcel-5.1.jar org.apache.bcel.util.BCELifier HelloWorld.class
ASM library also comes with similar ASMifierClassVisitor
class to generate Java source.
java -cp $ASM_HOME/lib/asm-util-3.0.jar:$ASM_HOME/lib/asm-3.0.jar org.objectweb.asm.util.ASMifierClassVisitor HelloWorld.class