The following program reads a name from the standard input and
prints a friendly "Hello". Since the readLine() method may
throw an IOException it is enclosed by a try-catch
clause.
We will sketch here how the above Java class can be created from the
scratch using the BCEL API. For
ease of reading we will use textual signatures and not create them
dynamically. For example, the signature
actually be created with
Initialization:
First we create an empty class and an instruction list:
We then create the main method, supplying the method's name and the
symbolic type signature encoded with Type objects.
We now define some often used types:
Create variables in and name: We call
the constructors, i.e., execute
BufferedReader(InputStreamReader(System.in)). The reference
to the BufferedReader object stays on top of the stack and
is stored in the newly allocated in variable.
Create local variable name and initialize it to null.
Create try-catch block: We remember the start of the
block, read a line from the standard input and store it into the
variable name.
Upon normal execution we jump behind exception handler, the target
address is not known yet.
We add the exception handler which simply returns from the method.
"Normal" code continues, now we can set the branch target of the GOTO.
Printing "Hello":
String concatenation compiles to StringBuffer operations.
Finalization: Finally, we have to set the stack size,
which normally would have to be computed on the fly and add a
default constructor method to the class, which is empty in this
case.
Last but not least we dump the JavaClass object to a file.
This class implements a simple peephole optimizer that removes any NOP
instructions from the given class.
If you want to learn how certain things are generated using BCEL you
can do the following: Write your program with the needed features in
Java and compile it as usual. Then use BCELifier to create
a class that creates that very input class using BCEL.
(Think about this sentence for a while, or just try it ...)
lets JustIce work standalone.
If you get a "java.lang.OutOfMemoryError", you should increase the
maximum Java heap space. A command like
will usually resolve the problem. The value above is suitable for
big server machines; if your machine starts swapping to disk, try
to lower the value.
Running a graphics based verifier
If you prefer a graphical application, you should use a command like
to launch one. Again, you may have to resolve a memory issue depending
on the classes to verify.