|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--org.bsf.smartValueObject.tools.JavaAssistInstrumentor
Javassist specific implementation.
This class makes heavy use of advanced javassist features like runtime compilation.
Instrumentor
,
Javassist homepageNested Class Summary | |
private static class |
JavaAssistInstrumentor.InstClassLoader
Custom ClassLoader using CtClass. |
Nested classes inherited from class org.bsf.smartValueObject.tools.Instrumentor |
Instrumentor.SmartReplacements |
Field Summary | |
private static javassist.CodeConverter |
converter
The codeconverter to be used to change field access. |
private javassist.CtClass |
ctclass
A The modified class in javassist's representation. |
private static JavaAssistInstrumentor.InstClassLoader |
instCL
Custom classloader to define classes at runtime. |
private static org.apache.commons.logging.Log |
log
|
private static javassist.ClassPool |
pool
Default pool to obtain CtClasses from. |
Fields inherited from interface org.bsf.smartValueObject.tools.Instrumentor |
CLEANMETHOD, CREATEDMETHOD, CREATEMETHOD, DELETEDMETHOD, DELETEMETHOD, DIRTYMETHOD, SMARTCOLLECTION, SMARTCONTAINERS, SMARTLIST, SMARTMAP, SMARTSET, VERSIONCLASS, VERSIONFIELD, VERSIONHELPER, VERSIONINTERFACE, VERSIONMETHOD |
Constructor Summary | |
JavaAssistInstrumentor()
Creates new instance. |
|
JavaAssistInstrumentor(java.lang.Class clazz)
Added for convenience. |
|
JavaAssistInstrumentor(java.lang.String name)
The instrumentor is initialized by this constructor. |
Method Summary | |
private static void |
addDelegations(javassist.CtClass iface,
javassist.CtField field,
javassist.CtClass declaring)
Generic method to implement an interface by delegation. |
private static void |
addFieldInterceptor(javassist.CtField field,
java.util.Properties ifaces)
Adds an interceptor to a field (for write access). |
private static void |
addFieldInterceptors(javassist.CtClass cc)
Adds interceptors to all fields declared in cc. |
private static javassist.CtField |
addVersionField(javassist.CtClass cc)
Adds a version field to the class. |
private boolean |
alreadyModified(javassist.CtClass ctclass)
Prevents class from being instrumented twice. |
private static javassist.CtMethod |
createTrapWrite(javassist.CtClass cc)
Creates a 'trap' for interception. |
private static javassist.CtMethod |
createTrapWriteGeneric(javassist.CtClass cc,
java.lang.String dumb,
java.lang.String smart)
To replace assignment to specific interfaces by a wrapped version. |
private static javassist.CtField |
createVersionField(javassist.CtClass declaring)
Creates the version field. |
java.lang.Class |
defineClass()
Use internal classloader to build class object. |
private static java.lang.String |
fieldWrite(java.lang.String name)
Convention to name methods. |
byte[] |
getBytecode()
Get modified class as byte array. |
static java.lang.ClassLoader |
getClassLoader()
For testing. |
static java.lang.Class |
loadAndDefine(java.lang.String name)
For testing. |
private static void |
makeFieldsPublic(javassist.CtClass cc)
Makes fields public. |
private static void |
makeVersionable(javassist.CtClass cc)
Makes class versionable. |
private static javassist.CtClass |
modifyClass(javassist.CtClass cc)
Applies all necessary modifications to make class versionable. |
void |
modifyClass(java.lang.String name)
Modifies this class. |
void |
modifyClass(java.lang.String basedir,
java.lang.String file)
|
static java.lang.Class |
smartify(java.lang.Class clazz)
Static convenience method. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
private static final org.apache.commons.logging.Log log
private static final javassist.ClassPool pool
private static final javassist.CodeConverter converter
private static JavaAssistInstrumentor.InstClassLoader instCL
private javassist.CtClass ctclass
Constructor Detail |
public JavaAssistInstrumentor()
public JavaAssistInstrumentor(java.lang.String name) throws InstrumentorException
name
- class to be modified, either as path or in package notation.
InstrumentorException
- when encountering problems while loading/
modifying the class.public JavaAssistInstrumentor(java.lang.Class clazz) throws InstrumentorException
clazz
- to be modified
InstrumentorException
Method Detail |
public void modifyClass(java.lang.String name) throws InstrumentorException
Instrumentor
modifyClass
in interface Instrumentor
name
- class to modify, package notation or filename.
InstrumentorException
- in case of errorspublic void modifyClass(java.lang.String basedir, java.lang.String file) throws InstrumentorException
modifyClass
in interface Instrumentor
InstrumentorException
public byte[] getBytecode() throws InstrumentorException
Instrumentor
getBytecode
in interface Instrumentor
InstrumentorException
public java.lang.Class defineClass()
Instrumentor
Exists rather for testing purposes, as classes won't be compatible !
defineClass
in interface Instrumentor
public static java.lang.Class smartify(java.lang.Class clazz) throws InstrumentorException
clazz
- to be modified
InstrumentorException
public static java.lang.Class loadAndDefine(java.lang.String name) throws java.lang.ClassNotFoundException
java.lang.ClassNotFoundException
public static java.lang.ClassLoader getClassLoader()
private static javassist.CtClass modifyClass(javassist.CtClass cc) throws InstrumentorException
cc
- class to be modified.
InstrumentorException
private static void makeFieldsPublic(javassist.CtClass cc)
cc
- private static void makeVersionable(javassist.CtClass cc) throws javassist.CannotCompileException, javassist.NotFoundException, InstrumentorException
javassist.CannotCompileException
javassist.NotFoundException
InstrumentorException
Instrumentor.VERSIONINTERFACE
,
Instrumentor.VERSIONFIELD
private static void addDelegations(javassist.CtClass iface, javassist.CtField field, javassist.CtClass declaring) throws InstrumentorException, javassist.NotFoundException, javassist.CannotCompileException
E.g. declaring.isDirty()
==>
declaring.field.isDirty()
.
iface
- interface to implement.field
- field to delegate to.declaring
- class to add interface to.
InstrumentorException
javassist.NotFoundException
javassist.CannotCompileException
private static void addFieldInterceptors(javassist.CtClass cc) throws javassist.NotFoundException, javassist.CannotCompileException
javassist.NotFoundException
javassist.CannotCompileException
private static void addFieldInterceptor(javassist.CtField field, java.util.Properties ifaces) throws javassist.NotFoundException, javassist.CannotCompileException
write access to field foo of type Bar will result in:
static write_foo(Object o, Bar bar) { trap_method };
field
- field to be intercepted.ifaces
- interfaces with their 'smart' replacements.
javassist.NotFoundException
javassist.CannotCompileException
private static java.lang.String fieldWrite(java.lang.String name)
private static javassist.CtField addVersionField(javassist.CtClass cc) throws javassist.CannotCompileException, javassist.NotFoundException
cc
- target class.
javassist.CannotCompileException
javassist.NotFoundException
Instrumentor.VERSIONFIELD
,
createVersionField(javassist.CtClass declaring)
private static javassist.CtField createVersionField(javassist.CtClass declaring) throws javassist.CannotCompileException, javassist.NotFoundException
javassist.CannotCompileException
javassist.NotFoundException
addVersionField(javassist.CtClass cc)
private static javassist.CtMethod createTrapWrite(javassist.CtClass cc) throws javassist.CannotCompileException
cc
- target class.
javassist.CannotCompileException
addFieldInterceptor(javassist.CtField, java.util.Properties)
private static javassist.CtMethod createTrapWriteGeneric(javassist.CtClass cc, java.lang.String dumb, java.lang.String smart) throws javassist.CannotCompileException
new SmartXXX(dumb,
versionable)
.
By using 'versionable' as the second parameter, the newly created object gets a reference to the version state of its parent.
cc
- target class.dumb
- package name of class to replace.smart
- package name of replacing class.
javassist.CannotCompileException
private boolean alreadyModified(javassist.CtClass ctclass) throws javassist.NotFoundException
javassist.NotFoundException
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |