Package javassist.tools.reflect
Class Reflection
java.lang.Object
javassist.tools.reflect.Reflection
- All Implemented Interfaces:
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:
-
Field Summary
FieldsModifier and TypeFieldDescription(package private) static final String
(package private) static final String
(package private) static final String
protected ClassPool
protected CodeConverter
(package private) static final String
(package private) static final String
(package private) static final String
(package private) static final String
protected CtClass[]
(package private) static final String
protected CtMethod
protected CtMethod
protected CtMethod
protected CtMethod
(package private) static final String
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate CtMethod
findOriginal
(CtMethod m, boolean dontSearch) private boolean
isExcluded
(String name) boolean
makeReflective
(Class<?> clazz, Class<?> metaobject, Class<?> metaclass) Produces a reflective class.boolean
makeReflective
(String classname, String metaobject, String metaclass) Produces a reflective class.boolean
makeReflective
(CtClass clazz, CtClass metaobject, CtClass metaclass) Produces a reflective class.private boolean
modifyClassfile
(CtClass clazz, CtClass metaobject, CtClass metaclass) void
Inserts hooks for intercepting accesses to the fields declared in reflective classes.private void
processFields
(CtClass clazz) private void
processMethods
(CtClass clazz, boolean dontSearch) private void
processMethods0
(int mod, CtClass clazz, CtMethod m, int identifier, boolean dontSearch) void
private void
registerReflectiveClass
(CtClass clazz) Registers a reflective class.void
Initializes the object.
-
Field Details
-
classobjectField
- See Also:
-
classobjectAccessor
- See Also:
-
metaobjectField
- See Also:
-
metaobjectGetter
- See Also:
-
metaobjectSetter
- See Also:
-
readPrefix
- See Also:
-
writePrefix
- See Also:
-
metaobjectClassName
- See Also:
-
classMetaobjectClassName
- See Also:
-
trapMethod
-
trapStaticMethod
-
trapRead
-
trapWrite
-
readParam
-
classPool
-
converter
-
-
Constructor Details
-
Reflection
public Reflection()Constructs a newReflection
object.
-
-
Method Details
-
isExcluded
-
start
Initializes the object.- Specified by:
start
in interfaceTranslator
- Parameters:
pool
- theClassPool
that this translator should use.- Throws:
NotFoundException
- if aCtClass
cannot be found.- See Also:
-
onLoad
public void onLoad(ClassPool pool, String classname) throws CannotCompileException, NotFoundException Inserts hooks for intercepting accesses to the fields declared in reflective classes.- Specified by:
onLoad
in interfaceTranslator
- Parameters:
pool
- theClassPool
that this translator should use.classname
- the name of the class being loaded.- Throws:
CannotCompileException
- if the code transformation by this method fails.NotFoundException
- if aCtClass
cannot be found.- See Also:
-
makeReflective
public boolean makeReflective(String classname, String metaobject, String metaclass) throws CannotCompileException, NotFoundException Produces 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:
false
if the class is already reflective.- Throws:
CannotCompileException
NotFoundException
- See Also:
-
makeReflective
public boolean makeReflective(Class<?> clazz, Class<?> metaobject, Class<?> metaclass) throws CannotCompileException, NotFoundException Produces 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:
false
if the class is already reflective.- Throws:
CannotCompileException
NotFoundException
- See Also:
-
makeReflective
public boolean makeReflective(CtClass clazz, CtClass metaobject, CtClass metaclass) throws CannotCompileException, CannotReflectException, NotFoundException Produces a reflective class. It modifies the givenCtClass
object 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:
false
if the class is already reflective.- Throws:
CannotCompileException
CannotReflectException
NotFoundException
- See Also:
-
registerReflectiveClass
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
- Throws:
NotFoundException
-
processFields
-
rebuildClassFile
- Throws:
BadBytecode
-