TechTrader Bytecode Toolkit

com.techtrader.modules.tools.bytecode
Class BCClass

java.lang.Object
  |
  +--com.techtrader.modules.tools.bytecode.BCEntity
        |
        +--com.techtrader.modules.tools.bytecode.BCClass

public class BCClass
extends BCEntity
implements Constants

Start here to understand this package. A BCClass is a representation of a bytecode class. It contains methods to manipulate the class object itself as well as methods to manage the fields and methods of the class. As with most entities in the bytecode framework, there are methods to manipulate the low-level state of the class (constant pool indexes, etc), but these methods can safely be ignored in favor of the available high-level methods.

Author:
Abe White

Fields inherited from interface com.techtrader.modules.tools.bytecode.Constants
AALOAD, AASTORE, ACCESS_ABSTRACT, ACCESS_FINAL, ACCESS_INTERFACE, ACCESS_NATIVE, ACCESS_PRIVATE, ACCESS_PROTECTED, ACCESS_PUBLIC, ACCESS_STATIC, ACCESS_STRICT, ACCESS_SUPER, ACCESS_SYNCHRONIZED, ACCESS_TRANSIENT, ACCESS_VOLATILE, ACONST_NULL, ALOAD, ALOAD_0, ALOAD_1, ALOAD_2, ALOAD_3, ANEWARRAY, ARETURN, ARRAY_BOOLEAN, ARRAY_BYTE, ARRAY_CHAR, ARRAY_DOUBLE, ARRAY_FLOAT, ARRAY_INT, ARRAY_LONG, ARRAY_SHORT, ARRAYLENGTH, ASTORE, ASTORE_0, ASTORE_1, ASTORE_2, ASTORE_3, ATHROW, ATTR_CODE, ATTR_CONST, ATTR_DEPRECATED, ATTR_EXCEPTIONS, ATTR_INNERCLASS, ATTR_LINENUMBERS, ATTR_LOCALS, ATTR_SOURCE, ATTR_SYNTHETIC, ATTR_UNKNOWN, BALOAD, BASTORE, BIPUSH, CALOAD, CASTORE, CHECKCAST, D2F, D2I, D2L, DADD, DALOAD, DASTORE, DCMPG, DCMPL, DCONST_0, DCONST_1, DDIV, DLOAD, DLOAD_0, DLOAD_1, DLOAD_2, DLOAD_3, DMUL, DNEG, DREM, DRETURN, DSTORE, DSTORE_0, DSTORE_1, DSTORE_2, DSTORE_3, DSUB, DUP, DUP_X1, DUP_X2, DUP2, DUP2_X1, DUP2_X2, F2D, F2I, F2L, FADD, FALOAD, FASTORE, FCMPG, FCMPL, FCONST_0, FCONST_1, FCONST_2, FDIV, FLOAD, FLOAD_0, FLOAD_1, FLOAD_2, FLOAD_3, FMUL, FNEG, FREM, FRETURN, FSTORE, FSTORE_0, FSTORE_1, FSTORE_2, FSTORE_3, FSUB, GETFIELD, GETSTATIC, GOTO, GOTO_W, I2B, I2C, I2D, I2F, I2L, I2S, IADD, IALOAD, IAND, IASTORE, ICONST_0, ICONST_1, ICONST_2, ICONST_3, ICONST_4, ICONST_5, ICONST_M1, IDIV, IF_ACMPEQ, IF_ACMPNE, IF_EQ, IF_GE, IF_GT, IF_ICMPEQ, IF_ICMPGE, IF_ICMPGT, IF_ICMPLE, IF_ICMPLT, IF_ICMPNE, IF_LE, IF_LT, IF_NE, IF_NONNULL, IF_NULL, IINC, ILOAD, ILOAD_0, ILOAD_1, ILOAD_2, ILOAD_3, IMUL, INEG, INSTANCEOF, INVOKEINTERFACE, INVOKESPECIAL, INVOKESTATIC, INVOKEVIRTUAL, IOR, IREM, IRETURN, ISHL, ISHR, ISTORE, ISTORE_0, ISTORE_1, ISTORE_2, ISTORE_3, ISUB, IUSHR, IXOR, JSR, JSR_W, L2D, L2F, L2I, LADD, LALOAD, LAND, LASTORE, LCMP, LCONST_0, LCONST_1, LDC, LDC_W, LDC2_W, LDIV, LLOAD, LLOAD_0, LLOAD_1, LLOAD_2, LLOAD_3, LMUL, LNEG, LOOKUPSWITCH, LOR, LREM, LRETURN, LSHL, LSHR, LSTORE, LSTORE_0, LSTORE_1, LSTORE_2, LSTORE_3, LSUB, LUSHR, LXOR, MATH_ADD, MATH_AND, MATH_DIV, MATH_MUL, MATH_NEG, MATH_OR, MATH_REM, MATH_SHL, MATH_SHR, MATH_SUB, MATH_USHR, MATH_XOR, MONITORENTER, MONITOREXIT, MULTIANEWARRAY, NEW, NEWARRAY, NOP, OPCODE_NAMES, POP, POP2, PUTFIELD, PUTSTATIC, RET, RETURN, SALOAD, SASTORE, SIPUSH, SWAP, TABLESWITCH, VALID_MAGIC, WIDE
 
Constructor Summary
BCClass()
          Default constructor.
BCClass(BCClass orig)
          Create a BCClass that is an exact copy of the given one.
BCClass(Class type)
          Create a BCClass for the given Class type.
BCClass(File classFile)
          Create a BCClass from the given .class file.
BCClass(InputStream in)
          Create a BCClass from the given stream representing a .class file.
BCClass(String name)
          Create a new BCClass with the given name.
 
Method Summary
 void acceptVisit(BCVisitor visit)
          Accept a visit from a BCVisitor, calling the appropriate methods to notify the visitor that it has entered this entity, and to provide it with the proper callbacks for each sub-entity owned by this object.
 BCMethod addDefaultConstructor()
          Add a default constructor to this class.
 BCField addField()
          Add a field to this class.
 BCField addField(String name, Class type)
          Add a field to this class.
 BCField addField(String name, String type)
          Add a field to this class.
 void addInterfaceName(String name)
          Add an interface to those implemented by this class.
 void addInterfaceType(Class type)
          Add a Class to those implemented by this interface.
 BCMethod addMethod()
          Add a method to this class.
 BCMethod addMethod(String name, Class returnType, Class[] paramTypes)
          Add a method to this class.
 BCMethod addMethod(String name, String returnType, String[] paramTypes)
          Add a method to this class.
 void clearFields()
          Clear all fields from this class.
 void clearInterfaces()
          Clear this class of all interface declarations.
 void clearMethods()
          Remove all methods from this class; note that this includes constructors, static initializers, etc.
 void copy(BCClass orig)
          Copy all of the data from the given original BCClass to this one, recursing into fields, methods, code, the constant pool, etc.
 int getAccessFlags()
          Return the access flags for this class as a bit array of ACCESS_XXX constants.
 BCField getField(String name)
          Get the field with the given name.
 BCField[] getFields()
          Get all the fields of this class.
 int getIndex()
          Get the index in the constant pool of the ClassEntry for this class.
 int[] getInterfaceIndexes()
          Get the list of indexes into the constant pool of the ClassEntrys describing all the interfaces this class implements/extends.
 String[] getInterfaceNames()
          Get the names of the interfaces for this class, including package names.
 Class[] getInterfaceTypes()
          Get the Class objects for the interfaces of this class.
 int getMagic()
          Get the magic number for this .class; if this is a valid class, this should be equal to the VALID_MAGIC constant.
 int getMajorVersion()
          Get the major version of the bytecode spec used for this class; JVMs are only required to operate with versions that they understand; leaving the default value (45) is safe.
 BCMethod getMethod(String name)
          Get the method with the given name.
 BCMethod getMethod(String name, Class[] params)
          Get the method with the given name and param types.
 BCMethod getMethod(String name, String[] params)
          Get the method with the given name and param types.
 BCMethod[] getMethods()
          Get all the methods of this class.
 BCMethod[] getMethods(String name)
          Get all methods with the given name.
 int getMinorVersion()
          Get the minor version of the bytecode spec used for this class; JVMs are only required to operate with versions that they understand; leaving the default value (3) is safe.
 String getName()
          Get the name of this class, including package name.
 ConstantPool getPool()
          Return the constant pool for this class.
 int getSuperclassIndex()
          Get the index in the constant pool of the ClassEntry for the superclass.
 String getSuperclassName()
          Get the name of the superclass for this class, including package name.
 Class getSuperclassType()
          Get the Class object for the superclass of this class.
 Class getType()
          Get the Class object for this class.
 boolean implementsInterface(Class type)
          Return true if the class declares that it implements the given interface.
 boolean implementsInterface(String name)
          Return true if the class declares that it implements the given interface.
 BCField importField(BCField field)
          Import the given field from another class, or, if the field belongs to this class, add a duplicate of it (in this case, it is an error not to change the field name).
 void importFields(BCClass other)
          Import all fields from another class.
 BCMethod importMethod(BCMethod method)
          Import the given method from another class, or, if the method belongs to this class, add a duplicate of it (in this case, it is an error not to change the method name or parameter types).
 void importMethods(BCClass other)
          Import all methods from the given class; note that this includes constructors, static initializers, etc.
 boolean isAbstract()
          Manipulate the class access flags.
 boolean isFinal()
          Manipulate the class access flags.
 boolean isInterface()
          Manipulate the class access flags.
 boolean isPackage()
          Manipulate the class access flags.
 boolean isPublic()
          Manipulate the class access flags.
 void makePackage()
          Manipulate the class access flags.
 void makePublic()
          Manipulate the class access flags.
 void read(Class type)
          Read the definition of the given class.
 void read(File classFile)
          Read the class definition from the given file.
 void read(InputStream instream)
          Read the class definition from the given stream.
 boolean removeField(BCField field)
          Removes a field from this class.
 boolean removeField(String name)
          Removes the field with the given name from this class.
 boolean removeInterfaceName(String name)
          Remove an interface implmented by this class.
 boolean removeInterfaceType(Class type)
          Remove an interface implemented by this class.
 boolean removeMethod(BCMethod method)
          Removes a method from this class.
 boolean removeMethod(String name)
          Removes the method with the given name from the class.
 boolean removeMethod(String name, Class[] params)
          Removes the method with the given signature.
 boolean removeMethod(String name, String[] params)
          Removes the method with the given signature.
 void setAbstract(boolean on)
          Manipulate the class access flags.
 void setAccessFlags(int access)
          Set the access flags for this class as a bit array of ACCESS_XXX constants.
 void setFinal(boolean on)
          Manipulate the class access flags.
 void setIndex(int index)
          Set the constant pool index of the ClassEntry for this class.
 void setInterface(boolean on)
          Manipulate the class access flags.
 void setInterfaceIndexes(int[] interfaceIndexes)
          Set the list of indexes into the constant pool of the ClassEntrys describing all the interfaces this class implements/extends; set to null if none.
 void setInterfaceNames(String[] interfaces)
          Set the interfaces implemented by this class.
 void setInterfaceTypes(Class[] interfaces)
          Set the interfaces implemented by this class.
 void setMagic(int magic)
          Set the magic number for this .class; if this is a valid class, this should be equal to the VALID_MAGIC constant (the default value).
 void setMajorVersion(int majorVersion)
          Set the major version of the bytecode spec used for this class; JVMs are only required to operate with versions that they understand; leaving the default value (45) is safe.
 void setMinorVersion(int minorVersion)
          Set the minor version of the bytecode spec used for this class; JVMs are only required to operate with versions that they understand; leaving the default value (3) is safe.
 void setName(String name)
          Set the name of this class.
 void setSuperclassIndex(int index)
          Set the constant pool index of the ClassEntry for the superclass.
 void setSuperclassName(String name)
          Set the name of the superclass to this class.
 void setSuperclassType(Class type)
          Set the Class object for the superclass of this class.
 byte[] toByteArray()
          Get the contents of this class as a byte array, possibly for use in a custom ClassLoader.
 void write()
          For existing classes, write the new bytecode to the same .class file the class was laoded from.
 void write(File classFile)
          Write the class to the specified file.
 void write(OutputStream outstream)
          Write the class to the specified stream.
 
Methods inherited from class com.techtrader.modules.tools.bytecode.BCEntity
addAttribute, clearAttributes, getAttribute, getAttributes, getAttributes, importAttribute, importAttributes, readAttributes, removeAttribute, removeAttribute, visitAttributes, writeAttributes
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BCClass

public BCClass()
Default constructor. Creates a new empty class.

BCClass

public BCClass(BCClass orig)
Create a BCClass that is an exact copy of the given one.

BCClass

public BCClass(Class type)
        throws IOException
Create a BCClass for the given Class type.

BCClass

public BCClass(String name)
Create a new BCClass with the given name.

BCClass

public BCClass(File classFile)
        throws IOException
Create a BCClass from the given .class file.

BCClass

public BCClass(InputStream in)
        throws IOException
Create a BCClass from the given stream representing a .class file.
Method Detail

read

public void read(File classFile)
          throws IOException
Read the class definition from the given file. This method resets all information in this instance.

read

public void read(InputStream instream)
          throws IOException
Read the class definition from the given stream. This method resets all information in this instance.

read

public void read(Class type)
          throws IOException
Read the definition of the given class. This method resets all information in this instance.

write

public void write()
           throws IOException
For existing classes, write the new bytecode to the same .class file the class was laoded from.

write

public void write(File classFile)
           throws IOException
Write the class to the specified file.

write

public void write(OutputStream outstream)
           throws IOException
Write the class to the specified stream.

toByteArray

public byte[] toByteArray()
                   throws IOException
Get the contents of this class as a byte array, possibly for use in a custom ClassLoader.

copy

public void copy(BCClass orig)
Copy all of the data from the given original BCClass to this one, recursing into fields, methods, code, the constant pool, etc.

getMagic

public int getMagic()
Get the magic number for this .class; if this is a valid class, this should be equal to the VALID_MAGIC constant.

setMagic

public void setMagic(int magic)
Set the magic number for this .class; if this is a valid class, this should be equal to the VALID_MAGIC constant (the default value).

getMajorVersion

public int getMajorVersion()
Get the major version of the bytecode spec used for this class; JVMs are only required to operate with versions that they understand; leaving the default value (45) is safe.

setMajorVersion

public void setMajorVersion(int majorVersion)
Set the major version of the bytecode spec used for this class; JVMs are only required to operate with versions that they understand; leaving the default value (45) is safe.

getMinorVersion

public int getMinorVersion()
Get the minor version of the bytecode spec used for this class; JVMs are only required to operate with versions that they understand; leaving the default value (3) is safe.

setMinorVersion

public void setMinorVersion(int minorVersion)
Set the minor version of the bytecode spec used for this class; JVMs are only required to operate with versions that they understand; leaving the default value (3) is safe.

getAccessFlags

public int getAccessFlags()
Return the access flags for this class as a bit array of ACCESS_XXX constants. This can be used to transfer access flags between classes without getting/setting each possible access flag.

setAccessFlags

public void setAccessFlags(int access)
Set the access flags for this class as a bit array of ACCESS_XXX constants. This can be used to transfer access flags between classes without getting/setting each possible access flag.

isPublic

public boolean isPublic()
Manipulate the class access flags.

makePublic

public void makePublic()
Manipulate the class access flags.

isPackage

public boolean isPackage()
Manipulate the class access flags.

makePackage

public void makePackage()
Manipulate the class access flags.

isFinal

public boolean isFinal()
Manipulate the class access flags.

setFinal

public void setFinal(boolean on)
Manipulate the class access flags.

isInterface

public boolean isInterface()
Manipulate the class access flags.

setInterface

public void setInterface(boolean on)
Manipulate the class access flags.

isAbstract

public boolean isAbstract()
Manipulate the class access flags.

setAbstract

public void setAbstract(boolean on)
Manipulate the class access flags.

getIndex

public int getIndex()
Get the index in the constant pool of the ClassEntry for this class.

setIndex

public void setIndex(int index)
Set the constant pool index of the ClassEntry for this class.

getName

public String getName()
Get the name of this class, including package name.

setName

public void setName(String name)
Set the name of this class.

getType

public Class getType()
              throws ClassNotFoundException
Get the Class object for this class.

getSuperclassIndex

public int getSuperclassIndex()
Get the index in the constant pool of the ClassEntry for the superclass.

setSuperclassIndex

public void setSuperclassIndex(int index)
Set the constant pool index of the ClassEntry for the superclass.

getSuperclassName

public String getSuperclassName()
Get the name of the superclass for this class, including package name.

setSuperclassName

public void setSuperclassName(String name)
Set the name of the superclass to this class.

getSuperclassType

public Class getSuperclassType()
                        throws ClassNotFoundException
Get the Class object for the superclass of this class.

setSuperclassType

public void setSuperclassType(Class type)
Set the Class object for the superclass of this class.

getInterfaceIndexes

public int[] getInterfaceIndexes()
Get the list of indexes into the constant pool of the ClassEntrys describing all the interfaces this class implements/extends.
Returns:
the implmented interfaces, or empty array if none

setInterfaceIndexes

public void setInterfaceIndexes(int[] interfaceIndexes)
Set the list of indexes into the constant pool of the ClassEntrys describing all the interfaces this class implements/extends; set to null if none.

getInterfaceNames

public String[] getInterfaceNames()
Get the names of the interfaces for this class, including package names.

getInterfaceTypes

public Class[] getInterfaceTypes()
                          throws ClassNotFoundException
Get the Class objects for the interfaces of this class.

setInterfaceNames

public void setInterfaceNames(String[] interfaces)
Set the interfaces implemented by this class.

setInterfaceTypes

public void setInterfaceTypes(Class[] interfaces)
Set the interfaces implemented by this class.

clearInterfaces

public void clearInterfaces()
Clear this class of all interface declarations.

removeInterfaceName

public boolean removeInterfaceName(String name)
Remove an interface implmented by this class.

removeInterfaceType

public boolean removeInterfaceType(Class type)
Remove an interface implemented by this class.

addInterfaceName

public void addInterfaceName(String name)
Add an interface to those implemented by this class.

addInterfaceType

public void addInterfaceType(Class type)
Add a Class to those implemented by this interface.

implementsInterface

public boolean implementsInterface(String name)
Return true if the class declares that it implements the given interface.

implementsInterface

public boolean implementsInterface(Class type)
Return true if the class declares that it implements the given interface.

getFields

public BCField[] getFields()
Get all the fields of this class.

getField

public BCField getField(String name)
Get the field with the given name.

importField

public BCField importField(BCField field)
Import the given field from another class, or, if the field belongs to this class, add a duplicate of it (in this case, it is an error not to change the field name).

importFields

public void importFields(BCClass other)
Import all fields from another class.

addField

public BCField addField()
Add a field to this class.

addField

public BCField addField(String name,
                        String type)
Add a field to this class.

addField

public BCField addField(String name,
                        Class type)
Add a field to this class.

clearFields

public void clearFields()
Clear all fields from this class.

removeField

public boolean removeField(String name)
Removes the field with the given name from this class.

removeField

public boolean removeField(BCField field)
Removes a field from this class. After this method, the field will be invalid, and the result of any operations on it is undefined.

getMethods

public BCMethod[] getMethods()
Get all the methods of this class.

getMethod

public BCMethod getMethod(String name)
Get the method with the given name. If multiple methods in this class have this name, which is returned is not defined.

getMethods

public BCMethod[] getMethods(String name)
Get all methods with the given name.
Returns:
the matching methods, or empty array if none

getMethod

public BCMethod getMethod(String name,
                          String[] params)
Get the method with the given name and param types.

getMethod

public BCMethod getMethod(String name,
                          Class[] params)
Get the method with the given name and param types.

importMethod

public BCMethod importMethod(BCMethod method)
Import the given method from another class, or, if the method belongs to this class, add a duplicate of it (in this case, it is an error not to change the method name or parameter types).

importMethods

public void importMethods(BCClass other)
Import all methods from the given class; note that this includes constructors, static initializers, etc.

addMethod

public BCMethod addMethod()
Add a method to this class.

addMethod

public BCMethod addMethod(String name,
                          String returnType,
                          String[] paramTypes)
Add a method to this class.

addMethod

public BCMethod addMethod(String name,
                          Class returnType,
                          Class[] paramTypes)
Add a method to this class.

clearMethods

public void clearMethods()
Remove all methods from this class; note that this includes constructors, static initializers, etc.

removeMethod

public boolean removeMethod(BCMethod method)
Removes a method from this class. After this method, the method will be invalid, and the result of any operations on it is undefined.

removeMethod

public boolean removeMethod(String name)
Removes the method with the given name from the class. If multiple methods have the name, they will all be removed.

removeMethod

public boolean removeMethod(String name,
                            String[] params)
Removes the method with the given signature.

removeMethod

public boolean removeMethod(String name,
                            Class[] params)
Removes the method with the given signature.

getPool

public ConstantPool getPool()
Return the constant pool for this class.
Overrides:
getPool in class BCEntity

addDefaultConstructor

public BCMethod addDefaultConstructor()
Add a default constructor to this class. This method can only be called if the superclass has been set.

acceptVisit

public void acceptVisit(BCVisitor visit)
Description copied from interface: VisitAcceptor
Accept a visit from a BCVisitor, calling the appropriate methods to notify the visitor that it has entered this entity, and to provide it with the proper callbacks for each sub-entity owned by this object.

TechTrader Bytecode Toolkit