Package org.jibx.binding.classes
Class ClassFile
java.lang.Object
org.jibx.binding.classes.ClassFile
Class file information. Wraps the actual class file data as well as
associated management information.
- Author:
- Dennis M. Sosnoski
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final byte[]
static final ClassItem[]
protected static final ExistingMethod[]
private static final int
Major version number for Java 7.private JavaClass
Base class information as loaded by BCEL.private ClassItem
Added default constructor for class.private File
Actual class file information.private ClassGen
Modified class generator (lazy create, only if needed).private ConstantPoolGen
Constant pool generator for modified class (lazy create, only if needed).private int
Cached hash code value for class.private int
Depth of superclass hierarchy for class (lazy computation).private String[]
All classes and interfaces of which this is an instance (lazy create, only if needed.protected InstructionBuilder
Instruction factory for modified class (lazy create, only if needed).protected String[]
Names of all interfaces directly implemented by this class.private boolean
Binding code can be added to class flag.private boolean
Hash code computation for class is current flag.private boolean
Flag for class modified.private boolean
Class in same package as superclass flag.private boolean
File is writable flag.private HashMap
Map to class item information.private Method[]
All methods defined by this class or interface (lazy create, only if needed).private String
Fully qualified class name.private String
Load path (used to report source of unmodifiable class).private File
Directory root for class.private String
Signature for class as type.private HashMap
Map for method names with possibly generated suffixes (lazy create, only if needed).protected ClassFile
Super class of this class (set by caller, since it may require additional information to find the class file).private ClassFile[]
Class files of interfaces extended by interface.private Type
Class as type.private int
Suffix number for making method names unique (lazy computation).private int
Usage count for this class.static final int
static final int
protected static final int
static final int
static final int
private static URLClassLoader
Direct class loader.private static ClassPath
Singleton loader from classpath.static final int
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
Constructor for preexisting class file from classpath.Constructor for preexisting class file.Constructor for new class file.Constructor for synthetic placeholder classfile with no backing class data. -
Method Summary
Modifier and TypeMethodDescriptionprotected void
accumulateInterfaces
(String[] intfs, HashMap map, ArrayList accs) Accumulate interface signatures recursively.Add default constructor to a class.Add field to class without initialization.Add field to class with initialint
value.Add field to class with initialString
value.boolean
addInterface
(String intf) Add interface to class.addMethod
(Method method) Add method to class.addPrivateField
(String type, String name) Add private field to class without initialization.void
Finalize current modified state of class.protected int
Computes a hash code based on characteristics of the class.void
delete()
Delete class file information.boolean
deleteField
(String name) Delete field from class.boolean
deleteMethod
(String name, String sig) Delete method from class.deriveClassName
(String prefix, String suffix) Derive generated class name.static boolean
equalFieldOrMethods
(FieldOrMethod a, FieldOrMethod b) Compare two field or method items to see if they're equal.static boolean
equalMethods
(Method a, Method b) Compare two methods to see if they're equal.boolean
Check if objects are equal.protected Field
getAccessibleField
(String name) Get internal information for field.protected Method
getAccessibleMethod
(String name, String sig) Get internal information for method without respect to potential trailing arguments or return value.private Method
getBestAccessibleMethod
(String name, int access, Type ret, Type[] args) Get information for best matching method.getBestMethod
(String name, String ret, String[] args) Get information for best matching method.getBindingMethods
(String prefix, String[] matches) Get all binding methods currently defined in class.static ClassFile
getClassFile
(String name) Constructor for preexisting class file from classpath.private ClassGen
Get generator for modifying class.static ClassLoader
Get the classloader used for classes referenced in the binding.ConstantPoolGen
Get constant pool generator for modifying class.protected Field
getDefinedField
(String name) Get internal information for field.getDirectField
(String name) Get information for field.Get information for field.Get array of fields defined by class.getFile()
Get actual file for class.Get information for initializer.String[]
Get signatures for all types of which instances of this type are instances.Get instruction builder for modifying class.String[]
Get names of all interfaces implemented by class.Get information for method without respect to potential trailing arguments or return value.Get information for method matching one of several possible signatures.Get array of methods defined by class.private Method[]
Get array of methods defined by class or interface.getName()
Get fully qualified class name.Get package name.JavaClass
Get raw current class information.getRoot()
Get root directory for load path.Get signature for class as type.getStaticMethod
(String name, String sig) Get information for static method without respect to return value.Get superclass information.Get superclass name.Type
getType()
Get class as type.int
Get use count for class.int
hashCode()
Get hash code.int
Increment use count for class.private void
init
(String name, String path, InputStream ins) Internal initialization method.private void
Retrieve superinterfaces for an interface class.boolean
Check if class is abstract.boolean
isAccessible
(ClassItem item) Check accessible method.boolean
isArray()
Check if class is an array.private static boolean
isAssignmentCompatible
(Type have, Type need) Check if one type is assignment compatible with another type.boolean
Check if class is in complete state.boolean
Check if binding methods can be added to class.boolean
isImplements
(String sig) Check if class implements an interface.boolean
Check if class is an interface.boolean
Check if class is modifiable.boolean
Check if class has been modified.private static boolean
isSuffixName
(String name) Check if a method name matches the pattern for a generated unique suffix.boolean
isSuperclass
(String name) Check if another class is a superclass of this one.static Class
Try loading class from classpath.makeUniqueMethodName
(String name) Make method name unique with generated suffix.private static boolean
matchAccess
(FieldOrMethod item, int access) Check for match to specified access level.void
removeMethod
(Method method) Remove method from class.void
Set class modified flag.static void
Set class paths to be searched.void
setSuperFile
(ClassFile sclas) Set superclass information.void
Force class to unmodifiable state.updateField
(String type, String name, int access, String init) Update class field with initialString
value.void
Write out modified class information.void
Write out modified class information.
-
Field Details
-
JAVA7_MAJOR_VERSION
private static final int JAVA7_MAJOR_VERSIONMajor 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
-
EMPTY_BYTES
protected static final byte[] EMPTY_BYTES -
EMPTY_CLASS_ITEMS
-
s_loader
private static ClassPath s_loaderSingleton loader from classpath. -
s_directLoader
Direct class loader. -
m_name
Fully qualified class name. -
m_signature
Signature for class as type. -
m_type
private Type m_typeClass as type. -
m_root
Directory root for class. -
m_path
Load path (used to report source of unmodifiable class). -
m_file
Actual class file information. -
m_isSamePackage
private boolean m_isSamePackageClass in same package as superclass flag. -
m_isWritable
private boolean m_isWritableFile is writable flag. -
m_isExtendable
private boolean m_isExtendableBinding code can be added to class flag. -
m_superClass
Super class of this class (set by caller, since it may require additional information to find the class file). -
m_interfaceNames
Names of all interfaces directly implemented by this class. -
m_superInterfaces
Class files of interfaces extended by interface. -
m_instanceOfs
All classes and interfaces of which this is an instance (lazy create, only if needed. -
m_methods
private Method[] m_methodsAll methods defined by this class or interface (lazy create, only if needed). -
m_curClass
private JavaClass m_curClassBase class information as loaded by BCEL. -
m_genClass
private ClassGen m_genClassModified class generator (lazy create, only if needed). -
m_genPool
private ConstantPoolGen m_genPoolConstant pool generator for modified class (lazy create, only if needed). -
m_instBuilder
Instruction factory for modified class (lazy create, only if needed). -
m_suffixMap
Map for method names with possibly generated suffixes (lazy create, only if needed). -
m_itemMap
Map to class item information. -
m_isModified
private boolean m_isModifiedFlag for class modified. -
m_useCount
private int m_useCountUsage count for this class. -
m_isHashCurrent
private boolean m_isHashCurrentHash code computation for class is current flag. -
m_hashCode
private int m_hashCodeCached hash code value for class. -
m_inheritDepth
private int m_inheritDepthDepth of superclass hierarchy for class (lazy computation). -
m_uniqueIndex
private int m_uniqueIndexSuffix number for making method names unique (lazy computation). -
m_defaultConstructor
Added default constructor for class.
-
-
Constructor Details
-
ClassFile
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 fileJiBXException
- if error in reading class file
-
ClassFile
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 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:
JiBXException
- on error loading interface information
-
ClassFile
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 fileJiBXException
- if error loading superclass or other support file
-
-
Method Details
-
getClassFile
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 fileJiBXException
- if error loading superclass or other support file
-
init
Internal initialization method. This is used to handle common initialization for the constructors.- Parameters:
name
- fully qualified class namepath
- class file pathins
- input stream for class file data- Throws:
JiBXException
- if unable to load class file
-
initInterface
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
Get fully qualified class name.- Returns:
- fully qualified name for class
-
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
Get package name.- Returns:
- package name for class
-
getRoot
Get root directory for load path.- Returns:
- root directory in path used for loading 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
Get superclass name.- Returns:
- fully qualified name of superclass
-
setSuperFile
Set superclass information.- Parameters:
sclas
- superclass information
-
getSuperFile
Get superclass information.- Returns:
- super class information as loaded (
null
if no superclass - java.lang.Object, interface, or primitive)
-
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
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 implementedmap
- map for interfaces already accumulatedaccs
- accumulated interface names- Throws:
JiBXException
- if configuration error
-
getInstanceSigs
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
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
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
Get array of fields defined by class.- Returns:
- array of fields defined by class
-
getDefinedField
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
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
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
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
Get array of methods defined by class.- Returns:
- array of methods defined by class
-
getAccessibleMethod
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
null
if method not found
-
getMethod
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
null
if method not found
-
getMethod
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
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 checkedaccess
- 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 checkedneed
- type needed- Returns:
true
if compatible,false
if not
-
getBestAccessibleMethod
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 nameaccess
- access level required for matching methodsret
- return value type (null
if indeterminant)args
- argument value types (null
if indeterminant)- Returns:
- method information, or
null
if method not found
-
getBestMethod
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 (null
if indeterminant)args
- argument value types (null
if indeterminant)- Returns:
- method information, or
null
if method not found
-
getInitializerMethod
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
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
null
if method not found
-
getBindingMethods
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
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
Get instruction builder for modifying class.- Returns:
- instruction builder for class
-
addMethod
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
Add field to class with initialString
value. 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
Add field to class with initialint
value. 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
Update class field with initialString
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 typename
- field nameaccess
- access flags for fieldinit
- initial value for field- Returns:
- field information
-
addField
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
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
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
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
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
Delete field from class.- Parameters:
name
- field name- Returns:
true
if field was present,false
if not
-
deleteMethod
Delete method from class.- Parameters:
name
- method namesig
- 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 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(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 itemb
- 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 methodb
- second method- Returns:
true
if the equal,false
if not
-
equals
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
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
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
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
Set class paths to be searched.- Parameters:
paths
- ordered set of paths to be searched for class files
-
loadClass
Try loading class from classpath.- Parameters:
name
- fully qualified name of class to be loaded- Returns:
- loaded class, or
null
if not found
-
getClassLoader
Get the classloader used for classes referenced in the binding.- Returns:
- classloader
-