/projects/sandmark-3.4/doc/tools.tex
LaTeX | 134 lines | 114 code | 20 blank | 0 comment | 0 complexity | 48a26a3b79bf16d8ac82c146e310a066 MD5 | raw file
- \section{Examining Java Classfiles}
- There are a number of tools that are helpful for
- viewing Java classfiles.
- \subsection{javap}
- {\tt javap} lists the contents of a Java classfile.
- It's particularly bad at displaying corrupted
- classfiles.
- Normally, we call {\tt javap} like this:
- \begin{verbatim}
- javap -c -s -verbose -l TTTApplication
- \end{verbatim}
- These are the available options:
- \begin{verbatim}
- Usage: javap <options> <classes>...
- where options include:
- -b Backward compatibility with javap in JDK 1.1
- -c Disassemble the code
- -classpath <pathlist> Specify where to find user class files
- -extdirs <dirs> Override location of installed extensions
- -help Print this usage message
- -J<flag> Pass <flag> directly to the runtime system
- -l Print line number and local variable tables
- -public Show only public classes and members
- -protected Show protected/public classes and members
- -package Show package/protected/public classes
- and members (default)
- -private Show all classes and members
- -s Print internal type signatures
- -bootclasspath <pathlist> Override location of class files loaded
- by the bootstrap class loader
- -verbose Print stack size, number of locals and args for methods
- If verifying, print reasons for failure
- \end{verbatim}
- \subsection{Jasmin}
- {\tt Jasmin} is a Java bytecode assembler. It reads
- a text file containing Java bytecode instructions and
- generates a classfile. It can be found here:
- \url{http://mrl.nyu.edu/~meyer/jasmin/about.html}.
- Here's is a simple class {\tt hello.j} in the {\tt Jasmin}
- assembler syntax:
- \begin{listing}{1}
- .class public HelloWorld
- .super java/lang/Object
- ;
- ; standard initializer (calls java.lang.Object's initializer)
- ;
- .method public <init>()V
- aload_0
- invokenonvirtual java/lang/Object/<init>()V
- return
- .end method
- ;
- ; main() - prints out Hello World
- ;
- .method public static main([Ljava/lang/String;)V
- .limit stack 2 ; up to two items can be pushed
- ; push System.out onto the stack
- getstatic java/lang/System/out Ljava/io/PrintStream;
- ; push a string onto the stack
- ldc "Hello World!"
- ; call the PrintStream.println() method.
- invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V
- ; done
- return
- .end method
- \end{listing}
- Call {\tt Jasmin} like this:
- \begin{verbatim}
- java -classpath smextern/jasmin.jar jasmin.Main hello.j
- \end{verbatim}
- which will produce a class file {\tt HelloWorld.class}.
- \subsection{BCEL's Class Construction Kit}
- {\tt cck} is an interactive viewer and editor of Java classfiles.
- It is built on {\tt BCEL}. Call it like this:
- \begin{verbatim}
- java -classpath .:smextern/BCEL.jar -jar smextern/cck.jar
- \end{verbatim}
- and open a classfile from the {\tt file}-menu.
- \subsection{BCEL's listclass}
- {\tt listclass} comes with the BCEL package. It's
- a replacement for {\tt javap}, and very useful
- in cases when {\tt javap} crashes. Call it like
- this:
- \begin{verbatim}
- java -classpath smextern/BCEL.jar listclass -code TTTApplication
- \end{verbatim}
- The following options are available:
- \begin{verbatim}
- java listclass [-constants] [-code] [-brief] [-dependencies] \
- [-nocontents] [-recurse] class... [-exclude <list>]
- \end{verbatim}
- \begin{description}
- \item[{\tt -code}:]
- List byte code of methods.
- \item[{\tt -brief}:]
- List byte codes briefly
- \item[{\tt -constants}:]
- Print constants table (constant pool)
- \item[{\tt -recurse}:]
- Usually intended to be used along with
- \item[{\tt -dependencies:}]
- When this flag is set, listclass will also print i
- \end{description}
- \subsection{BCEL's JustIce}
- {\tt JustIce} verifies classfiles. Call it like this:
- \begin{verbatim}
- java -classpath .:smextern/BCEL.jar:smextern/JustIce.jar \
- org.apache.bcel.verifier.GraphicalVerifier
- \end{verbatim}
- or like this:
- \begin{verbatim}
- java -classpath .:smextern/BCEL.jar:smextern/JustIce.jar \
- org.apache.bcel.verifier.Verifier TTTApplication.class
- \end{verbatim}