[abc-users] Tracematch vs aspect variable binding

From: Alan Teoh <alan.teoh07_at_imperial.ac.uk>
Date: Thu, 14 Feb 2008 13:00:31 +0000

Hi again,

I am investigating on tracematches and wrote an example along with an
aspect to see how they behave.

class Account {
    private int type; // Integer to determine account type
   
    public Account(int t) {
        this.type = t;
    }
   
    public void withdraw() {
        System.out.println("Withdraw");
    }
   
    public void deposit() {
        System.out.println("Deposit");
    }
    public static void main(String[] args) {
        Account a1 = new Account(1);
        Account a2 = new Account(1);
       
        a1.withdraw();
        a1.deposit();
        a2.deposit(); // Match occurs here with aspect
        a2.deposit();
        a1.deposit(); // Match occurs here with tracematches
    }
}

Tracematch for Account.java:
public aspect AccountTM {
    tracematch (Account a) {
        sym wd after:
            call (* Account.withdraw(..)) && target(a);
        sym dp after:
            call (* Account.deposit(..)) && target(a);
       
        wd dp[2]
        {
            System.out.println("1 withdraw and 2 deposit method calls
made by " + a);
        }
    }
}

Aspect:
public aspect TraceMatchAccount {
    private int countwd = 0; // Integer to keep track the number of
withdraw calls
    private int countdp = 0; // Integer to keep track the number of
deposit calls

    after(Account a): call(* Account.withdraw(..)) && target(a) {
        if(countwd < 1) {
            countwd++; // Increment the withdrawal count
        }

        // Reset counter if deposit was made prior to the withdraw call
        if(countdp > 0) {
            countwd = 0;
            countdp = 0;
        }
    }
    
    after(Account a): call(* Account.deposit(..)) && target(a) {
        countdp++;
        if(countwd > 0 && countdp == 2) {
            System.out.println("1 withdraw and 2 deposit method calls
made by " + a);
            countwd = 0;
            countdp = 0;
        }
    }
}

Both were compiled separately as different cases (via ajc and abc for
aspect and tracematch respectively). Basically I want the the behaviour
of 1 withdraw() and 2 deposit calls by an Account object to be traced.
The problem in Account.java is that while the tracematch exhibits
expected behaviour, the aspect does not. My understanding is that the
'target' pointcut picks out each join point where the target object is
of the type specified (in this case, Account). The aspect would only
check the type of the receiver for this case, and therefore a2 is
matched in the process as well. On the other hand, while tracematches
check the target type(Account), it also checks variable bindings to the
target object, and therefore it will never match a2.

I would have thought that the aspect would bind the variable accordingly
like how the tracematch does.

What changes do I need to make to the aspect so that it is equivalent to
the tracematch? I was thinking of writing code to compare the object
that is currently being evaluated on the aspect (i.e. compare
a2.equals(a1) ), but that looks a bit naive to me. I am fairly new to
aspect oriented programming, so I might have made a elementary mistake
somewhere in my aspect, and I would appreciate some help on this matter.

Thanks.
Received on Thu Feb 14 2008 - 13:00:38 GMT

This archive was generated by hypermail 2.2.0 : Thu Feb 14 2008 - 13:50:11 GMT