classUtils.dumper
Class ClassFile

java.lang.Object
  extended by classUtils.dumper.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

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 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()
          Write out a text version of this class.
 AttributeInfo getAttribute(java.lang.String name)
          Return the attribute named 'name' from the class file.
static ClassFile getClassFile()
           
static ClassFile getClassFile(java.io.File f)
           
static ClassFile getClassFile(java.io.FileInputStream fis)
           
static ClassFile[] getClassFiles()
           
 java.lang.String getClassName()
           
 ConstantPoolInfo getConstantPoolItem(short index)
          Return a constant pool item from this class.
 ConstantPoolInfo getConstantRef(short index)
           
 boolean hasMainMethod()
           
static void main(java.lang.String[] args)
           
 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.
static ClassFile readClassFile(java.io.InputStream fis)
           
static void testGetClassFiles()
           
 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
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ACC_PUBLIC

public static final int ACC_PUBLIC
See Also:
Constant Field Values

ACC_PRIVATE

public static final int ACC_PRIVATE
See Also:
Constant Field Values

ACC_PROTECTED

public static final int ACC_PROTECTED
See Also:
Constant Field Values

ACC_STATIC

public static final int ACC_STATIC
See Also:
Constant Field Values

ACC_FINAL

public static final int ACC_FINAL
See Also:
Constant Field Values

ACC_SYNCHRONIZED

public static final int ACC_SYNCHRONIZED
See Also:
Constant Field Values

ACC_THREADSAFE

public static final int ACC_THREADSAFE
See Also:
Constant Field Values

ACC_TRANSIENT

public static final int ACC_TRANSIENT
See Also:
Constant Field Values

ACC_NATIVE

public static final int ACC_NATIVE
See Also:
Constant Field Values

ACC_INTERFACE

public static final int ACC_INTERFACE
See Also:
Constant Field Values

ACC_ABSTRACT

public static final int ACC_ABSTRACT
See Also:
Constant Field Values

dumpConstants

public boolean dumpConstants
Constructor Detail

ClassFile

public ClassFile()
Method Detail

read

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

Throws:
java.io.IOException

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.

Throws:
java.io.IOException
java.lang.Exception

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 "


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[]"


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"


getClassName

public java.lang.String getClassName()

toString

public java.lang.String toString()
The boring version of display().

Overrides:
toString in class java.lang.Object

display

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

Throws:
java.lang.Exception

getConstantRef

public ConstantPoolInfo getConstantRef(short index)

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.

Throws:
java.lang.Exception

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 affineTransform 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.


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.


getAttribute

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


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.

Throws:
java.lang.Exception

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


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.


testGetClassFiles

public static void testGetClassFiles()

getClassFiles

public static ClassFile[] getClassFiles()

getClassFile

public static ClassFile getClassFile()

getClassFile

public static ClassFile getClassFile(java.io.File f)

getClassFile

public static ClassFile getClassFile(java.io.FileInputStream fis)

readClassFile

public static ClassFile readClassFile(java.io.InputStream fis)
                               throws java.lang.Exception
Throws:
java.lang.Exception

hasMainMethod

public boolean hasMainMethod()

main

public static void main(java.lang.String[] args)