Package javassist.tools.reflect
Class Reflection
- java.lang.Object
-
- javassist.tools.reflect.Reflection
-
- All Implemented Interfaces:
Translator
public class Reflection extends java.lang.Object implements Translator
The class implementing the behavioral reflection mechanism.If a class is reflective, then all the method invocations on every instance of that class are intercepted by the runtime metaobject controlling that instance. The methods inherited from the super classes are also intercepted except final methods. To intercept a final method in a super class, that super class must be also reflective.
To do this, the original class file representing a reflective class:
class Person { public int f(int i) { return i + 1; } public int value; }is modified so that it represents a class:
class Person implements Metalevel { public int _original_f(int i) { return i + 1; } public int f(int i) { delegate to the metaobject } public int value; public int _r_value() { read "value" } public void _w_value(int v) { write "value" } public ClassMetaobject _getClass() { return a class metaobject } public Metaobject _getMetaobject() { return a metaobject } public void _setMetaobject(Metaobject m) { change a metaobject } }- See Also:
ClassMetaobject,Metaobject,Loader,Compiler
-
-
Field Summary
Fields Modifier and Type Field Description (package private) static java.lang.StringclassMetaobjectClassName(package private) static java.lang.StringclassobjectAccessor(package private) static java.lang.StringclassobjectFieldprotected ClassPoolclassPoolprotected CodeConverterconverter(package private) static java.lang.StringmetaobjectClassName(package private) static java.lang.StringmetaobjectField(package private) static java.lang.StringmetaobjectGetter(package private) static java.lang.StringmetaobjectSetterprotected CtClass[]readParam(package private) static java.lang.StringreadPrefixprotected CtMethodtrapMethodprotected CtMethodtrapReadprotected CtMethodtrapStaticMethodprotected CtMethodtrapWrite(package private) static java.lang.StringwritePrefix
-
Constructor Summary
Constructors Constructor Description Reflection()Constructs a newReflectionobject.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private CtMethodfindOriginal(CtMethod m, boolean dontSearch)private booleanisExcluded(java.lang.String name)booleanmakeReflective(java.lang.Class clazz, java.lang.Class metaobject, java.lang.Class metaclass)Produces a reflective class.booleanmakeReflective(java.lang.String classname, java.lang.String metaobject, java.lang.String metaclass)Produces a reflective class.booleanmakeReflective(CtClass clazz, CtClass metaobject, CtClass metaclass)Produces a reflective class.private booleanmodifyClassfile(CtClass clazz, CtClass metaobject, CtClass metaclass)voidonLoad(ClassPool pool, java.lang.String classname)Inserts hooks for intercepting accesses to the fields declared in reflective classes.private voidprocessFields(CtClass clazz)private voidprocessMethods(CtClass clazz, boolean dontSearch)private voidprocessMethods0(int mod, CtClass clazz, CtMethod m, int identifier, boolean dontSearch)voidrebuildClassFile(ClassFile cf)private voidregisterReflectiveClass(CtClass clazz)Registers a reflective class.voidstart(ClassPool pool)Initializes the object.
-
-
-
Field Detail
-
classobjectField
static final java.lang.String classobjectField
- See Also:
- Constant Field Values
-
classobjectAccessor
static final java.lang.String classobjectAccessor
- See Also:
- Constant Field Values
-
metaobjectField
static final java.lang.String metaobjectField
- See Also:
- Constant Field Values
-
metaobjectGetter
static final java.lang.String metaobjectGetter
- See Also:
- Constant Field Values
-
metaobjectSetter
static final java.lang.String metaobjectSetter
- See Also:
- Constant Field Values
-
readPrefix
static final java.lang.String readPrefix
- See Also:
- Constant Field Values
-
writePrefix
static final java.lang.String writePrefix
- See Also:
- Constant Field Values
-
metaobjectClassName
static final java.lang.String metaobjectClassName
- See Also:
- Constant Field Values
-
classMetaobjectClassName
static final java.lang.String classMetaobjectClassName
- See Also:
- Constant Field Values
-
trapMethod
protected CtMethod trapMethod
-
trapStaticMethod
protected CtMethod trapStaticMethod
-
trapRead
protected CtMethod trapRead
-
trapWrite
protected CtMethod trapWrite
-
readParam
protected CtClass[] readParam
-
classPool
protected ClassPool classPool
-
converter
protected CodeConverter converter
-
-
Method Detail
-
isExcluded
private boolean isExcluded(java.lang.String name)
-
start
public void start(ClassPool pool) throws NotFoundException
Initializes the object.- Specified by:
startin interfaceTranslator- Parameters:
pool- theClassPoolthat this translator should use.- Throws:
NotFoundException- if aCtClasscannot be found.- See Also:
Loader
-
onLoad
public void onLoad(ClassPool pool, java.lang.String classname) throws CannotCompileException, NotFoundException
Inserts hooks for intercepting accesses to the fields declared in reflective classes.- Specified by:
onLoadin interfaceTranslator- Parameters:
pool- theClassPoolthat this translator should use.classname- the name of the class being loaded.- Throws:
CannotCompileException- if the code transformation by this method fails.NotFoundException- if aCtClasscannot be found.- See Also:
Loader
-
makeReflective
public boolean makeReflective(java.lang.String classname, java.lang.String metaobject, java.lang.String metaclass) throws CannotCompileException, NotFoundExceptionProduces a reflective class. If the super class is also made reflective, it must be done before the sub class.- Parameters:
classname- the name of the reflective classmetaobject- the class name of metaobjects.metaclass- the class name of the class metaobject.- Returns:
falseif the class is already reflective.- Throws:
CannotCompileExceptionNotFoundException- See Also:
Metaobject,ClassMetaobject
-
makeReflective
public boolean makeReflective(java.lang.Class clazz, java.lang.Class metaobject, java.lang.Class metaclass) throws CannotCompileException, NotFoundExceptionProduces a reflective class. If the super class is also made reflective, it must be done before the sub class.- Parameters:
clazz- the reflective class.metaobject- the class of metaobjects. It must be a subclass ofMetaobject.metaclass- the class of the class metaobject. It must be a subclass ofClassMetaobject.- Returns:
falseif the class is already reflective.- Throws:
CannotCompileExceptionNotFoundException- See Also:
Metaobject,ClassMetaobject
-
makeReflective
public boolean makeReflective(CtClass clazz, CtClass metaobject, CtClass metaclass) throws CannotCompileException, CannotReflectException, NotFoundException
Produces a reflective class. It modifies the givenCtClassobject and makes it reflective. If the super class is also made reflective, it must be done before the sub class.- Parameters:
clazz- the reflective class.metaobject- the class of metaobjects. It must be a subclass ofMetaobject.metaclass- the class of the class metaobject. It must be a subclass ofClassMetaobject.- Returns:
falseif the class is already reflective.- Throws:
CannotCompileExceptionCannotReflectExceptionNotFoundException- See Also:
Metaobject,ClassMetaobject
-
registerReflectiveClass
private void registerReflectiveClass(CtClass clazz)
Registers a reflective class. The field accesses to the instances of this class are instrumented.
-
modifyClassfile
private boolean modifyClassfile(CtClass clazz, CtClass metaobject, CtClass metaclass) throws CannotCompileException, NotFoundException
-
processMethods
private void processMethods(CtClass clazz, boolean dontSearch) throws CannotCompileException, NotFoundException
-
processMethods0
private void processMethods0(int mod, CtClass clazz, CtMethod m, int identifier, boolean dontSearch) throws CannotCompileException, NotFoundException
-
findOriginal
private CtMethod findOriginal(CtMethod m, boolean dontSearch) throws NotFoundException
- Throws:
NotFoundException
-
processFields
private void processFields(CtClass clazz) throws CannotCompileException, NotFoundException
-
rebuildClassFile
public void rebuildClassFile(ClassFile cf) throws BadBytecode
- Throws:
BadBytecode
-
-