The Java Assembling Language

Jasml Home

User Guide

A Simple Example

Jasml Syntax


Java Macro Instructions

Java Macro Instruction Index




SourceForge.net Logo


A Simple Example

The example bellow will first create a simple java class with almost all of the supported attributes in it and printing "Hello World!", then decompile it into jasm source file and modify it to print "Halo world!".

1. Download Jasml package at here, unpackage it to c:\jasml. Then create a file named c:\testpackage\SimpleClass.java, and copy the content bellow into the file.

----------------------------- SimpleClass.java --------------------------------
package testpackage;
import java.io.IOException;
import java.io.Serializable;
public class SimpleClass extends Object implements Serializable {
public static String aStaticField;
public final int aFinalField = 5;
public final static int aFinalStaticField = 0;
public SimpleClass() throws IOException, Exception {
System.out.println("Hello World!");
int a = 0;
Runnable r = new Runnable() {
public void run() {
try {
} catch (Exception e) {
a = a++;
interface AnInnnerClass {
public static void main(String[] args) throws Exception{
new SimpleClass();

------------------------------end SimpleClass.java -------------------------------

2. Setting up the class path:

set classpath=c:\jasml\jasml.jar;c:\

3. Compile c:\testpackage\SimpleClass.java:

javac c:\testpackage\SimpleClass.java

Note, in some compilers, the LineNumberTable and LocalVariableTable and SourceFile attribute is not generated by default, such as the jdk 1.4.2_11. Look at the help doc of that compilers of how to generate these attributes. In jdk 1.4.2_11, to generate these attributes, you can use:

javac -g:{line,vars,source} c:\testpackage\SimpleClass.java

4. Decompile c:\testpackage\SimpleClass.class with jasm:

  java jasml -d -h -l -n c:\src.jasm c:\testpackage\SimpleClass.class

There will be a new generated c:\src.jasm, with the content like:

[Major : 46]
[Minor : 0]
public class testpackage.SimpleClass extends java.lang.Object implements java.io.Serializable{
public static java.lang.String aStaticField
public final int aFinalField = 5 [Deprecated]
public static final int aFinalStaticField = 0
public void <init> () throws java.io.IOException,java.lang.Exception{
line0 : aload_0 //Load reference from local variable
invokespecial void java.lang.Object.<init>() //Invoke instance method; special handling for superclass, private, and instance initialization method invocations
aload_0 //Load reference from local variable
iconst_5 //Push int constant 5
putfield int testpackage.SimpleClass.aFinalField //Set field in object
getstatic java.io.PrintStream java.lang.System.out //Get static field from class
ldc "Hello World!" //Push item from runtime constant pool
invokevirtual void java.io.PrintStream.println(java.lang.String) //Invoke instance method; dispatch based on class
iconst_0 //Push int constant 0
istore_1 //Store int into local variable
line19 : new testpackage.SimpleClass$1 //Create new object
dup //Duplicate the top operand stack value
aload_0 //Load reference from local variable
invokespecial void testpackage.SimpleClass$1.<init>(testpackage.SimpleClass) //Invoke instance method; special handling for superclass, private, and instance initialization method invocations
astore_2 //Store reference into local variable
line28 : aload_2 //Load reference from local variable
invokeinterface void java.lang.Runnable.run() 1 //Invoke interface method
line34 : goto line43 //Branch always
line37 : astore_3 //Store reference into local variable
line38 : iload_1 //Load int from local variable
iinc a(1) 1 //Increment local variable by constant
line42 : istore_1 //Store int into local variable
line43 : return //Return void from method

[LocalVariables :
java.lang.Exception e start=line38, end=line42, index=3
testpackage.SimpleClass this start=line0, end=line43, index=0
int a start=line19, end=line43, index=1
java.lang.Runnable r start=line28, end=line43, index=2]

start=line28 , end=line34 , handler=line37 , catch_type=java.lang.Exception]

[MaxStack : 3]
[MaxLocal : 4]
public static void main (java.lang.String[]) throws java.lang.Exception{
line0 : new testpackage.SimpleClass //Create new object
dup //Duplicate the top operand stack value
invokespecial void testpackage.SimpleClass.<init>() //Invoke instance method; special handling for superclass, private, and instance initialization method invocations
pop //Pop the top operand stack value
line8 : return //Return void from method

[LocalVariables :
java.lang.String[] args start=line0, end=line8, index=0]

[MaxStack : 2]
[MaxLocal : 1]

[InnerClasses :
access = abstract interface , name = AnInnnerClass , fullname = testpackage.SimpleClass$AnInnnerClass , outername = testpackage.SimpleClass
access = class , name = 0 , fullname = testpackage.SimpleClass$1 , outername = 0]

------------------------------end src.jasm-----------------------------------------

NOTE: some compilers may not generate the LineNumberTable and LocalVariableTable, so the file may look different.

5. Run the original SimpleClass, and see the out put:

java testpackage.SimpleClass

On the screen, "Hello World!" will be printed out.

6. Now let's try to modify the SimpleClass to print "Halo World!".

First, go to C:\testpackage and rename SimpleClass.class to SimpleClass.class1. (Do not change other .class files there, they are inner class definitions which is necessary to run the test.)

Then open c:\src.jasm, and search for "Hello World!", it's should be like :

ldc "Hello World!" //Push item from runtime constant pool

Change "Hello World!" to "Halo World!", save the file and leave everything else untoutched.

7. Compile jasm file into class file with

java jasml -c -n c:\testpackage\SimpleClass.class c:\src.jasm

There will be a new c:\testpackage\SimpleClass.class created.

8. Now run the SimpleClass again and see what's the output:

java testpackage.SimpleClass

Copyright 2006 - Jiang Yang , All Rights Reserved