public class ClassFile
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
protected static byte[] |
EMPTY_BYTES |
static ClassItem[] |
EMPTY_CLASS_ITEMS |
protected static ExistingMethod[] |
EMPTY_METHOD_ARRAY |
private static int |
JAVA7_MAJOR_VERSION
Major version number for Java 7.
|
private JavaClass |
m_curClass
Base class information as loaded by BCEL.
|
private ClassItem |
m_defaultConstructor
Added default constructor for class.
|
private java.io.File |
m_file
Actual class file information.
|
private ClassGen |
m_genClass
Modified class generator (lazy create, only if needed).
|
private ConstantPoolGen |
m_genPool
Constant pool generator for modified class (lazy create, only if
needed).
|
private int |
m_hashCode
Cached hash code value for class.
|
private int |
m_inheritDepth
Depth of superclass hierarchy for class (lazy computation).
|
private java.lang.String[] |
m_instanceOfs
All classes and interfaces of which this is an instance (lazy create,
only if needed.
|
protected InstructionBuilder |
m_instBuilder
Instruction factory for modified class (lazy create, only if needed).
|
protected java.lang.String[] |
m_interfaceNames
Names of all interfaces directly implemented by this class.
|
private boolean |
m_isExtendable
Binding code can be added to class flag.
|
private boolean |
m_isHashCurrent
Hash code computation for class is current flag.
|
private boolean |
m_isModified
Flag for class modified.
|
private boolean |
m_isSamePackage
Class in same package as superclass flag.
|
private boolean |
m_isWritable
File is writable flag.
|
private java.util.HashMap |
m_itemMap
Map to class item information.
|
private Method[] |
m_methods
All methods defined by this class or interface (lazy create, only if
needed).
|
private java.lang.String |
m_name
Fully qualified class name.
|
private java.lang.String |
m_path
Load path (used to report source of unmodifiable class).
|
private java.io.File |
m_root
Directory root for class.
|
private java.lang.String |
m_signature
Signature for class as type.
|
private java.util.HashMap |
m_suffixMap
Map for method names with possibly generated suffixes (lazy create, only
if needed).
|
protected ClassFile |
m_superClass
Super class of this class (set by caller, since it may require
additional information to find the class file).
|
private ClassFile[] |
m_superInterfaces
Class files of interfaces extended by interface.
|
private Type |
m_type
Class as type.
|
private int |
m_uniqueIndex
Suffix number for making method names unique (lazy computation).
|
private int |
m_useCount
Usage count for this class.
|
static int |
PACKAGE_ACCESS |
static int |
PRIVATE_ACCESS |
protected static int |
PRIVATEFIELD_ACCESS |
static int |
PROTECTED_ACCESS |
static int |
PUBLIC_ACCESS |
private static java.net.URLClassLoader |
s_directLoader
Direct class loader.
|
private static ClassPath |
s_loader
Singleton loader from classpath.
|
static int |
SYNTHETIC_ACCESS_FLAG |
Modifier | Constructor and Description |
---|---|
private |
ClassFile(java.lang.String name,
ClassPath.ClassFile cf)
Constructor for preexisting class file from classpath.
|
|
ClassFile(java.lang.String name,
java.io.File root,
ClassFile sclas,
int access,
java.lang.String[] impls)
Constructor for new class file.
|
|
ClassFile(java.lang.String name,
java.io.File root,
java.io.File file)
Constructor for preexisting class file.
|
|
ClassFile(java.lang.String name,
java.lang.String sig)
Constructor for synthetic placeholder classfile with no backing class
data.
|
Modifier and Type | Method and Description |
---|---|
protected void |
accumulateInterfaces(java.lang.String[] intfs,
java.util.HashMap map,
java.util.ArrayList accs)
Accumulate interface signatures recursively.
|
ClassItem |
addDefaultConstructor()
Add default constructor to a class.
|
ClassItem |
addField(java.lang.String type,
java.lang.String name,
int access)
Add field to class without initialization.
|
ClassItem |
addField(java.lang.String type,
java.lang.String name,
int access,
int init)
Add field to class with initial
int value. |
ClassItem |
addField(java.lang.String type,
java.lang.String name,
int access,
java.lang.String init)
Add field to class with initial
String value. |
boolean |
addInterface(java.lang.String intf)
Add interface to class.
|
ClassItem |
addMethod(Method method)
Add method to class.
|
ClassItem |
addPrivateField(java.lang.String type,
java.lang.String name)
Add private field to class without initialization.
|
void |
codeComplete()
Finalize current modified state of class.
|
protected int |
computeHashCode()
Computes a hash code based on characteristics of the class.
|
void |
delete()
Delete class file information.
|
boolean |
deleteField(java.lang.String name)
Delete field from class.
|
boolean |
deleteMethod(java.lang.String name,
java.lang.String sig)
Delete method from class.
|
java.lang.String |
deriveClassName(java.lang.String prefix,
java.lang.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 |
equals(java.lang.Object obj)
Check if objects are equal.
|
protected Field |
getAccessibleField(java.lang.String name)
Get internal information for field.
|
protected Method |
getAccessibleMethod(java.lang.String name,
java.lang.String sig)
Get internal information for method without respect to potential trailing
arguments or return value.
|
private Method |
getBestAccessibleMethod(java.lang.String name,
int access,
Type ret,
Type[] args)
Get information for best matching method.
|
ClassItem |
getBestMethod(java.lang.String name,
java.lang.String ret,
java.lang.String[] args)
Get information for best matching method.
|
ExistingMethod[] |
getBindingMethods(java.lang.String prefix,
java.lang.String[] matches)
Get all binding methods currently defined in class.
|
static ClassFile |
getClassFile(java.lang.String name)
Constructor for preexisting class file from classpath.
|
private ClassGen |
getClassGen()
Get generator for modifying class.
|
static java.lang.ClassLoader |
getClassLoader()
Get the classloader used for classes referenced in the binding.
|
ConstantPoolGen |
getConstPoolGen()
Get constant pool generator for modifying class.
|
protected Field |
getDefinedField(java.lang.String name)
Get internal information for field.
|
ClassItem |
getDirectField(java.lang.String name)
Get information for field.
|
ClassItem |
getField(java.lang.String name)
Get information for field.
|
ClassItem[] |
getFieldItems()
Get array of fields defined by class.
|
java.io.File |
getFile()
Get actual file for class.
|
ClassItem |
getInitializerMethod(java.lang.String sig)
Get information for initializer.
|
java.lang.String[] |
getInstanceSigs()
Get signatures for all types of which instances of this type are
instances.
|
InstructionBuilder |
getInstructionBuilder()
Get instruction builder for modifying class.
|
java.lang.String[] |
getInterfaces()
Get names of all interfaces implemented by class.
|
ClassItem |
getMethod(java.lang.String name,
java.lang.String sig)
Get information for method without respect to potential trailing
arguments or return value.
|
ClassItem |
getMethod(java.lang.String name,
java.lang.String[] sigs)
Get information for method matching one of several possible signatures.
|
ClassItem[] |
getMethodItems()
Get array of methods defined by class.
|
private Method[] |
getMethods()
Get array of methods defined by class or interface.
|
java.lang.String |
getName()
Get fully qualified class name.
|
java.lang.String |
getPackage()
Get package name.
|
JavaClass |
getRawClass()
Get raw current class information.
|
java.io.File |
getRoot()
Get root directory for load path.
|
java.lang.String |
getSignature()
Get signature for class as type.
|
ClassItem |
getStaticMethod(java.lang.String name,
java.lang.String sig)
Get information for static method without respect to return value.
|
ClassFile |
getSuperFile()
Get superclass information.
|
java.lang.String |
getSuperName()
Get superclass name.
|
Type |
getType()
Get class as type.
|
int |
getUseCount()
Get use count for class.
|
int |
hashCode()
Get hash code.
|
int |
incrementUseCount()
Increment use count for class.
|
private void |
init(java.lang.String name,
java.lang.String path,
java.io.InputStream ins)
Internal initialization method.
|
private void |
initInterface()
Retrieve superinterfaces for an interface class.
|
boolean |
isAbstract()
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 |
isComplete()
Check if class is in complete state.
|
boolean |
isExtendable()
Check if binding methods can be added to class.
|
boolean |
isImplements(java.lang.String sig)
Check if class implements an interface.
|
boolean |
isInterface()
Check if class is an interface.
|
boolean |
isModifiable()
Check if class is modifiable.
|
boolean |
isModified()
Check if class has been modified.
|
private static boolean |
isSuffixName(java.lang.String name)
Check if a method name matches the pattern for a generated unique suffix.
|
boolean |
isSuperclass(java.lang.String name)
Check if another class is a superclass of this one.
|
static java.lang.Class |
loadClass(java.lang.String name)
Try loading class from classpath.
|
java.lang.String |
makeUniqueMethodName(java.lang.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 |
setModified()
Set class modified flag.
|
static void |
setPaths(java.lang.String[] paths)
Set class paths to be searched.
|
void |
setSuperFile(ClassFile sclas)
Set superclass information.
|
void |
setUnmodifiable()
Force class to unmodifiable state.
|
ClassItem |
updateField(java.lang.String type,
java.lang.String name,
int access,
java.lang.String init)
Update class field with initial
String value. |
void |
writeFile()
Write out modified class information.
|
void |
writeFile(java.io.OutputStream os)
Write out modified class information.
|
private static final int JAVA7_MAJOR_VERSION
public static final int PRIVATE_ACCESS
public static final int PACKAGE_ACCESS
public static final int PROTECTED_ACCESS
public static final int PUBLIC_ACCESS
public static final int SYNTHETIC_ACCESS_FLAG
protected static final int PRIVATEFIELD_ACCESS
protected static final ExistingMethod[] EMPTY_METHOD_ARRAY
protected static final byte[] EMPTY_BYTES
public static final ClassItem[] EMPTY_CLASS_ITEMS
private static ClassPath s_loader
private static java.net.URLClassLoader s_directLoader
private java.lang.String m_name
private java.lang.String m_signature
private Type m_type
private java.io.File m_root
private java.lang.String m_path
private java.io.File m_file
private boolean m_isSamePackage
private boolean m_isWritable
private boolean m_isExtendable
protected ClassFile m_superClass
protected java.lang.String[] m_interfaceNames
private ClassFile[] m_superInterfaces
private java.lang.String[] m_instanceOfs
private Method[] m_methods
private JavaClass m_curClass
private ClassGen m_genClass
private ConstantPoolGen m_genPool
protected InstructionBuilder m_instBuilder
private java.util.HashMap m_suffixMap
private java.util.HashMap m_itemMap
private boolean m_isModified
private int m_useCount
private boolean m_isHashCurrent
private int m_hashCode
private int m_inheritDepth
private int m_uniqueIndex
private ClassItem m_defaultConstructor
public ClassFile(java.lang.String name, java.io.File root, java.io.File file) throws java.io.IOException, JiBXException
name
- fully qualified class nameroot
- directory root from class loading path listfile
- actual class filejava.io.IOException
- if unable to open fileJiBXException
- if error in reading class filepublic ClassFile(java.lang.String name, java.lang.String sig)
name
- fully qualified class namesig
- corresponding class signaturepublic ClassFile(java.lang.String name, java.io.File root, ClassFile sclas, int access, java.lang.String[] impls) throws JiBXException
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)JiBXException
- on error loading interface informationprivate ClassFile(java.lang.String name, ClassPath.ClassFile cf) throws JiBXException, java.io.IOException
name
- fully qualified class namejava.io.IOException
- if unable to open fileJiBXException
- if error loading superclass or other support filepublic static ClassFile getClassFile(java.lang.String name) throws java.io.IOException, JiBXException
name
- fully qualified class namejava.io.IOException
- if error reading fileJiBXException
- if error loading superclass or other support fileprivate void init(java.lang.String name, java.lang.String path, java.io.InputStream ins) throws JiBXException
name
- fully qualified class namepath
- class file pathins
- input stream for class file dataJiBXException
- if unable to load class fileprivate void initInterface() throws JiBXException
JiBXException
- on error loading interface informationpublic boolean isInterface()
true
if an interface, false
if notpublic boolean isAbstract()
true
if an abstract class, false
if notpublic boolean isArray()
true
if an array class, false
if notpublic boolean isModifiable()
true
if class is modifiable, false
if
notpublic boolean isExtendable()
true
if methods can be added, false
if notpublic java.lang.String getName()
public java.lang.String getSignature()
public Type getType()
public java.lang.String getPackage()
public java.io.File getRoot()
public java.io.File getFile()
public JavaClass getRawClass()
public java.lang.String getSuperName()
public void setSuperFile(ClassFile sclas)
sclas
- superclass informationpublic ClassFile getSuperFile()
null
if no
superclass - java.lang.Object, interface, or primitive)public java.lang.String[] getInterfaces()
null
, empty array if none)public boolean addInterface(java.lang.String intf)
intf
- fully qualified interface nametrue
if added, false
if already presentprotected void accumulateInterfaces(java.lang.String[] intfs, java.util.HashMap map, java.util.ArrayList accs) throws JiBXException
intfs
- names of interfaces implementedmap
- map for interfaces already accumulatedaccs
- accumulated interface namesJiBXException
- if configuration errorpublic java.lang.String[] getInstanceSigs() throws JiBXException
JiBXException
- if configuration errorpublic boolean isImplements(java.lang.String sig) throws JiBXException
sig
- signature of interface to be checkedtrue
if interface is implemented by class,
false
if notJiBXException
- if configuration errorpublic boolean isSuperclass(java.lang.String name)
name
- of superclass to be checkedtrue
if named class is a superclass of this one,
false
if notpublic ClassItem[] getFieldItems()
protected Field getDefinedField(java.lang.String name)
name
- field namenull
if field not foundprotected Field getAccessibleField(java.lang.String name)
name
- field namenull
if field not foundpublic ClassItem getDirectField(java.lang.String name)
name
- field namenull
if field not foundpublic ClassItem getField(java.lang.String name) throws JiBXException
name
- field nameJiBXException
- if field not foundprivate Method[] getMethods()
public ClassItem[] getMethodItems()
protected Method getAccessibleMethod(java.lang.String name, java.lang.String sig)
name
- method namesig
- partial method signature to be matchednull
if method not foundpublic ClassItem getMethod(java.lang.String name, java.lang.String sig)
name
- method namesig
- partial method signature to be matchednull
if method not foundpublic ClassItem getMethod(java.lang.String name, java.lang.String[] sigs)
name
- method namesigs
- possible signatures for method (including return type)null
if method not foundprivate static boolean matchAccess(FieldOrMethod item, int access)
item
- information for field or method to be checkedaccess
- required access level for matchtrue
if access level match, false
if
notprivate static boolean isAssignmentCompatible(Type have, Type need)
have
- type being checkedneed
- type neededtrue
if compatible, false
if notprivate Method getBestAccessibleMethod(java.lang.String name, int access, Type ret, Type[] args)
name
- method nameaccess
- access level required for matching methodsret
- return value type (null
if indeterminant)args
- argument value types (null
if indeterminant)null
if method not foundpublic ClassItem getBestMethod(java.lang.String name, java.lang.String ret, java.lang.String[] args)
name
- method nameret
- return value type (null
if indeterminant)args
- argument value types (null
if indeterminant)null
if method not foundpublic ClassItem getInitializerMethod(java.lang.String sig)
sig
- encoded argument list signaturenull
if method not foundpublic ClassItem getStaticMethod(java.lang.String name, java.lang.String sig)
name
- method namesig
- encoded argument list signaturenull
if method not foundpublic ExistingMethod[] getBindingMethods(java.lang.String prefix, java.lang.String[] matches)
prefix
- identifying prefix for binding methodsmatches
- pairs of method name and signature to be matched as
exceptions to the prefix matchingpublic boolean isAccessible(ClassItem item)
item
- field or method informationtrue
if accessible, false
if notprivate ClassGen getClassGen()
public ConstantPoolGen getConstPoolGen()
public InstructionBuilder getInstructionBuilder()
public ClassItem addMethod(Method method)
method
- method to be addedpublic void removeMethod(Method method)
method
- method to be removedpublic ClassItem addField(java.lang.String type, java.lang.String name, int access, java.lang.String init)
String
value. If a field
with the same name already exists, it is overwritten.type
- fully qualified class name of field typename
- field nameaccess
- access flags for fieldinit
- initial value for fieldpublic ClassItem addField(java.lang.String type, java.lang.String name, int access, int init)
int
value. If a field with
the same name already exists, it is overwritten.type
- fully qualified class name of field typename
- field nameaccess
- access flags for fieldinit
- initial value for fieldpublic ClassItem updateField(java.lang.String type, java.lang.String name, int access, java.lang.String init)
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.type
- fully qualified class name of field typename
- field nameaccess
- access flags for fieldinit
- initial value for fieldpublic ClassItem addField(java.lang.String type, java.lang.String name, int access)
type
- fully qualified class name of field typename
- field nameaccess
- access flags for fieldpublic ClassItem addPrivateField(java.lang.String type, java.lang.String name)
type
- fully qualified class name of field typename
- field namepublic ClassItem addDefaultConstructor()
private static boolean isSuffixName(java.lang.String name)
name
- method name to be checkedtrue
if name matches suffix pattern,
false
if notpublic java.lang.String makeUniqueMethodName(java.lang.String name)
name
- base name before suffix is appendedpublic boolean deleteField(java.lang.String name)
name
- field nametrue
if field was present, false
if notpublic boolean deleteMethod(java.lang.String name, java.lang.String sig)
name
- method namesig
- method signaturetrue
if method was present, false
if notpublic int getUseCount()
public int incrementUseCount()
public void setUnmodifiable()
public boolean isModified()
true
if class is modified, false
if notpublic void setModified()
public boolean isComplete()
true
if class is complete, false
if notprotected int computeHashCode()
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.public void codeComplete()
public int hashCode()
codeComplete()
method is called.hashCode
in class java.lang.Object
public static boolean equalFieldOrMethods(FieldOrMethod a, FieldOrMethod b)
a
- first field or method itemb
- second field or method itemtrue
if the equal, false
if notpublic static boolean equalMethods(Method a, Method b)
a
- first methodb
- second methodtrue
if the equal, false
if notpublic boolean equals(java.lang.Object obj)
codeComplete()
method is called.equals
in class java.lang.Object
obj
- true
if equal objects, false
if notpublic void delete()
public void writeFile(java.io.OutputStream os) throws java.io.IOException
os
- output stream for writing modified classjava.io.IOException
- if error writing to filepublic void writeFile() throws java.io.IOException
java.io.IOException
- if error writing to filepublic java.lang.String deriveClassName(java.lang.String prefix, java.lang.String suffix)
prefix
- generated class name prefixsuffix
- generated class name suffixpublic static void setPaths(java.lang.String[] paths)
paths
- ordered set of paths to be searched for class filespublic static java.lang.Class loadClass(java.lang.String name)
name
- fully qualified name of class to be loadednull
if not foundpublic static java.lang.ClassLoader getClassLoader()