wt.loader
Class ClassFile

java.lang.Object
  extended bywt.loader.ClassFile

public class ClassFile
extends 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().

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
           
(package private)  short accessFlags
           
(package private)  AttributeInfo[] attributes
           
(package private)  ConstantPoolInfo[] constantPool
           
 boolean debug
           
 boolean dumpConstants
           
(package private)  FieldInfo[] fields
           
(package private)  ConstantPoolInfo[] interfaces
           
(package private)  boolean isValidClass
           
(package private)  int magic
           
(package private)  short majorVersion
           
(package private)  MethodInfo[] methods
           
(package private)  short minorVersion
           
(package private)  ConstantPoolInfo superClass
           
(package private)  ConstantPoolInfo thisClass
           
private static String versionID
           
 
Constructor Summary
ClassFile()
           
 
Method Summary
static 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(String name, String signature)
          Delete a named method from this class.
 void display(PrintStream ps)
          Write out a text version of this class.
 AttributeInfo getAttribute(String name)
          Return the attribute named 'name' from the class file.
 String getClassName()
           
 ConstantPoolInfo getConstantPoolItem(short index)
          Return a constant pool item from this class.
 ConstantPoolInfo getConstantRef(short index)
           
 void mapClass(String oldClass, String newClass)
          Map occurences of class oldClass to occurrences of class newClass.
 void mapPackage(String oldPackage, String newPackage)
          Map occurences of package oldPackage to package newPackage.
static String nextSig(String sig)
          Returns the next signature from a string of concatenated signatures.
private  String printClassName(String s)
          Print the name of a class in "canonical form"
 boolean read(InputStream in)
          Read a class from InputStream in.
 String toString()
          The boring version of display().
static String typeString(String typeString, String varName)
          Takes a type signature and a string representing a variable name and returns a declaration for that variable name.
 void write(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

versionID

private static final String versionID
See Also:
Constant Field Values

magic

int magic

majorVersion

short majorVersion

minorVersion

short minorVersion

constantPool

ConstantPoolInfo[] constantPool

accessFlags

short accessFlags

thisClass

ConstantPoolInfo thisClass

superClass

ConstantPoolInfo superClass

interfaces

ConstantPoolInfo[] interfaces

fields

FieldInfo[] fields

methods

MethodInfo[] methods

attributes

AttributeInfo[] attributes

isValidClass

boolean isValidClass

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

debug

public boolean debug

dumpConstants

public boolean dumpConstants
Constructor Detail

ClassFile

public ClassFile()
Method Detail

read

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

Throws:
IOException

write

public void write(OutputStream out)
           throws IOException,
                  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:
IOException
Exception

accessString

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


typeString

public static String typeString(String typeString,
                                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 String nextSig(String sig)
Returns the next signature from a string of concatenated signatures. For example if the signature was "[BII", this method would return "II"


printClassName

private String printClassName(String s)
Print the name of a class in "canonical form"


getClassName

public String getClassName()

toString

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


display

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

Throws:
Exception

getConstantRef

public ConstantPoolInfo getConstantRef(short index)

addConstantPoolItem

public short addConstantPoolItem(ConstantPoolInfo item)
                          throws 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:
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 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.


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(String name)
Return the attribute named 'name' from the class file.


getConstantPoolItem

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

Throws:
Exception

mapClass

public void mapClass(String oldClass,
                     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(String oldPackage,
                       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(String name,
                         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.