Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
RE: [aspectj-users] AspectJ Runtime Hooks for Object Persistence

AspectJ worked great.  Below is what I came up with.  The only issue is how to assign these two aspects at runtime to whatever class I want (as apposed to declaring it statically).  Anyone know were I can look?
 
Thanks... Sean
 
aspect InterceptPointcuts {
 
    private Interceptor Interceptable.interceptor = Passthrough.DEFAULT;
 
    public void Interceptable.setInterceptor(Interceptor i) {
        interceptor = i == null ? Passthrough.DEFAULT : i;
    }
    public Interceptor Interceptable.getInterceptor() {
        return interceptor;
    }
 
    declare parents : persistence.objects.* implements Interceptable;
}
 
aspect Intercept {
 
    pointcut getter(Interceptable i):
        get(!transient * (persistence.objects.*).*) && target(i);
 
    pointcut setter(Interceptable i, Object value):
        set(!transient * (persistence.objects.*).*) && target(i) && args(value);
 
    before(Interceptable i): getter(i) {
        i.getInterceptor().getField(i, thisJoinPoint.getSignature().getName());
    }
 
    before(Interceptable i, Object value): setter(i, value) {
        i.getInterceptor().setField(i, thisJoinPoint.getSignature().getName(), value);
    }
 
}
 
-----Original Message-----
From: aspectj-users-admin@xxxxxxxxxxx [mailto:aspectj-users-admin@xxxxxxxxxxx]On Behalf Of Sean R. Drucker
Sent: Saturday, March 01, 2003 9:48 AM
To: aspectj-users@xxxxxxxxxxx
Subject: [aspectj-users] AspectJ Runtime Hooks for Object Persistence

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...

Back to the top