public class Reflection extends java.lang.Object implements Translator
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 }
}
ClassMetaobject,
Metaobject,
Loader,
Compiler| Modifier and Type | Field and Description |
|---|---|
(package private) static java.lang.String |
classMetaobjectClassName |
(package private) static java.lang.String |
classobjectAccessor |
(package private) static java.lang.String |
classobjectField |
protected ClassPool |
classPool |
protected CodeConverter |
converter |
(package private) static java.lang.String |
metaobjectClassName |
(package private) static java.lang.String |
metaobjectField |
(package private) static java.lang.String |
metaobjectGetter |
(package private) static java.lang.String |
metaobjectSetter |
protected CtClass[] |
readParam |
(package private) static java.lang.String |
readPrefix |
protected CtMethod |
trapMethod |
protected CtMethod |
trapRead |
protected CtMethod |
trapStaticMethod |
protected CtMethod |
trapWrite |
(package private) static java.lang.String |
writePrefix |
| Constructor and Description |
|---|
Reflection()
Constructs a new
Reflection object. |
| Modifier and Type | Method and Description |
|---|---|
private CtMethod |
findOriginal(CtMethod m,
boolean dontSearch) |
private boolean |
isExcluded(java.lang.String name) |
boolean |
makeReflective(java.lang.Class clazz,
java.lang.Class metaobject,
java.lang.Class metaclass)
Produces a reflective class.
|
boolean |
makeReflective(CtClass clazz,
CtClass metaobject,
CtClass metaclass)
Produces a reflective class.
|
boolean |
makeReflective(java.lang.String classname,
java.lang.String metaobject,
java.lang.String metaclass)
Produces a reflective class.
|
private boolean |
modifyClassfile(CtClass clazz,
CtClass metaobject,
CtClass metaclass) |
void |
onLoad(ClassPool pool,
java.lang.String classname)
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 |
rebuildClassFile(ClassFile cf) |
private void |
registerReflectiveClass(CtClass clazz)
Registers a reflective class.
|
void |
start(ClassPool pool)
Initializes the object.
|
static final java.lang.String classobjectField
static final java.lang.String classobjectAccessor
static final java.lang.String metaobjectField
static final java.lang.String metaobjectGetter
static final java.lang.String metaobjectSetter
static final java.lang.String readPrefix
static final java.lang.String writePrefix
static final java.lang.String metaobjectClassName
static final java.lang.String classMetaobjectClassName
protected CtMethod trapMethod
protected CtMethod trapStaticMethod
protected CtMethod trapRead
protected CtMethod trapWrite
protected CtClass[] readParam
protected ClassPool classPool
protected CodeConverter converter
private boolean isExcluded(java.lang.String name)
public void start(ClassPool pool) throws NotFoundException
start in interface Translatorpool - the ClassPool that this translator
should use.NotFoundException - if a CtClass cannot be found.Loaderpublic void onLoad(ClassPool pool, java.lang.String classname) throws CannotCompileException, NotFoundException
onLoad in interface Translatorpool - the ClassPool that this translator
should use.classname - the name of the class being loaded.CannotCompileException - if the code transformation
by this method fails.NotFoundException - if a CtClass cannot be found.Loaderpublic boolean makeReflective(java.lang.String classname,
java.lang.String metaobject,
java.lang.String metaclass)
throws CannotCompileException,
NotFoundException
classname - the name of the reflective classmetaobject - the class name of metaobjects.metaclass - the class name of the class metaobject.false if the class is already reflective.CannotCompileExceptionNotFoundExceptionMetaobject,
ClassMetaobjectpublic boolean makeReflective(java.lang.Class clazz,
java.lang.Class metaobject,
java.lang.Class metaclass)
throws CannotCompileException,
NotFoundException
clazz - the reflective class.metaobject - the class of metaobjects.
It must be a subclass of
Metaobject.metaclass - the class of the class metaobject.
It must be a subclass of
ClassMetaobject.false if the class is already reflective.CannotCompileExceptionNotFoundExceptionMetaobject,
ClassMetaobjectpublic boolean makeReflective(CtClass clazz, CtClass metaobject, CtClass metaclass) throws CannotCompileException, CannotReflectException, NotFoundException
CtClass object and makes it reflective.
If the super class is also made reflective, it must be done
before the sub class.clazz - the reflective class.metaobject - the class of metaobjects.
It must be a subclass of
Metaobject.metaclass - the class of the class metaobject.
It must be a subclass of
ClassMetaobject.false if the class is already reflective.CannotCompileExceptionCannotReflectExceptionNotFoundExceptionMetaobject,
ClassMetaobjectprivate void registerReflectiveClass(CtClass clazz)
private boolean modifyClassfile(CtClass clazz, CtClass metaobject, CtClass metaclass) throws CannotCompileException, NotFoundException
private void processMethods(CtClass clazz, boolean dontSearch) throws CannotCompileException, NotFoundException
private void processMethods0(int mod,
CtClass clazz,
CtMethod m,
int identifier,
boolean dontSearch)
throws CannotCompileException,
NotFoundException
private CtMethod findOriginal(CtMethod m, boolean dontSearch) throws NotFoundException
NotFoundExceptionprivate void processFields(CtClass clazz) throws CannotCompileException, NotFoundException
public void rebuildClassFile(ClassFile cf) throws BadBytecode
BadBytecode