Can anyone point me in the right direction for
doing the following (can
AspectJ do this?):
-At runtime, using weaving, load a class
and declare all field get and set pointcuts for all
non-transient fields in the class. Obviously, field point-cuts from
outside the class can not be created
as they require access to the calling code. This is not an
issue.
-At runtime, be able to hook/unhook the object with
a single object to intercept the field pointcuts. Only one hook object can be attached to the weaved object at a
time.
Thanks... Sean
Reasons for doing this (if you care):
-Developing a large object persistence project
(>100 classes).
-Depending on the usage of the persistence object,
the fields will be loaded using different fetching schemes. Therefore, the
fields that need to be intercepted (to do the background fetching) can vary at
runtime.
Sample:
public class Employee {
private String name; private transient int
setCount;
public String getName()
{ return
name; } public void setName(String
name) {
setCount++; this.name =
name;
// edge
case assert ((this.name =
this.name) == this.name); }
}
Could be weaved as follows:
public class Employee implements Interceptable {
private String name; private transient int
setCount;
public String getName()
{ return
(String)interceptor.getField(this, "name", name);
} public void setName(String name)
{
setCount++; this.name =
(String)interceptor.setField(this, "name", this.name, name);
// edge
case assert ((this.name =
(String)interceptor.setField(this, "name",
this.name,
(String)interceptor.getField(this, "name", this.name)))
==
(String)interceptor.getField(this, "name", this.name));
}
// implement
hook public void setInterceptor(Interceptor interceptor)
{ this.interceptor = interceptor
== null ? Passthrough.DEFAULT : interceptor;
} public Interceptor getInterceptor()
{ return
interceptor; }
private Interceptor interceptor
= Passthrough.DEFAULT;
}
Passthrough/Interceptor:
public class Passthrough implements Interceptor
{
public static final Interceptor
DEFAULT = new Passthrough();
public Object getField(Object
obj, String field, Object currentValue)
{ return
currentValue; }
public Object setField(Object
obj, String field, Object currentValue, Object newValue)
{ return
newValue; } }
Thanks...
|