Dumping Java Class Files

I wrote some code a while back to dump out Java class files (actually to parse Java class files) so that I could manipulate them in mysterious ways. Basically before I left Sun I was working on a least privilege security model for Java and I needed to write a class loader that could return a 'neutered' class to an untrusted application, this code was used to test out some of these ideas. Obviously it isn't very polished but then again it's free.

One of the things these classes do are collect the class refs in a class file. This makes it trivial to provide a complete collection of all classes referenced by any base class by following all of the references. If someone does this, please mail me the result. When someone starts shipping n editor that is programmed in Java I'll write a class to automatically add syntax highlighting to user method and variables.

The classes are as follows (the tar file has a symantec project file that could build them as well):

ClassFile.java
This is the main class, it reads in a class file and parses its contents. Set the debug flag for verbose output.
AttributeInfo.java
The AttributeInfo.java class defines the class for and Attribute in the class file. It encapsulates the method for parsing an optional class file attribute.
MethodInfo.java
MethodInfo.java defines the class for parsing method blocks. If you want to add a disassembler like javap has, this is where you would plug it in.
FieldInfo.java
FieldInfo.java parse fields (instance variables) in the class file.
ConstantPoolInfo.java
This class deals with constant pool entries.
dumpclass.java
This is a very primitive example program that opens a class file, reads it in, and then dumps it out. Mostly it shows how you can use the ClassFile class.

A tar file of all of the classes is also here, there is a Symantec cafe project file as well (I don't know how well these things travel). You should be able to untar the files, cd to the dumpclass directory and type java dumpclass dumpclass.class.

The code is free for non-commercial use, if you use it in a product you can get a non-exclusive royalty free license by sending one copy of your product to me. License applies only to the shipped version, does not include the right to relicense and must be renewed when substantaially revised versions of the product are shipped.

[Copyright © 1994-2004 Charles McManis, All Rights Reserved]

Back to the Java Stuff