Chapter 8. Capturing Join Points on Attributes
Introduction
One slightly controversial feature of the AspectJ
developer’s toolkit is the ability to monitor any
accesses or modifications that may occur on a
class’s attribute. AspectJ provides this capability
in the form of the
get(Signature)
and
set(Signature)
pointcuts that are the focus of
this chapter.
Anyone who has practiced traditional object-oriented techniques will
be a little worried when they hear this as it effectively breaks
encapsulation, especially if the attributes being monitored are
declared protected
or private
.
It is good advice to think carefully before you decide your aspect
needs to have direct access to a class’s internals
to avoid unnecessary tight coupling between your aspects and your
classes.
However, sometimes a cross-cutting concern requires this level of
intimacy with a class’s to apply the aspect
effectively. Used judiciously the get(Signature)
and set(Signature)
pointcuts showcased in this
chapter can provide a powerful means of advising your classes, but
they must be used carefully to ensure that you are not making your
classes and your aspects needlessly brittle.
8.1. Capturing When an Object’s Attribute Is Accessed
Problem
You want to capture when an object’s attribute is accessed.
Solution
Use the get(Signature)
pointcut. The syntax of the
get(Signature)
pointcut is:
pointcut <pointcut name>(<any values to be picked up>
) : get(<optional modifier> <type> <class>
.<field>
);
Discussion
The get(Signature) ...
Get AspectJ Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.