Package org.jibx.binding.classes
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 Summary
Fields Modifier and Type Field Description protected static byte[]EMPTY_BYTESstatic ClassItem[]EMPTY_CLASS_ITEMSprotected static ExistingMethod[]EMPTY_METHOD_ARRAYprotected InstructionBuilderm_instBuilderInstruction factory for modified class (lazy create, only if needed).protected String[]m_interfaceNamesNames of all interfaces directly implemented by this class.protected ClassFilem_superClassSuper class of this class (set by caller, since it may require additional information to find the class file).static intPACKAGE_ACCESSstatic intPRIVATE_ACCESSprotected static intPRIVATEFIELD_ACCESSstatic intPROTECTED_ACCESSstatic intPUBLIC_ACCESSstatic intSYNTHETIC_ACCESS_FLAG
-
Constructor Summary
Constructors Constructor Description ClassFile(String name, File root, File file)Constructor for preexisting class file.ClassFile(String name, File root, ClassFile sclas, int access, String[] impls)Constructor for new class file.ClassFile(String name, String sig)Constructor for synthetic placeholder classfile with no backing class data.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidaccumulateInterfaces(String[] intfs, HashMap map, ArrayList accs)Accumulate interface signatures recursively.ClassItemaddDefaultConstructor()Add default constructor to a class.ClassItemaddField(String type, String name, int access)Add field to class without initialization.ClassItemaddField(String type, String name, int access, int init)Add field to class with initialintvalue.ClassItemaddField(String type, String name, int access, String init)Add field to class with initialStringvalue.booleanaddInterface(String intf)Add interface to class.ClassItemaddMethod(org.apache.bcel.classfile.Method method)Add method to class.ClassItemaddPrivateField(String type, String name)Add private field to class without initialization.voidcodeComplete()Finalize current modified state of class.protected intcomputeHashCode()Computes a hash code based on characteristics of the class.voiddelete()Delete class file information.booleandeleteField(String name)Delete field from class.booleandeleteMethod(String name, String sig)Delete method from class.StringderiveClassName(String prefix, String suffix)Derive generated class name.static booleanequalFieldOrMethods(org.apache.bcel.classfile.FieldOrMethod a, org.apache.bcel.classfile.FieldOrMethod b)Compare two field or method items to see if they're equal.static booleanequalMethods(org.apache.bcel.classfile.Method a, org.apache.bcel.classfile.Method b)Compare two methods to see if they're equal.booleanequals(Object obj)Check if objects are equal.protected org.apache.bcel.classfile.FieldgetAccessibleField(String name)Get internal information for field.protected org.apache.bcel.classfile.MethodgetAccessibleMethod(String name, String sig)Get internal information for method without respect to potential trailing arguments or return value.ClassItemgetBestMethod(String name, String ret, String[] args)Get information for best matching method.ExistingMethod[]getBindingMethods(String prefix, String[] matches)Get all binding methods currently defined in class.static ClassFilegetClassFile(String name)Constructor for preexisting class file from classpath.static ClassLoadergetClassLoader()Get the classloader used for classes referenced in the binding.org.apache.bcel.generic.ConstantPoolGengetConstPoolGen()Get constant pool generator for modifying class.protected org.apache.bcel.classfile.FieldgetDefinedField(String name)Get internal information for field.ClassItemgetDirectField(String name)Get information for field.ClassItemgetField(String name)Get information for field.ClassItem[]getFieldItems()Get array of fields defined by class.FilegetFile()Get actual file for class.ClassItemgetInitializerMethod(String sig)Get information for initializer.String[]getInstanceSigs()Get signatures for all types of which instances of this type are instances.InstructionBuildergetInstructionBuilder()Get instruction builder for modifying class.String[]getInterfaces()Get names of all interfaces implemented by class.ClassItemgetMethod(String name, String sig)Get information for method without respect to potential trailing arguments or return value.ClassItemgetMethod(String name, String[] sigs)Get information for method matching one of several possible signatures.ClassItem[]getMethodItems()Get array of methods defined by class.StringgetName()Get fully qualified class name.StringgetPackage()Get package name.org.apache.bcel.classfile.JavaClassgetRawClass()Get raw current class information.FilegetRoot()Get root directory for load path.StringgetSignature()Get signature for class as type.ClassItemgetStaticMethod(String name, String sig)Get information for static method without respect to return value.ClassFilegetSuperFile()Get superclass information.StringgetSuperName()Get superclass name.org.apache.bcel.generic.TypegetType()Get class as type.intgetUseCount()Get use count for class.inthashCode()Get hash code.intincrementUseCount()Increment use count for class.booleanisAbstract()Check if class is abstract.booleanisAccessible(ClassItem item)Check accessible method.booleanisArray()Check if class is an array.booleanisComplete()Check if class is in complete state.booleanisExtendable()Check if binding methods can be added to class.booleanisImplements(String sig)Check if class implements an interface.booleanisInterface()Check if class is an interface.booleanisModifiable()Check if class is modifiable.booleanisModified()Check if class has been modified.booleanisSuperclass(String name)Check if another class is a superclass of this one.static ClassloadClass(String name)Try loading class from classpath.StringmakeUniqueMethodName(String name)Make method name unique with generated suffix.voidremoveMethod(org.apache.bcel.classfile.Method method)Remove method from class.voidsetModified()Set class modified flag.static voidsetPaths(String[] paths)Set class paths to be searched.voidsetSuperFile(ClassFile sclas)Set superclass information.voidsetUnmodifiable()Force class to unmodifiable state.ClassItemupdateField(String type, String name, int access, String init)Update class field with initialStringvalue.voidwriteFile()Write out modified class information.voidwriteFile(OutputStream os)Write out modified class information.
-
-
-
Field Detail
-
PRIVATE_ACCESS
public static final int PRIVATE_ACCESS
- See Also:
- Constant Field Values
-
PACKAGE_ACCESS
public static final int PACKAGE_ACCESS
- See Also:
- Constant Field Values
-
PROTECTED_ACCESS
public static final int PROTECTED_ACCESS
- See Also:
- Constant Field Values
-
PUBLIC_ACCESS
public static final int PUBLIC_ACCESS
- See Also:
- Constant Field Values
-
SYNTHETIC_ACCESS_FLAG
public static final int SYNTHETIC_ACCESS_FLAG
- See Also:
- Constant Field Values
-
PRIVATEFIELD_ACCESS
protected static final int PRIVATEFIELD_ACCESS
- See Also:
- Constant Field Values
-
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 nameroot- directory root from class loading path listfile- actual class file- Throws:
IOException- if unable to open fileorg.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 namesig- 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 nameroot- directory root from class loading path listsclas- superclass of new classaccess- access flags for classimpls- 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 fileorg.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:
trueif an interface,falseif not
-
isAbstract
public boolean isAbstract()
Check if class is abstract. This only checks existing classes, assuming that no generated classes are abstract.- Returns:
trueif an abstract class,falseif not
-
isArray
public boolean isArray()
Check if class is an array. This only checks existing classes, assuming that no generated classes are arrays.- Returns:
trueif an array class,falseif not
-
isModifiable
public boolean isModifiable()
Check if class is modifiable.- Returns:
trueif class is modifiable,falseif not
-
isExtendable
public boolean isExtendable()
Check if binding methods can be added to class.- Returns:
trueif methods can be added,falseif 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 (
nullif 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:
trueif added,falseif 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 implementedmap- map for interfaces already accumulatedaccs- 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:
trueif interface is implemented by class,falseif 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:
trueif named class is a superclass of this one,falseif 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
nullif 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
nullif 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
nullif 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 namesig- partial method signature to be matched- Returns:
- method information, or
nullif 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 namesig- partial method signature to be matched- Returns:
- method information, or
nullif 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 namesigs- possible signatures for method (including return type)- Returns:
- method information, or
nullif 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 nameret- return value type (nullif indeterminant)args- argument value types (nullif indeterminant)- Returns:
- method information, or
nullif 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
nullif 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 namesig- encoded argument list signature- Returns:
- method information, or
nullif 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 methodsmatches- 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:
trueif accessible,falseif 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 initialStringvalue. If a field with the same name already exists, it is overwritten.- Parameters:
type- fully qualified class name of field typename- field nameaccess- access flags for fieldinit- initial value for field- Returns:
- field information
-
addField
public ClassItem addField(String type, String name, int access, int init)
Add field to class with initialintvalue. If a field with the same name already exists, it is overwritten.- Parameters:
type- fully qualified class name of field typename- field nameaccess- access flags for fieldinit- initial value for field- Returns:
- field information
-
updateField
public ClassItem updateField(String type, String name, int access, String init)
Update class field with initialStringvalue. 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 typename- field nameaccess- access flags for fieldinit- 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 typename- field nameaccess- 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 typename- 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:
trueif field was present,falseif not
-
deleteMethod
public boolean deleteMethod(String name, String sig)
Delete method from class.- Parameters:
name- method namesig- method signature- Returns:
trueif method was present,falseif 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:
trueif class is modified,falseif not
-
setModified
public void setModified()
Set class modified flag.
-
isComplete
public boolean isComplete()
Check if class is in complete state.- Returns:
trueif class is complete,falseif 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 modifiedcodeComplete()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 thecodeComplete()method is called.
-
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 itemb- second field or method item- Returns:
trueif the equal,falseif 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 methodb- second method- Returns:
trueif the equal,falseif 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 thecodeComplete()method is called.
-
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 IOExceptionWrite 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 prefixsuffix- 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
nullif not found
-
getClassLoader
public static ClassLoader getClassLoader()
Get the classloader used for classes referenced in the binding.- Returns:
- classloader
-
-