futils.utils
Class ClassFile

java.lang.Object
  |
  +--futils.utils.ClassFile

public class ClassFile
extends java.lang.Object

This class is used to manipulate Java class files in strange and mysterious ways. Usage it typically to feed it an array of bytes that are a class file, manipulate the class, then convert the class back into bytes, and feed the final result to defineClass().

Version:
1.6, 19 Aug 1995
Author:
Chuck McManis
See Also:
AttributeInfo, ConstantPoolInfo, MethodInfo, FieldInfo

Field Summary
static int ACC_ABSTRACT
           
static int ACC_FINAL
           
static int ACC_INTERFACE
           
static int ACC_NATIVE
           
static int ACC_PRIVATE
           
static int ACC_PROTECTED
           
static int ACC_PUBLIC
           
static int ACC_STATIC
           
static int ACC_SYNCHRONIZED
           
static int ACC_THREADSAFE
           
static int ACC_TRANSIENT
           
 boolean debug
           
 boolean dumpConstants
           
 
Constructor Summary
ClassFile()
           
 
Method Summary
static java.lang.String accessString(short flags)
          Returns a string that represents what the access flags are set for.
 void addAttribute(AttributeInfo newAttribute)
          Add a new optional class Attribute.
 short addConstantPoolItem(ConstantPoolInfo item)
          Add a single constant pool item and return its index.
 void addConstantPoolItems(ConstantPoolInfo[] items)
          Add some items to the constant pool.
 void deleteMethod(java.lang.String name, java.lang.String signature)
          Delete a named method from this class.
 void display(java.io.PrintStream ps)
          Write out a text version of this class.
 AttributeInfo getAttribute(java.lang.String name)
          Return the attribute named 'name' from the class file.
 java.lang.String getClassName()
           
 ConstantPoolInfo getConstantPoolItem(short index)
          Return a constant pool item from this class.
 ConstantPoolInfo getConstantRef(short index)
           
 void mapClass(java.lang.String oldClass, java.lang.String newClass)
          Map occurences of class oldClass to occurrences of class newClass.
 void mapPackage(java.lang.String oldPackage, java.lang.String newPackage)
          Map occurences of package oldPackage to package newPackage.
static java.lang.String nextSig(java.lang.String sig)
          Returns the next signature from a string of concatenated signatures.
 boolean read(java.io.InputStream in)
          Read a class from InputStream in.
 java.lang.String toString()
          The boring version of display().
static java.lang.String typeString(java.lang.String typeString, java.lang.String varName)
          Takes a type signature and a string representing a variable name and returns a declaration for that variable name.
 void write(java.io.OutputStream out)
          Write the class out as a stream of bytes to the output stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ACC_ABSTRACT

public static final int ACC_ABSTRACT

ACC_FINAL

public static final int ACC_FINAL

ACC_INTERFACE

public static final int ACC_INTERFACE

ACC_NATIVE

public static final int ACC_NATIVE

ACC_PRIVATE

public static final int ACC_PRIVATE

ACC_PROTECTED

public static final int ACC_PROTECTED

ACC_PUBLIC

public static final int ACC_PUBLIC

ACC_STATIC

public static final int ACC_STATIC

ACC_SYNCHRONIZED

public static final int ACC_SYNCHRONIZED

ACC_THREADSAFE

public static final int ACC_THREADSAFE

ACC_TRANSIENT

public static final int ACC_TRANSIENT

debug

public boolean debug

dumpConstants

public boolean dumpConstants
Constructor Detail

ClassFile

public ClassFile()
Method Detail

accessString

public static java.lang.String accessString(short flags)
Returns a string that represents what the access flags are set for. So 0x14 returns "public final "

addAttribute

public void addAttribute(AttributeInfo newAttribute)
Add a new optional class Attribute. Items is an array of constant pool items that are first added to the constant pool. At a minimum items[0] must be an ASCIZ item with the name of the attribute. If the body of the attribute references constant pool items these should be in the item list as well.

addConstantPoolItem

public short addConstantPoolItem(ConstantPoolInfo item)
                          throws java.lang.Exception
Add a single constant pool item and return its index. If the item is already in the pool then the index of the preexisting item is returned. Thus you cannot assume that a pointer to your item will be useful.

addConstantPoolItems

public void addConstantPoolItems(ConstantPoolInfo[] items)
Add some items to the constant pool. This is used to add new items to the constant pool. The items references in arg1 and arg2 are expected to be valid pointers (if necessary). Pruning is done to prevent adding redundant items to the list and to preserve string space. The algorithm is simple, first identify pool items containing constants in the list of items to be added that are already in the constant pool. If any are found to already exist, change the pointers in the non-constant items to point to the ones in the pool rather than the ones in the list. Next check to see if any of the non-constant items are already in the pool and if so fix up the others in the list to point to the ones in the pool. Finally, add any items (there must be at least one) from the item list that aren't already in the pool, all of the pointers will already be fixed. NOTE: Since constants in the constant pool may be referenced inside the opaque portion of attributes the constant table cannot be re-ordered, only extended.

deleteMethod

public void deleteMethod(java.lang.String name,
                         java.lang.String signature)
Delete a named method from this class. This method is used to excise specific methods from the loaded class. The actual method code remains, however the method signature is deleted from the constant pool. If this method is called by a class the exception IncompatibleClassChangeException is generated by the runtime.

display

public void display(java.io.PrintStream ps)
             throws java.lang.Exception
Write out a text version of this class.

getAttribute

public AttributeInfo getAttribute(java.lang.String name)
Return the attribute named 'name' from the class file.

getClassName

public java.lang.String getClassName()

getConstantPoolItem

public ConstantPoolInfo getConstantPoolItem(short index)
                                     throws java.lang.Exception
Return a constant pool item from this class. (note does fixup of indexes to facilitate extracting nested or linked items.

getConstantRef

public ConstantPoolInfo getConstantRef(short index)

mapClass

public void mapClass(java.lang.String oldClass,
                     java.lang.String newClass)
Map occurences of class oldClass to occurrences of class newClass. This method is used to retarget accesses to one class, seamlessly to another. The format for the class name is slash (/) separated so the class util.ClassFile would be represented as util/ClassFile

mapPackage

public void mapPackage(java.lang.String oldPackage,
                       java.lang.String newPackage)
Map occurences of package oldPackage to package newPackage. The format for the package name is slash (/) separated so the package java.util would be represented as java/util

nextSig

public static java.lang.String nextSig(java.lang.String sig)
Returns the next signature from a string of concatenated signatures. For example if the signature was "[BII", this method would return "II"

read

public boolean read(java.io.InputStream in)
             throws java.io.IOException
Read a class from InputStream in.

toString

public java.lang.String toString()
The boring version of display().
Overrides:
toString in class java.lang.Object

typeString

public static java.lang.String typeString(java.lang.String typeString,
                                          java.lang.String varName)
Takes a type signature and a string representing a variable name and returns a declaration for that variable name. For example, passing this the strings "[B" and "myArray" will return the string "byte myArray[]"

write

public void write(java.io.OutputStream out)
           throws java.io.IOException,
                  java.lang.Exception
Write the class out as a stream of bytes to the output stream. Generally you will read a class file, manipulate it in some way, and then write it out again before passing it to defineClass in some class loader.