Field annotation
mksong Feb 23, 2010 4:50 PMHello,
I don't know why #2 and #3 do not work in the following example.
Please, help me out.
- (1) works well
<bind pointcut="field(* bank.BankAccount->*)">
- (2) does not work.
<bind pointcut="field(@bank.SecurityFieldAnnotation bank.BankAccount->*)">
- (3) does not work.
<bind pointcut="field(* bank.BankAccount->@bank.SecurityFieldAnnotation)">
----------------------------------------------------------------------------
package bank;
import bank.SecurityFieldAnnotation;
public class BankAccount
{
@SecurityFieldAnnotation
private int counter;
public int getCounter() { return counter; }
public void setCounter(int counter) { this.counter = counter;}
}
----------------------------------------------------------------------------
package bank;
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
@Target({ElementType.FIELD})
public @interface SecurityFieldAnnotation {
}
----------------------------------------------------------------------------
package bank;
import org.jboss.aop.joinpoint.*;
public class SecurityFieldAspect
{
public Object access(FieldReadInvocation invocation)
throws Throwable {
System.out.println("[DBG] access read");
return invocation.invokeNext();
}
public Object access(FieldWriteInvocation invocation)
throws Throwable {
System.out.println("[DBG] access write");
return invocation.invokeNext();
}
}
----------------------------------------------------------------------------
package bank;
public class Bank {
public static void main(String[] args) {
BankAccount bankAccount = new BankAccount();
bankAccount.setCounter(1);
int counter = bankAccount.getCounter();
System.out.println("[DBG] counter: " + counter);
}
}
----------------------------------------------------------------------------
<aop>
<aspect class="bank.SecurityFieldAspect"
scope="PER_JOINPOINT"/>
<bind pointcut="field(* bank.BankAccount->@bank.SecurityFieldAnnotation)">
<advice name="access"
aspect="bank.SecurityFieldAspect"/>
</bind>
</aop>