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 intMajor version number for Java 7.private JavaClassBase class information as loaded by BCEL.private ClassItemAdded default constructor for class.private FileActual class file information.private ClassGenModified class generator (lazy create, only if needed).private ConstantPoolGenConstant pool generator for modified class (lazy create, only if needed).private intCached hash code value for class.private intDepth 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 InstructionBuilderInstruction factory for modified class (lazy create, only if needed).protected String[]Names of all interfaces directly implemented by this class.private booleanBinding code can be added to class flag.private booleanHash code computation for class is current flag.private booleanFlag for class modified.private booleanClass in same package as superclass flag.private booleanFile is writable flag.private HashMapMap to class item information.private Method[]All methods defined by this class or interface (lazy create, only if needed).private StringFully qualified class name.private StringLoad path (used to report source of unmodifiable class).private FileDirectory root for class.private StringSignature for class as type.private HashMapMap for method names with possibly generated suffixes (lazy create, only if needed).protected ClassFileSuper 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 TypeClass as type.private intSuffix number for making method names unique (lazy computation).private intUsage count for this class.static final intstatic final intprotected static final intstatic final intstatic final intprivate static URLClassLoaderDirect class loader.private static ClassPathSingleton loader from classpath.static final int -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateConstructor 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 voidaccumulateInterfaces(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 initialintvalue.Add field to class with initialStringvalue.booleanaddInterface(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.voidFinalize current modified state of class.protected intComputes 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.deriveClassName(String prefix, String suffix) Derive generated class name.static booleanequalFieldOrMethods(FieldOrMethod a, FieldOrMethod b) Compare two field or method items to see if they're equal.static booleanequalMethods(Method a, Method b) Compare two methods to see if they're equal.booleanCheck if objects are equal.protected FieldgetAccessibleField(String name) Get internal information for field.protected MethodgetAccessibleMethod(String name, String sig) Get internal information for method without respect to potential trailing arguments or return value.private MethodgetBestAccessibleMethod(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 ClassFilegetClassFile(String name) Constructor for preexisting class file from classpath.private ClassGenGet generator for modifying class.static ClassLoaderGet the classloader used for classes referenced in the binding.ConstantPoolGenGet constant pool generator for modifying class.protected FieldgetDefinedField(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.JavaClassGet 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.TypegetType()Get class as type.intGet use count for class.inthashCode()Get hash code.intIncrement use count for class.private voidinit(String name, String path, InputStream ins) Internal initialization method.private voidRetrieve superinterfaces for an interface class.booleanCheck if class is abstract.booleanisAccessible(ClassItem item) Check accessible method.booleanisArray()Check if class is an array.private static booleanisAssignmentCompatible(Type have, Type need) Check if one type is assignment compatible with another type.booleanCheck if class is in complete state.booleanCheck if binding methods can be added to class.booleanisImplements(String sig) Check if class implements an interface.booleanCheck if class is an interface.booleanCheck if class is modifiable.booleanCheck if class has been modified.private static booleanisSuffixName(String name) Check if a method name matches the pattern for a generated unique suffix.booleanisSuperclass(String name) Check if another class is a superclass of this one.static ClassTry loading class from classpath.makeUniqueMethodName(String name) Make method name unique with generated suffix.private static booleanmatchAccess(FieldOrMethod item, int access) Check for match to specified access level.voidremoveMethod(Method method) Remove method from class.voidSet class modified flag.static voidSet class paths to be searched.voidsetSuperFile(ClassFile sclas) Set superclass information.voidForce class to unmodifiable state.updateField(String type, String name, int access, String init) Update class field with initialStringvalue.voidWrite out modified class information.voidWrite 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:
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
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 (
nullif 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:
trueif added,falseif 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:
trueif interface is implemented by class,falseif 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:
trueif named class is a superclass of this one,falseif 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
nullif 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
nullif 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
nullif 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
nullif 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
nullif 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
nullif 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:
trueif access level match,falseif 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:
trueif compatible,falseif 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 (nullif indeterminant)args- argument value types (nullif indeterminant)- Returns:
- method information, or
nullif 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 (nullif indeterminant)args- argument value types (nullif indeterminant)- Returns:
- method information, or
nullif 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
nullif 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
nullif 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:
trueif accessible,falseif 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 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
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
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
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:
trueif name matches suffix pattern,falseif 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:
trueif field was present,falseif not
-
deleteMethod
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(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:
trueif the equal,falseif 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:
trueif the equal,falseif 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
nullif not found
-
getClassLoader
Get the classloader used for classes referenced in the binding.- Returns:
- classloader
-