Package serp.bytecode

Class BCClass

All Implemented Interfaces:
BCEntity, VisitAcceptor

public class BCClass extends Annotated implements VisitAcceptor
The BCClass represents a class object in the bytecode framework, in many ways mirroring the Class class of Java reflection. The represented class might be a primitive, array, existing object type, or some new user- defined type. As with most entities in the bytecode framework, the BCClass contains methods to manipulate the low-level state of the class (constant pool indexes, etc), but these can and should be ignored in favor of the available high-level methods.

A BCClass instance is loaded from a Project and remains attached to that project for its lifetime. If a BCClass is removed from its project, the result of any further operations on the class are undefined.

Note that if a BCClass represents a primitive or array type, all of the available mutator methods and any methods that access the constant pool will throw UnsupportedOperationExceptions.

Author:
Abe White
  • Method Details

    • write

      public void write() throws IOException
      Write the class bytecode to the .class file in the proper directory of the CLASSPATH. The file must exist already, so this method only works on existing classes.
      Throws:
      IOException
    • write

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

      public void write(OutputStream outstream) throws IOException
      Write the class bytecode to the specified stream.
      Throws:
      IOException
    • toByteArray

      public byte[] toByteArray()
      Return the bytecode of this class as a byte array, possibly for use in a custom ClassLoader.
    • getMagic

      public int getMagic()
      Return the magic number for this class; if this is a valid type, this should be equal to Constants.VALID_MAGIC (the default value).
    • setMagic

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

      public int getMajorVersion()
      Return 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 of Constants.MAJOR_VERSION 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 of Constants.MAJOR_VERSION 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 of Constants.MINOR_VERSION 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 of Constants.MINOR_VERSION is safe.
    • getAccessFlags

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

      public void setAccessFlags(int access)
      Set the access flags for this class as a bit array of ACCESS_XXX constants from Constants. This can be used to transfer access flags between classes without getting/setting each possible 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.
    • isSynthetic

      public boolean isSynthetic()
      Manipulate the class access flags.
    • setSynthetic

      public void setSynthetic(boolean on)
      Manipulate the class access flags.
    • isAnnotation

      public boolean isAnnotation()
      Manipulate the class access flags.
    • setAnnotation

      public void setAnnotation(boolean on)
      Manipulate the class access flags. Setting to true also makes this an interface.
    • isEnum

      public boolean isEnum()
      Manipulate the class access flags.
    • setEnum

      public void setEnum(boolean on)
      Manipulate the class access flags.
    • isPrimitive

      public boolean isPrimitive()
      Return true if this class is a primitive type.
    • isArray

      public boolean isArray()
      Return true if this class is an array type.
    • getIndex

      public int getIndex()
      Return the ConstantPool index of the ClassEntry for this class. Returns 0 if the class does not have a constant pool (such as a primitive or array).
    • setIndex

      public void setIndex(int index)
      Set the ConstantPool index of the ClassEntry for this class. Unlike most other low-level methods, the index will be checked against the pool immediately; classes must have a valid name at all times.
    • getName

      public String getName()
      Return the name of this class, including package name. The name will be in a form suitable for a Class.forName(java.lang.String) call.
    • getClassName

      public String getClassName()
      Return the name of the class only, without package.
    • getPackageName

      public String getPackageName()
      Return the package name only, without class, or null if none.
    • setName

      public void setName(String name)
      Set the name of this class, including package name.
    • getType

      public Class getType()
      Return the Class object for this class, if it is loadable.
    • getComponentName

      public String getComponentName()
      Return the component type name of this class, or null if not an array. The name will be in a form suitable for a Class.forName(java.lang.String) call.
    • getComponentType

      public Class getComponentType()
      Return the component type of this class, or null if not an array.
    • getComponentBC

      public BCClass getComponentBC()
      Return the component type of this class, or null if not an array.
    • getSuperclassIndex

      public int getSuperclassIndex()
      Return the ConstantPool index of the ClassEntry for the superclass of this class. Returns -1 if the class does not have a constant pool (such as a primitive or array).
    • setSuperclassIndex

      public void setSuperclassIndex(int index)
      Set the ConstantPool index of the ClassEntry for the superclass of this class.
    • getSuperclassName

      public String getSuperclassName()
      Return the name of the superclass for this class, including package name. The name will be in a form suitable for a Class.forName(java.lang.String) call, or null for types without superclasses.
    • getSuperclassType

      public Class getSuperclassType()
      Return the Class object for the superclass of this class, if it is loadable. Returns null for types without superclasses.
    • getSuperclassBC

      public BCClass getSuperclassBC()
      Return the bytecode of the superclass of this class, or null for types without superclasses.
    • setSuperclass

      public void setSuperclass(String name)
      Set the superclass of this class.
    • setSuperclass

      public void setSuperclass(Class type)
      Set the superclass of this class.
    • setSuperclass

      public void setSuperclass(BCClass type)
      Set the superclass of this class.
    • getDeclaredInterfaceIndexes

      public int[] getDeclaredInterfaceIndexes()
      Return the list of ConstantPool indexes of the ClassEntrys describing all the interfaces this class declares that it implements/extends.
      Returns:
      the implmented interfaces, or an empty array if none
    • setDeclaredInterfaceIndexes

      public void setDeclaredInterfaceIndexes(int[] interfaceIndexes)
      Set the list of ConstantPool indexes of the ClassEntrys describing all the interfaces this class declares it implements/extends; set to null or an empty array if none.
    • getDeclaredInterfaceNames

      public String[] getDeclaredInterfaceNames()
      Return the names of the interfaces declared for this class, including package names, or an empty array if none. The names will be in a form suitable for a Class.forName(java.lang.String) call.
    • getDeclaredInterfaceTypes

      public Class[] getDeclaredInterfaceTypes()
      Return the Class objects for the declared interfaces of this class, or an empty array if none.
    • getDeclaredInterfaceBCs

      public BCClass[] getDeclaredInterfaceBCs()
      Return the bytecode for the declared interfaces of this class, or an empty array if none.
    • setDeclaredInterfaces

      public void setDeclaredInterfaces(String[] interfaces)
      Set the interfaces declared implemented/extended by this class; set to null or an empty array if none.
    • setDeclaredInterfaces

      public void setDeclaredInterfaces(Class[] interfaces)
      Set the interfaces declared implemented/extended by this class; set to null or an empty array if none.
    • setDeclaredInterfaces

      public void setDeclaredInterfaces(BCClass[] interfaces)
      Set the interfaces declared implemented/extended by this class; set to null or an empty array if none.
    • getInterfaceNames

      public String[] getInterfaceNames()
      Return the names of all unique interfaces implemented by this class, including those of all superclasses. The names will be returned in a form suitable for a Class.forName(java.lang.String) call. This method does not recurse into interfaces-of-interfaces.
    • getInterfaceTypes

      public Class[] getInterfaceTypes()
      Return the Class objects of all unique interfaces implemented by this class, including those of all superclasses. This method does not recurse into interfaces-of-interfaces.
    • getInterfaceBCs

      public BCClass[] getInterfaceBCs()
      Return the bytecode of all unique interfaces implemented by this class, including those of all superclasses. This method does not recurse into interfaces-of-interfaces.
    • clearDeclaredInterfaces

      public void clearDeclaredInterfaces()
      Clear this class of all interface declarations.
    • removeDeclaredInterface

      public boolean removeDeclaredInterface(String name)
      Remove an interface declared by this class.
      Returns:
      true if the class had the interface, false otherwise
    • removeDeclaredInterface

      public boolean removeDeclaredInterface(Class type)
      Remove an interface declared by this class.
      Returns:
      true if the class had the interface, false otherwise
    • removeDeclaredInterface

      public boolean removeDeclaredInterface(BCClass type)
      Remove an interface declared by this class.
      Returns:
      true if the class had the interface, false otherwise
    • moveDeclaredInterface

      public void moveDeclaredInterface(int fromIdx, int toIdx)
      Rearrange declared interface order.
    • declareInterface

      public void declareInterface(String name)
      Add an interface to those declared by this class.
    • declareInterface

      public void declareInterface(Class type)
      Add an interface to those declared by this class.
    • declareInterface

      public void declareInterface(BCClass type)
      Add an interface to those declared by this class.
    • isInstanceOf

      public boolean isInstanceOf(String name)
      Return true if this class or any of its superclasses implement/extend the given interface/class. This method does not recurse into interfaces-of-interfaces.
    • isInstanceOf

      public boolean isInstanceOf(Class type)
      Return true if this class or any of its superclasses implement/extend the given interface/class. This method does not recurse into interfaces-of-interfaces.
    • isInstanceOf

      public boolean isInstanceOf(BCClass type)
      Return true if this class or any of its superclasses implement/extend the given interface/class. This method does not recurse into interfaces-of-interfaces.
    • getDeclaredFields

      public BCField[] getDeclaredFields()
      Return all the declared fields of this class, or an empty array if none.
    • getDeclaredField

      public BCField getDeclaredField(String name)
      Return the declared field with the given name, or null if none.
    • getFields

      public BCField[] getFields()
      Return all the fields of this class, including those of all superclasses, or an empty array if none.
    • getFields

      public BCField[] getFields(String name)
      Return all fields with the given name, including those of all superclasses, or an empty array if none.
    • setDeclaredFields

      public void setDeclaredFields(BCField[] fields)
      Set the fields for this class; this method is useful for importing all fields from another class. Set to null or empty array if none.
    • declareField

      public BCField declareField(BCField field)
      Import the information from given field as a new field in this class.
      Returns:
      the added field
    • declareField

      public BCField declareField(String name, String type)
      Add a field to this class.
      Returns:
      the added field
    • declareField

      public BCField declareField(String name, Class type)
      Add a field to this class.
      Returns:
      the added field
    • declareField

      public BCField declareField(String name, BCClass type)
      Add a field to this class.
      Returns:
      the added field
    • clearDeclaredFields

      public void clearDeclaredFields()
      Clear all fields from this class.
    • removeDeclaredField

      public boolean removeDeclaredField(String name)
      Remove a field from this class. After this method, the removed field will be invalid, and the result of any operations on it is undefined.
      Returns:
      true if this class contained the field, false otherwise
    • removeDeclaredField

      public boolean removeDeclaredField(BCField field)
      Remove a field from this class. After this method, the removed field will be invalid, and the result of any operations on it is undefined.
      Returns:
      true if this class contained the field, false otherwise
    • moveDeclaredField

      public void moveDeclaredField(int fromIdx, int toIdx)
      Rearrange declared field order.
    • getDeclaredMethods

      public BCMethod[] getDeclaredMethods()
      Return all methods declared by this class. Constructors and static initializers are included.
    • getDeclaredMethod

      public BCMethod getDeclaredMethod(String name)
      Return the declared method with the given name, or null if none. If multiple methods are declared with the given name, which is returned is undefined. Note that in bytecode, constructors are named <init> and static initializers are named <clinit>.
    • getDeclaredMethods

      public BCMethod[] getDeclaredMethods(String name)
      Return all the declared methods with the given name, or an empty array if none. Note that in bytecode, constructors are named <init> and static initializers are named <clinit>.
    • getDeclaredMethod

      public BCMethod getDeclaredMethod(String name, String[] paramTypes)
      Return the declared method with the given name and parameter types, or null if none. If multiple methods are declared with the given name and parameters, which is returned is undefined. Note that in bytecode, constructors are named <init> and static initializers are named <clinit>.
    • getDeclaredMethod

      public BCMethod getDeclaredMethod(String name, Class[] paramTypes)
      Return the declared method with the given name and parameter types, or null if none. If multiple methods are declared with the given name and parameters, which is returned is undefined. Note that in bytecode, constructors are named <init> and static initializers are named <clinit>.
    • getDeclaredMethod

      public BCMethod getDeclaredMethod(String name, BCClass[] paramTypes)
      Return the declared method with the given name and parameter types, or null if none. If multiple methods are declared with the given name and parameters, which is returned is undefined. Note that in bytecode, constructors are named <init> and static initializers are named <clinit>.
    • getDeclaredMethods

      public BCMethod[] getDeclaredMethods(String name, String[] paramTypes)
      Return all declared methods with the given name and parameter types. Note that in bytecode, constructors are named <init> and static initializers are named <clinit>.
    • getDeclaredMethods

      public BCMethod[] getDeclaredMethods(String name, Class[] paramTypes)
      Return all declared methods with the given name and parameter types. Note that in bytecode, constructors are named <init> and static initializers are named <clinit>.
    • getDeclaredMethods

      public BCMethod[] getDeclaredMethods(String name, BCClass[] paramTypes)
      Return all declared methods with the given name and parameter types. Note that in bytecode, constructors are named <init> and static initializers are named <clinit>.
    • getDeclaredMethod

      public BCMethod getDeclaredMethod(String name, String returnType, String[] paramTypes)
      Return the declared method with the given name and signature, or null if none. Note that in bytecode, constructors are named <init> and static initializers are named <clinit>.
    • getDeclaredMethod

      public BCMethod getDeclaredMethod(String name, Class returnType, Class[] paramTypes)
      Return the declared method with the given name and signature, or null if none. Note that in bytecode, constructors are named <init> and static initializers are named <clinit>.
    • getDeclaredMethod

      public BCMethod getDeclaredMethod(String name, BCClass returnType, BCClass[] paramTypes)
      Return the declared method with the given name and signature, or null if none. Note that in bytecode, constructors are named <init> and static initializers are named <clinit>.
    • getMethods

      public BCMethod[] getMethods()
      Return the methods of this class, including those of all superclasses, or an empty array if none. The base version of methods that are overridden will be included, as will all constructors and static initializers. The methods will be ordered from those in the most-specific type up to those in Object.
    • getMethods

      public BCMethod[] getMethods(String name)
      Return the methods with the given name, including those of all superclasses, or an empty array if none. Note that in bytecode, constructors are named <init> and static initializers are named <clinit>.
    • getMethods

      public BCMethod[] getMethods(String name, String[] paramTypes)
      Return the methods with the given name and parameter types, including those of all superclasses, or an empty array if none. Note that in bytecode, constructors are named <init> and static initializers are named <clinit>.
    • getMethods

      public BCMethod[] getMethods(String name, Class[] paramTypes)
      Return the methods with the given name and parameter types, including those of all superclasses, or an empty array if none. Note that in bytecode, constructors are named <init> and static initializers are named <clinit>.
    • getMethods

      public BCMethod[] getMethods(String name, BCClass[] paramTypes)
      Return the methods with the given name and parameter types, including those of all superclasses, or an empty array if none. Note that in bytecode, constructors are named <init> and static initializers are named <clinit>.
    • setDeclaredMethods

      public void setDeclaredMethods(BCMethod[] methods)
      Set the methods for this class; this method is useful for importing all methods from another class. Set to null or empty array if none.
    • declareMethod

      public BCMethod declareMethod(BCMethod method)
      Import the information in the given method as a new method of this class.
      Returns:
      the added method
    • declareMethod

      public BCMethod declareMethod(String name, String returnType, String[] paramTypes)
      Add a method to this class. Note that in bytecode, constructors are named <init> and static initializers are named <clinit>.
      Returns:
      the added method
    • declareMethod

      public BCMethod declareMethod(String name, Class returnType, Class[] paramTypes)
      Add a method to this class. Note that in bytecode, constructors are named <init> and static initializers are named <clinit>.
      Returns:
      the added method
    • declareMethod

      public BCMethod declareMethod(String name, BCClass returnType, BCClass[] paramTypes)
      Add a method to this class. Note that in bytecode, constructors are named <init> and static initializers are named <clinit>.
      Returns:
      the added method
    • clearDeclaredMethods

      public void clearDeclaredMethods()
      Clear all declared methods from this class.
    • removeDeclaredMethod

      public boolean removeDeclaredMethod(String name)
      Remove a method from this class. After this method, the removed method will be invalid, and the result of any operations on it is undefined. If multiple methods match the given name, which is removed is undefined. Note that in bytecode, constructors are named <init> and static initializers are named <clinit>.
      Returns:
      true if this class contained the method, false otherwise
    • removeDeclaredMethod

      public boolean removeDeclaredMethod(BCMethod method)
      Removes a method from this class. After this method, the removed method will be invalid, and the result of any operations on it is undefined.
      Returns:
      true if this class contained the method, false otherwise
    • removeDeclaredMethod

      public boolean removeDeclaredMethod(String name, String[] paramTypes)
      Removes a method from this class. After this method, the removed method will be invalid, and the result of any operations on it is undefined. Note that in bytecode, constructors are named <init> and static initializers are named <clinit>.
      Returns:
      true if this class contained the method, false otherwise
    • removeDeclaredMethod

      public boolean removeDeclaredMethod(String name, Class[] paramTypes)
      Removes a method from this class. After this method, the removed method will be invalid, and the result of any operations on it is undefined. Note that in bytecode, constructors are named <init> and static initializers are named <clinit>.
      Returns:
      true if this class contained the method, false otherwise
    • removeDeclaredMethod

      public boolean removeDeclaredMethod(String name, BCClass[] paramTypes)
      Removes a method from this class. After this method, the removed method will be invalid, and the result of any operations on it is undefined. Note that in bytecode, constructors are named <init> and static initializers are named <clinit>.
      Returns:
      true if this class contained the method, false otherwise
    • moveDeclaredMethod

      public void moveDeclaredMethod(int fromIdx, int toIdx)
      Rearrange method order.
    • addDefaultConstructor

      public BCMethod addDefaultConstructor()
      Convenience method to add a default constructor to this class. If a default constructor already exists, this method will return it without modification. This method can only be called if the superclass has been set.
      Returns:
      the default constructor
    • getSourceFile

      public SourceFile getSourceFile(boolean add)
      Return source file information for the class. Acts internally through the Attributes interface.
      Parameters:
      add - if true, a new source file attribute will be added if not already present
      Returns:
      the source file information, or null if none and the add param is set to false
    • removeSourceFile

      public boolean removeSourceFile()
      Remove the source file attribute for the class. Acts internally through the Attributes interface.
      Returns:
      true if there was a file to remove
    • getInnerClasses

      public InnerClasses getInnerClasses(boolean add)
      Return inner classes information for the class. Acts internally through the Attributes interface.
      Parameters:
      add - if true, a new inner classes attribute will be added if not already present
      Returns:
      the inner classes information, or null if none and the add param is set to false
    • removeInnerClasses

      public boolean removeInnerClasses()
      Remove the inner classes attribute for the class. Acts internally through the Attributes interface.
      Returns:
      true if there was an attribute to remove
    • isDeprecated

      public boolean isDeprecated()
      Convenience method to return deprecation information for the class. Acts internally through the Attributes interface.
    • setDeprecated

      public void setDeprecated(boolean on)
      Convenience method to set whether this class should be considered deprecated. Acts internally through the Attributes interface.
    • 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 one.
      Specified by:
      acceptVisit in interface VisitAcceptor
    • getProject

      public Project getProject()
      Description copied from interface: BCEntity
      Return the project of the current class.
      Specified by:
      getProject in interface BCEntity
    • getPool

      public ConstantPool getPool()
      Description copied from interface: BCEntity
      Return the constant pool of the current class.
      Specified by:
      getPool in interface BCEntity
    • getClassLoader

      public ClassLoader getClassLoader()
      Description copied from interface: BCEntity
      Return the class loader to use when loading related classes.
      Specified by:
      getClassLoader in interface BCEntity
    • isValid

      public boolean isValid()
      Description copied from interface: BCEntity
      Return false if this entity has been removed from its parent; in this case the results of any operations on the entity are undefined.
      Specified by:
      isValid in interface BCEntity