Class 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 Detail

      • 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
      • 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_instBuilder

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

      • ClassFile

        public ClassFile​(String name,
                         File root,
                         File file)
                  throws IOException,
                         org.jibx.runtime.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
        org.jibx.runtime.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 org.jibx.runtime.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:
        org.jibx.runtime.JiBXException - on error loading interface information
    • Method Detail

      • getClassFile

        public static ClassFile getClassFile​(String name)
                                      throws IOException,
                                             org.jibx.runtime.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
        org.jibx.runtime.JiBXException - if error loading superclass or other support file
      • 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 org.apache.bcel.generic.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 org.apache.bcel.classfile.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 org.jibx.runtime.JiBXException
        Accumulate interface signatures recursively.
        Parameters:
        intfs - names of interfaces implemented
        map - map for interfaces already accumulated
        accs - accumulated interface names
        Throws:
        org.jibx.runtime.JiBXException - if configuration error
      • getInstanceSigs

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

        public boolean isImplements​(String sig)
                             throws org.jibx.runtime.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:
        org.jibx.runtime.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 org.apache.bcel.classfile.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 org.apache.bcel.classfile.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 org.jibx.runtime.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:
        org.jibx.runtime.JiBXException - if field not found
      • getMethodItems

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

        protected org.apache.bcel.classfile.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
      • 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
      • getConstPoolGen

        public org.apache.bcel.generic.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​(org.apache.bcel.classfile.Method method)
        Add method to class.
        Parameters:
        method - method to be added
        Returns:
        added method information
      • removeMethod

        public void removeMethod​(org.apache.bcel.classfile.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
      • 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​(org.apache.bcel.classfile.FieldOrMethod a,
                                                  org.apache.bcel.classfile.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​(org.apache.bcel.classfile.Method a,
                                           org.apache.bcel.classfile.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 - Object to compare
        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