Java Reference
In-Depth Information
Life isn't quite as simple as I'm describing it. The string manipulation that processed the
set
method determined a property name. If the property doesn't actually exist (or, rather,
the
get
method doesn't exist), accessing it isn't going to work. Still, asking that each
setter has a corresponding getter doesn't seem to be too much to expect, especially because
Groovy POGOs do that automatically.
To finish this section,
listing 7.19
shows an aspect added to the banking example from the
beginning of this chapter, tracing methods in the
Account
class. Because
Account
is a
POGO, I don't have explicit setter methods. I don't necessarily want to track all the getters,
either, because one of them is
getMetaClass
, and that's not a business method.
One way around that is to use a Java interface implemented by the POGO. Instead, here
I'm going to use explicit pointcuts and put them together.
Here's the complete
AccountAspect
listing with the pointcuts and advice.
Listing 7.19. An aspect tracking methods in the
Account
POGO
import java.util.logging.Logger
import org.aspectj.lang.JoinPoint
import org.aspectj.lang.annotation.Aspect
import org.aspectj.lang.annotation.Before
import org.aspectj.lang.annotation.Pointcut
@Aspect
class AccountAspect {
Logger log = Logger.getLogger(AccountAspect.class.name)
@Pointcut("execution(* mjg..Account.deposit(*))")
void deposits() {}
@Pointcut("execution(* mjg..Account.withdraw(*))")
void withdrawals() {}
@Pointcut("execution(* mjg..Account.getBalance())")
void balances() {}
@Before("balances() || deposits() || withdrawals()")
void audit(JoinPoint jp) {
String method = jp.signature.name
og.info("$method called with ${jp.args} on ${jp.target}")
}
}