Piotr
Piotr

Reputation: 4963

Pointcut for method that changes a class variable

Is it possible to write a effective pointcut that matches a method that changes a class variable of a specific class type? The point of doing this is that my classes have a lastModificationDate that I want to update to the latest date whenever a class variable is changed.

Example of method:

public void stupidMethod() {

 ...
 for (int i = 0; i < 100; i++) this.var = whatever;
 ...
} <--- I want to match here

Currently I have this, but it is not very optimal:

after(SimpleEntity entity) : set(* *.*) && target(entity) && !within(SimpleEntityAspect)

Upvotes: 2

Views: 1718

Answers (1)

kriegaex
kriegaex

Reputation: 67457

No. getand set pointcuts exist for members only, but not for local variables. Consequently, your example will only match member variable assignments for SimpleEntity objects. If this is what you want to do, please rephrase your question's heading and content so as to make clear what you really want to achieve. Please also provide some more code context, such as e.g. the relevant part of the type declaration for SimpleEntity.

My best guess for now is that you want to match the exit point of methods in which a certain type's members are changed. If you also tell us what exactly you want to do in your advice (e.g. print the method name or the assigned value etc.), we might be able to better help you.


Update: Okay, I have found a solution which does what you want, using pertarget aspect instantiation plus ITD (inter-type declaration). I have not tested performance or memory consumption, I am leaving that up to you.

Sample entity class:

public class SimpleEntity {
    private static int currentId = 1;

    private int id;
    private String name;
    private long lastModification;

    public SimpleEntity(String name) {
        this.id = currentId++;
        this.name = name;
    }

    public void stupidMethod(final int count) {
        for (int i = 0; i < count; i++)
            name = name.replaceFirst("_[0-9]+$", "") + "_" + i;
    }

    public int tripleValue(final int value) {
        return 3 * value;
    }

    @Override
    public String toString() {
        return "SimpleEntity [id=" + id + ", name=" + name + ", lastModification=" + lastModification + "]";
    }
}

Sample application class creating and using entities:

public class Application {
    public static void main(String[] args) {
        SimpleEntity entity1 = new SimpleEntity("Adam");
        entity1.stupidMethod(3);
        entity1.tripleValue(11);
        entity1.stupidMethod(3);
        SimpleEntity entity2 = new SimpleEntity("Eve");
        entity2.stupidMethod(3);
        entity1.tripleValue(22);
        entity2.stupidMethod(3);
    }
}

Aspect doing what was requested by Piotr Blasiak:

public privileged aspect SetterCallingMethodAspect pertarget(entitySetter(SimpleEntity)) {
    private static interface MemberChangeDetector {}
    private boolean MemberChangeDetector.changed;

    declare parents : SimpleEntity implements MemberChangeDetector;

    pointcut entitySetter(SimpleEntity entity) :
        set (* SimpleEntity+.*) && target(entity) && !within(SetterCallingMethodAspect);
    pointcut constructorExecution() :
        execution(*.new(..)) && !cflow(adviceexecution());
    pointcut methodExecution() :
        execution(* *(..)) && !cflow(adviceexecution());

    after(SimpleEntity entity) : entitySetter(entity) {
        entity.changed = true;
        System.out.println(this + ",  " + thisJoinPointStaticPart + "  ->  " + entity);
    }

    after(SimpleEntity entity) : if(entity.changed) && target(entity)
        && (constructorExecution() || methodExecution())
    {
        entity.changed = false;
        entity.lastModification = System.nanoTime();
        System.out.println(
            this + ",  " + thisJoinPointStaticPart +
            "  ->  update lastModification to " + entity.lastModification
        );
    }
}

Sample console output:

SetterCallingMethodAspect@bb6ab6,  set(int SimpleEntity.id)  ->  SimpleEntity [id=1, name=null, lastModification=0]
SetterCallingMethodAspect@bb6ab6,  set(String SimpleEntity.name)  ->  SimpleEntity [id=1, name=Adam, lastModification=0]
SetterCallingMethodAspect@bb6ab6,  execution(SimpleEntity(String))  ->  update lastModification to 1863715110885880
SetterCallingMethodAspect@bb6ab6,  set(String SimpleEntity.name)  ->  SimpleEntity [id=1, name=Adam_0, lastModification=1863715110885880]
SetterCallingMethodAspect@bb6ab6,  set(String SimpleEntity.name)  ->  SimpleEntity [id=1, name=Adam_1, lastModification=1863715110885880]
SetterCallingMethodAspect@bb6ab6,  set(String SimpleEntity.name)  ->  SimpleEntity [id=1, name=Adam_2, lastModification=1863715110885880]
SetterCallingMethodAspect@bb6ab6,  execution(void SimpleEntity.stupidMethod(int))  ->  update lastModification to 1863715112627443
SetterCallingMethodAspect@bb6ab6,  set(String SimpleEntity.name)  ->  SimpleEntity [id=1, name=Adam_0, lastModification=1863715112627443]
SetterCallingMethodAspect@bb6ab6,  set(String SimpleEntity.name)  ->  SimpleEntity [id=1, name=Adam_1, lastModification=1863715112627443]
SetterCallingMethodAspect@bb6ab6,  set(String SimpleEntity.name)  ->  SimpleEntity [id=1, name=Adam_2, lastModification=1863715112627443]
SetterCallingMethodAspect@bb6ab6,  execution(void SimpleEntity.stupidMethod(int))  ->  update lastModification to 1863715114328497
SetterCallingMethodAspect@12d03f9,  set(int SimpleEntity.id)  ->  SimpleEntity [id=2, name=null, lastModification=0]
SetterCallingMethodAspect@12d03f9,  set(String SimpleEntity.name)  ->  SimpleEntity [id=2, name=Eve, lastModification=0]
SetterCallingMethodAspect@12d03f9,  execution(SimpleEntity(String))  ->  update lastModification to 1863715120762834
SetterCallingMethodAspect@12d03f9,  set(String SimpleEntity.name)  ->  SimpleEntity [id=2, name=Eve_0, lastModification=1863715120762834]
SetterCallingMethodAspect@12d03f9,  set(String SimpleEntity.name)  ->  SimpleEntity [id=2, name=Eve_1, lastModification=1863715120762834]
SetterCallingMethodAspect@12d03f9,  set(String SimpleEntity.name)  ->  SimpleEntity [id=2, name=Eve_2, lastModification=1863715120762834]
SetterCallingMethodAspect@12d03f9,  execution(void SimpleEntity.stupidMethod(int))  ->  update lastModification to 1863715121338606
SetterCallingMethodAspect@12d03f9,  set(String SimpleEntity.name)  ->  SimpleEntity [id=2, name=Eve_0, lastModification=1863715121338606]
SetterCallingMethodAspect@12d03f9,  set(String SimpleEntity.name)  ->  SimpleEntity [id=2, name=Eve_1, lastModification=1863715121338606]
SetterCallingMethodAspect@12d03f9,  set(String SimpleEntity.name)  ->  SimpleEntity [id=2, name=Eve_2, lastModification=1863715121338606]
SetterCallingMethodAspect@12d03f9,  execution(void SimpleEntity.stupidMethod(int))  ->  update lastModification to 1863715121829729

Upvotes: 3

Related Questions