Class ClassFile

java.lang.Object
org.jibx.binding.classes.ClassFile

public class ClassFile extends Object
Class file information. Wraps the actual class file data as well as associated management information.
Author:
Dennis M. Sosnoski
  • Field Details

    • JAVA7_MAJOR_VERSION

      private static final int JAVA7_MAJOR_VERSION
      Major version number for Java 7.
      See Also:
    • PRIVATE_ACCESS

      public static final int PRIVATE_ACCESS
      See Also:
    • PACKAGE_ACCESS

      public static final int PACKAGE_ACCESS
      See Also:
    • PROTECTED_ACCESS

      public static final int PROTECTED_ACCESS
      See Also:
    • PUBLIC_ACCESS

      public static final int PUBLIC_ACCESS
      See Also:
    • SYNTHETIC_ACCESS_FLAG

      public static final int SYNTHETIC_ACCESS_FLAG
      See Also:
    • PRIVATEFIELD_ACCESS

      protected static final int PRIVATEFIELD_ACCESS
    • EMPTY_METHOD_ARRAY

      protected static final ExistingMethod[] EMPTY_METHOD_ARRAY
    • EMPTY_BYTES

      protected static final byte[] EMPTY_BYTES
    • EMPTY_CLASS_ITEMS

      public static final ClassItem[] EMPTY_CLASS_ITEMS
    • s_loader

      private static ClassPath s_loader
      Singleton loader from classpath.
    • s_directLoader

      private static URLClassLoader s_directLoader
      Direct class loader.
    • m_name

      private String m_name
      Fully qualified class name.
    • m_signature

      private String m_signature
      Signature for class as type.
    • m_type

      private Type m_type
      Class as type.
    • m_root

      private File m_root
      Directory root for class.
    • m_path

      private String m_path
      Load path (used to report source of unmodifiable class).
    • m_file

      private File m_file
      Actual class file information.
    • m_isSamePackage

      private boolean m_isSamePackage
      Class in same package as superclass flag.
    • m_isWritable

      private boolean m_isWritable
      File is writable flag.
    • m_isExtendable

      private boolean m_isExtendable
      Binding code can be added to class flag.
    • m_superClass

      protected ClassFile m_superClass
      Super class of this class (set by caller, since it may require additional information to find the class file).
    • m_interfaceNames

      protected String[] m_interfaceNames
      Names of all interfaces directly implemented by this class.
    • m_superInterfaces

      private ClassFile[] m_superInterfaces
      Class files of interfaces extended by interface.
    • m_instanceOfs

      private String[] m_instanceOfs
      All classes and interfaces of which this is an instance (lazy create, only if needed.
    • m_methods

      private Method[] m_methods
      All methods defined by this class or interface (lazy create, only if needed).
    • m_curClass

      private JavaClass m_curClass
      Base class information as loaded by BCEL.
    • m_genClass

      private ClassGen m_genClass
      Modified class generator (lazy create, only if needed).
    • m_genPool

      private ConstantPoolGen m_genPool
      Constant pool generator for modified class (lazy create, only if needed).
    • m_instBuilder

      protected InstructionBuilder m_instBuilder
      Instruction factory for modified class (lazy create, only if needed).
    • m_suffixMap

      private HashMap m_suffixMap
      Map for method names with possibly generated suffixes (lazy create, only if needed).
    • m_itemMap

      private HashMap m_itemMap
      Map to class item information.
    • m_isModified

      private boolean m_isModified
      Flag for class modified.
    • m_useCount

      private int m_useCount
      Usage count for this class.
    • m_isHashCurrent

      private boolean m_isHashCurrent
      Hash code computation for class is current flag.
    • m_hashCode

      private int m_hashCode
      Cached hash code value for class.
    • m_inheritDepth

      private int m_inheritDepth
      Depth of superclass hierarchy for class (lazy computation).
    • m_uniqueIndex

      private int m_uniqueIndex
      Suffix number for making method names unique (lazy computation).
    • m_defaultConstructor

      private ClassItem m_defaultConstructor
      Added default constructor for class.
  • Constructor Details

    • ClassFile

      public ClassFile(String name, File root, File file) throws IOException, JiBXException
      Constructor for preexisting class file. Loads the class data and prepares it for use.
      Parameters:
      name - fully qualified class name
      root - directory root from class loading path list
      file - actual class file
      Throws:
      IOException - if unable to open file
      JiBXException - if error in reading class file
    • ClassFile

      public ClassFile(String name, String sig)
      Constructor for synthetic placeholder classfile with no backing class data.
      Parameters:
      name - fully qualified class name
      sig - corresponding class signature
    • ClassFile

      public ClassFile(String name, File root, ClassFile sclas, int access, String[] impls) throws JiBXException
      Constructor for new class file. Initializes the class data and prepares it for use.
      Parameters:
      name - fully qualified class name
      root - directory root from class loading path list
      sclas - superclass of new class
      access - access flags for class
      impls - array of interfaces implemented by new class (non-null, empty if none)
      Throws:
      JiBXException - on error loading interface information
    • ClassFile

      private ClassFile(String name, ClassPath.ClassFile cf) throws JiBXException, IOException
      Constructor for preexisting class file from classpath. Loads the class data and prepares it for use.
      Parameters:
      name - fully qualified class name
      Throws:
      IOException - if unable to open file
      JiBXException - if error loading superclass or other support file
  • Method Details

    • getClassFile

      public static ClassFile getClassFile(String name) throws IOException, JiBXException
      Constructor for preexisting class file from classpath. Loads the class data and prepares it for use.
      Parameters:
      name - fully qualified class name
      Returns:
      null if unable to find class file
      Throws:
      IOException - if error reading file
      JiBXException - if error loading superclass or other support file
    • init

      private void init(String name, String path, InputStream ins) throws JiBXException
      Internal initialization method. This is used to handle common initialization for the constructors.
      Parameters:
      name - fully qualified class name
      path - class file path
      ins - input stream for class file data
      Throws:
      JiBXException - if unable to load class file
    • initInterface

      private void initInterface() throws JiBXException
      Retrieve superinterfaces for an interface class. These are collected at initialization so that we can support getting the full set of methods later without worrying about throwing an exception.
      Throws:
      JiBXException - on error loading interface information
    • isInterface

      public boolean isInterface()
      Check if class is an interface. This only checks existing classes, assuming that no generated classes are interfaces.
      Returns:
      true if an interface, false if not
    • isAbstract

      public boolean isAbstract()
      Check if class is abstract. This only checks existing classes, assuming that no generated classes are abstract.
      Returns:
      true if an abstract class, false if not
    • isArray

      public boolean isArray()
      Check if class is an array. This only checks existing classes, assuming that no generated classes are arrays.
      Returns:
      true if an array class, false if not
    • isModifiable

      public boolean isModifiable()
      Check if class is modifiable.
      Returns:
      true if class is modifiable, false if not
    • isExtendable

      public boolean isExtendable()
      Check if binding methods can be added to class.
      Returns:
      true if methods can be added, false if not
    • getName

      public String getName()
      Get fully qualified class name.
      Returns:
      fully qualified name for class
    • getSignature

      public String getSignature()
      Get signature for class as type.
      Returns:
      signature for class used as type
    • getType

      public Type getType()
      Get class as type.
      Returns:
      class as type
    • getPackage

      public String getPackage()
      Get package name.
      Returns:
      package name for class
    • getRoot

      public File getRoot()
      Get root directory for load path.
      Returns:
      root directory in path used for loading file
    • getFile

      public File getFile()
      Get actual file for class.
      Returns:
      file used for class
    • getRawClass

      public JavaClass getRawClass()
      Get raw current class information.
      Returns:
      raw current class information
    • getSuperName

      public String getSuperName()
      Get superclass name.
      Returns:
      fully qualified name of superclass
    • setSuperFile

      public void setSuperFile(ClassFile sclas)
      Set superclass information.
      Parameters:
      sclas - superclass information
    • getSuperFile

      public ClassFile getSuperFile()
      Get superclass information.
      Returns:
      super class information as loaded (null if no superclass - java.lang.Object, interface, or primitive)
    • getInterfaces

      public String[] getInterfaces()
      Get names of all interfaces implemented by class.
      Returns:
      names of all interfaces implemented directly by class (non-null, empty array if none)
    • addInterface

      public boolean addInterface(String intf)
      Add interface to class. The interface is added to the class if not already defined.
      Parameters:
      intf - fully qualified interface name
      Returns:
      true if added, false if already present
    • accumulateInterfaces

      protected void accumulateInterfaces(String[] intfs, HashMap map, ArrayList accs) throws JiBXException
      Accumulate interface signatures recursively.
      Parameters:
      intfs - names of interfaces implemented
      map - map for interfaces already accumulated
      accs - accumulated interface names
      Throws:
      JiBXException - if configuration error
    • getInstanceSigs

      public String[] getInstanceSigs() throws JiBXException
      Get signatures for all types of which instances of this type are instances.
      Returns:
      all signatures suppored by instances
      Throws:
      JiBXException - if configuration error
    • isImplements

      public boolean isImplements(String sig) throws JiBXException
      Check if class implements an interface.
      Parameters:
      sig - signature of interface to be checked
      Returns:
      true if interface is implemented by class, false if not
      Throws:
      JiBXException - if configuration error
    • isSuperclass

      public boolean isSuperclass(String name)
      Check if another class is a superclass of this one.
      Parameters:
      name - of superclass to be checked
      Returns:
      true if named class is a superclass of this one, false if not
    • getFieldItems

      public ClassItem[] getFieldItems()
      Get array of fields defined by class.
      Returns:
      array of fields defined by class
    • getDefinedField

      protected Field getDefinedField(String name)
      Get internal information for field. This can only be used with existing classes, and only checks for fields that are actually members of the class (not superclasses).
      Parameters:
      name - field name
      Returns:
      field information, or null if field not found
    • getAccessibleField

      protected Field getAccessibleField(String name)
      Get internal information for field. This can only be used with existing classes. If the field is not found directly, superclasses are checked for inherited fields matching the supplied name.
      Parameters:
      name - field name
      Returns:
      field information, or null if field not found
    • getDirectField

      public ClassItem getDirectField(String name)
      Get information for field. This can only be used with existing classes, and only checks for fields that are actually members of the class (not superclasses).
      Parameters:
      name - field name
      Returns:
      field information, or null if field not found
    • getField

      public ClassItem getField(String name) throws JiBXException
      Get information for field. This can only be used with existing classes. If the field is not found directly, superclasses are checked for inherited fields matching the supplied name.
      Parameters:
      name - field name
      Returns:
      field information
      Throws:
      JiBXException - if field not found
    • getMethods

      private Method[] getMethods()
      Get array of methods defined by class or interface. In the case of an interface, this merges all methods from superinterfaces in the array returned.
      Returns:
      array of methods defined by class
    • getMethodItems

      public ClassItem[] getMethodItems()
      Get array of methods defined by class.
      Returns:
      array of methods defined by class
    • getAccessibleMethod

      protected Method getAccessibleMethod(String name, String sig)
      Get internal information for method without respect to potential trailing arguments or return value. This can only be used with existing classes. If the method is not found directly, superclasses are checked for inherited methods matching the supplied name. This compares the supplied partial signature against the actual method signature, and considers it a match if the actual sigature starts with the supplied signature..
      Parameters:
      name - method name
      sig - partial method signature to be matched
      Returns:
      method information, or null if method not found
    • getMethod

      public ClassItem getMethod(String name, String sig)
      Get information for method without respect to potential trailing arguments or return value. This can only be used with existing classes. If the method is not found directly, superclasses are checked for inherited methods matching the supplied name. This compares the supplied partial signature against the actual method signature, and considers it a match if the actual sigature starts with the supplied signature..
      Parameters:
      name - method name
      sig - partial method signature to be matched
      Returns:
      method information, or null if method not found
    • getMethod

      public ClassItem getMethod(String name, String[] sigs)
      Get information for method matching one of several possible signatures. This can only be used with existing classes. If a match is not found directly, superclasses are checked for inherited methods matching the supplied name and signatures. The signature variations are checked in the order supplied.
      Parameters:
      name - method name
      sigs - possible signatures for method (including return type)
      Returns:
      method information, or null if method not found
    • matchAccess

      private static boolean matchAccess(FieldOrMethod item, int access)
      Check for match to specified access level. This treats a field or method as matching if the access level is the same as or more open than the required level.
      Parameters:
      item - information for field or method to be checked
      access - required access level for match
      Returns:
      true if access level match, false if not
    • isAssignmentCompatible

      private static boolean isAssignmentCompatible(Type have, Type need)
      Check if one type is assignment compatible with another type. This is an ugly replacement for apparently broken BCEL code.
      Parameters:
      have - type being checked
      need - type needed
      Returns:
      true if compatible, false if not
    • getBestAccessibleMethod

      private Method getBestAccessibleMethod(String name, int access, Type ret, Type[] args)
      Get information for best matching method. This tries to find a method which matches the specified name, return type, and argument types. If an exact match is not found it looks for a method with a return type that is extended or implemented by the specified type and arguments that are extended or implemented by the specified types. This can only be used with existing classes. If the method is not found directly, superclasses are checked for inherited methods.
      Parameters:
      name - method name
      access - access level required for matching methods
      ret - return value type (null if indeterminant)
      args - argument value types (null if indeterminant)
      Returns:
      method information, or null if method not found
    • getBestMethod

      public ClassItem getBestMethod(String name, String ret, String[] args)
      Get information for best matching method. This tries to find a method which matches the specified name, return type, and argument types. If an exact match is not found it looks for a method with a return type that is extended or implemented by the specified type and arguments that are extended or implemented by the specified types. This can only be used with existing classes. If the method is not found directly, superclasses are checked for inherited methods.
      Parameters:
      name - method name
      ret - return value type (null if indeterminant)
      args - argument value types (null if indeterminant)
      Returns:
      method information, or null if method not found
    • getInitializerMethod

      public ClassItem getInitializerMethod(String sig)
      Get information for initializer. This can only be used with existing classes. Only the class itself is checked for an initializer matching the argument list signature.
      Parameters:
      sig - encoded argument list signature
      Returns:
      method information, or null if method not found
    • getStaticMethod

      public ClassItem getStaticMethod(String name, String sig)
      Get information for static method without respect to return value. This can only be used with existing classes. Only the class itself is checked for a method matching the supplied name and argument list signature.
      Parameters:
      name - method name
      sig - encoded argument list signature
      Returns:
      method information, or null if method not found
    • getBindingMethods

      public ExistingMethod[] getBindingMethods(String prefix, String[] matches)
      Get all binding methods currently defined in class. Binding methods are generally identified by a supplied prefix, but additional methods can be specified the the combination of exact name and signature. This is a little kludgy, but necessary to handle the "marshal" method added to mapped classes.
      Parameters:
      prefix - identifying prefix for binding methods
      matches - pairs of method name and signature to be matched as exceptions to the prefix matching
      Returns:
      existing binding methods
    • isAccessible

      public boolean isAccessible(ClassItem item)
      Check accessible method. Check if a field or method in another class is accessible from within this class.
      Parameters:
      item - field or method information
      Returns:
      true if accessible, false if not
    • getClassGen

      private ClassGen getClassGen()
      Get generator for modifying class.
      Returns:
      generator for class
    • getConstPoolGen

      public ConstantPoolGen getConstPoolGen()
      Get constant pool generator for modifying class.
      Returns:
      constant pool generator for class
    • getInstructionBuilder

      public InstructionBuilder getInstructionBuilder()
      Get instruction builder for modifying class.
      Returns:
      instruction builder for class
    • addMethod

      public ClassItem addMethod(Method method)
      Add method to class.
      Parameters:
      method - method to be added
      Returns:
      added method information
    • removeMethod

      public void removeMethod(Method method)
      Remove method from class.
      Parameters:
      method - method to be removed
    • addField

      public ClassItem addField(String type, String name, int access, String init)
      Add field to class with initial String value. If a field with the same name already exists, it is overwritten.
      Parameters:
      type - fully qualified class name of field type
      name - field name
      access - access flags for field
      init - initial value for field
      Returns:
      field information
    • addField

      public ClassItem addField(String type, String name, int access, int init)
      Add field to class with initial int value. If a field with the same name already exists, it is overwritten.
      Parameters:
      type - fully qualified class name of field type
      name - field name
      access - access flags for field
      init - initial value for field
      Returns:
      field information
    • updateField

      public ClassItem updateField(String type, String name, int access, String init)
      Update class field with initial String value. If the field already exists with the same characteristics it is left unchanged; otherwise any existing field with the same name is overwritten.
      Parameters:
      type - fully qualified class name of field type
      name - field name
      access - access flags for field
      init - initial value for field
      Returns:
      field information
    • addField

      public ClassItem addField(String type, String name, int access)
      Add field to class without initialization. If a field with the same name already exists, it is overwritten.
      Parameters:
      type - fully qualified class name of field type
      name - field name
      access - access flags for field
      Returns:
      field information
    • addPrivateField

      public ClassItem addPrivateField(String type, String name)
      Add private field to class without initialization. If a field with the same name already exists, it is overwritten.
      Parameters:
      type - fully qualified class name of field type
      name - field name
      Returns:
      field information
    • addDefaultConstructor

      public ClassItem addDefaultConstructor()
      Add default constructor to a class. The added default constructor just calls the default constructor for the superclass. If the superclass doesn't have a default constructor, this method is called recursively to add one if possible.
      Returns:
      constructor information
    • isSuffixName

      private static boolean isSuffixName(String name)
      Check if a method name matches the pattern for a generated unique suffix.
      Parameters:
      name - method name to be checked
      Returns:
      true if name matches suffix pattern, false if not
    • makeUniqueMethodName

      public String makeUniqueMethodName(String name)
      Make method name unique with generated suffix. The suffixed method name is tracked so that it will not be used again.
      Parameters:
      name - base name before suffix is appended
      Returns:
      name with unique suffix appended
    • deleteField

      public boolean deleteField(String name)
      Delete field from class.
      Parameters:
      name - field name
      Returns:
      true if field was present, false if not
    • deleteMethod

      public boolean deleteMethod(String name, String sig)
      Delete method from class.
      Parameters:
      name - method name
      sig - method signature
      Returns:
      true if method was present, false if not
    • getUseCount

      public int getUseCount()
      Get use count for class.
      Returns:
      use count for this class
    • incrementUseCount

      public int incrementUseCount()
      Increment use count for class.
      Returns:
      use count (after increment)
    • setUnmodifiable

      public void setUnmodifiable()
      Force class to unmodifiable state. This prevents any additions, changes, deletions to the file.
    • isModified

      public boolean isModified()
      Check if class has been modified.
      Returns:
      true if class is modified, false if not
    • setModified

      public void setModified()
      Set class modified flag.
    • isComplete

      public boolean isComplete()
      Check if class is in complete state.
      Returns:
      true if class is complete, false if not
    • computeHashCode

      protected int computeHashCode()
      Computes a hash code based on characteristics of the class. The characteristics used in computing the hash code include the base class, implemented interfaces, method names, field names, and package, but not the actual class name or signature. The current static version of the class is used for this computation, so if the class is being modified codeComplete() should be called before this method. Note that this is designed for use with the classes generated by JiBX, and is not necessarily a good model for general usage.
      Returns:
      computed hash code value
    • codeComplete

      public void codeComplete()
      Finalize current modified state of class. This converts the modified class state into a static form, then computes a hash code based on characteristics of the class. If the class has not been modified it just computes the hash code. Note that this won't initialize the array of superinterfaces if used with an interface, but that shouldn't be a problem since we don't create any interfaces.
    • hashCode

      public int hashCode()
      Get hash code. This is based on most characteristics of the class, including the actual methods, but excluding the class name. It is only valid after the codeComplete() method is called.
      Overrides:
      hashCode in class Object
      Returns:
      hash code based on code sequence
    • equalFieldOrMethods

      public static boolean equalFieldOrMethods(FieldOrMethod a, FieldOrMethod b)
      Compare two field or method items to see if they're equal. This handles only the comparisons that apply to both fields and methods. It does not include comparing access flags, since these may be different due to access requirements.
      Parameters:
      a - first field or method item
      b - second field or method item
      Returns:
      true if the equal, false if not
    • equalMethods

      public static boolean equalMethods(Method a, Method b)
      Compare two methods to see if they're equal. This checks only the details of the exception handling and actual code, not the name or signature.
      Parameters:
      a - first method
      b - second method
      Returns:
      true if the equal, false if not
    • equals

      public boolean equals(Object obj)
      Check if objects are equal. Compares first based on hash code, then on the actual contents of the class, including package, implemented interfaces, superclass, methods, and fields (but not the actual class name). It is only valid after the codeComplete() method is called.
      Overrides:
      equals in class Object
      Parameters:
      obj -
      Returns:
      true if equal objects, false if not
    • delete

      public void delete()
      Delete class file information. Deletes the class file for this class, if it exists. Does nothing if no class file has been generated.
    • writeFile

      public void writeFile(OutputStream os) throws IOException
      Write out modified class information. Writes the modified class file to an output stream.
      Parameters:
      os - output stream for writing modified class
      Throws:
      IOException - if error writing to file
    • writeFile

      public void writeFile() throws IOException
      Write out modified class information. Writes the modified class file back out to the original file. If the class file has not been modified, the original file is kept unchanged.
      Throws:
      IOException - if error writing to file
    • deriveClassName

      public String deriveClassName(String prefix, String suffix)
      Derive generated class name. This generates a JiBX class name from the name of this class, using the supplied prefix and suffix information. The derived class name is always in the same package as this class.
      Parameters:
      prefix - generated class name prefix
      suffix - generated class name suffix
      Returns:
      derived class name
    • setPaths

      public static void setPaths(String[] paths)
      Set class paths to be searched.
      Parameters:
      paths - ordered set of paths to be searched for class files
    • loadClass

      public static Class loadClass(String name)
      Try loading class from classpath.
      Parameters:
      name - fully qualified name of class to be loaded
      Returns:
      loaded class, or null if not found
    • getClassLoader

      public static ClassLoader getClassLoader()
      Get the classloader used for classes referenced in the binding.
      Returns:
      classloader