There are arguably no interesting lines in this class. All of the work is done via aspects and annotations.
Line
Description
6
This is the first place where we direclty change the address instance. In other examples, changes to Address instance are tracked by the FieldSetAspect. In this case, as we will see, this change is ignored.
We applied the @IgnoreField annotation to the ignoredField.
Line
Description
13
@IgnoreField is an annotation that targets fields. We apply the annotation to the field named ignoredField. This does nothing directly to the field. This is just information associated with the field that will later be used by the SetFieldAspect.
IgnoreField.java
Interesting Lines
We create a new kind of annotation. This annotation by itself only allows adding information to some element type. In this case, it is a field. This does not actively do anything. However, this annotation and a chagne to the SetFieldAspect combine to cause fields with this annotation to be ignored.
Line
Description
12
This annotation’s lifetime is RUNTIME. This means it is available even after the class has been loaded and used in a VM. Contrast that with SOURCE - thrown out after compilation and CLASS - recorded in the class file but not retained by the VM. See RetentionPolicy for details.
13
This annotation targets FIELDs. There are several other options. For details, see ElementType.
FieldSetAspect.java
Interesting Lines
The key change to this class is on line 17 where we ignore fields with the annotation IgnoreField. Otherwise this aspect is mostly unchanged from previous examples. (In fact, this verion of the aspect includes changes for the CFlow exmample on lines 21 - 23.
Line
Description
13 - 15
Define a pointcut that covers the setting of all fields included in the class TrackedObjectMixin. We will use this later with ! to exclude these fields. This is to avoid a recursion problem. The details of why were covered by an exercise. To see that exercise, click here.
17 - 19
Define a pointcut that captures the setting of all fields in the AddressClass except for those that have the annotation IgnoreField. AspectJ uses @Annotation to describe an annotation. In this case, we negate it so we’re saying “does not have the annotation”. This is the only change required to any of the aspects to support skipping individual fields.
This Around advice applies to pointcuts that setters in Address but do not have the annotation IgnoreField AND NOT any pointcuts in the TrackedObjectMixin class AND NOT any pointcuts that happen in in the call of a constructor or anything called by the constructor. It’s a lot to deal with, but by breaking it up into smaller pointcuts, it tends to be easier to understand and more manageable.
Comments