[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] Runtime AspectJ

Does anyone know of a simple runtime version of AspectJ (or would like
to create one?)???  Would the AspectJ code base be a good start or are
there other class weaving libraries?

Here is sample code that would create an aspect at runtime and introduce
a before get pointcut on the name field in class Employee:

Aspect aspect = new Aspect();
aspect.addBeforeGet(Employee.class.getDeclaredField("name"));
Employee emp = (Employee)aspect.loadClass("Employee").newInstance();

Here is the Aspect class:

public abstract class Aspect extends ClassLoader {
    // add pointcut.
    public final void addBeforeGet(Field field) {
      // code here to declare pointcut
    }
    // before field get advice goes here.
    protected abstract void beforeGet(Field field, Object obj);
}

To make implementation very simple, here are the initial constraints:
-Only pointcuts are Field, Method and Constructor (AccessibleObject).
-Field pointcuts would fire on all field instances. 


It seems implementation could be pretty simple

public class Employee {
    public String getName() {
        return name;
    }
    private String name;
}

Effect of loading through Aspect().loadClass():

public class Employee {
    private static final Aspect aspect =
(Aspect)Employee.class.getClassLoader();
    private static final Field nameField =
aspect.getField(Employee.class, "name");
    public String getName() {
        aspect.beforeGet(nameField, this);
        return name;
    }
    private String name;
}

Thanks... Sean