Port of schuchert.wikispaces.com


AspectJ_Annotation_Experience

AspectJ_Annotation_Experience

AspectJ Annotation: Experience

Here is the full source for this example: AspectJAnnotationsSrc.zip. If you’d like to know how to set up a project using it, click here.

This example is written in a way that it is meant to follow: AspectJ_Example_3 and AspectJ_Example_4. I specifically leave out details unchanged from those examples.

Now we’ll introduce selecting pointcuts augmented by annotations. Begin by reviewing the following Java files and the expected output. Based on all of that information, your assignment is to describe what is happening and try to guess how it is happening. Note that the code provided here is somewhat stripped down to reduce what you have to look at. Later, we provide the full source in the Explained section.

Main.java

public class Main {
    public static void main(String args[]) {
        Address a = new Address();
        a.setIgnoredField("Ignored");
        Dao.save(a);
        a.setZip("75001");
        Dao.save(a);
    }
}

Dao.java

public class Dao {
    public static void save(Object o) {
        if (o != null) {
            System.out.printf("Saving: %s\n", o.getClass());
        }
    }
}

The Output

Not saving: class annotation.Address, it is unchanged
Saving: class annotation.Address

IgnoreField.java - the annotation

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface IgnoreField {
    String value() default "";
}

FieldSetAspect.java

@Aspect
public class FieldSetAspect {
    @Pointcut("set(* annotation.TrackedObjectMixin.*)")
    public void trackedObject() {
    }

    @Pointcut("args(rhs) && set(!@annotation.IgnoreField * annotation.Address.*)")
    public void allFields(Object rhs) {
    }

    @Pointcut("cflow(execution(annotation.ITrackedObject+.new (..)))")
    public void constructors() {
    }

    @Around("allFields(rhs) && !trackedObject() && !constructors()")
    public Object trackFieldAssignment(ProceedingJoinPoint thisJoinPoint, Object rhs) throws Throwable {
       // ...
    }

public class Address implements Serializable {
    private String addressLine1;
    private String addressLine2;
    private String city;
    private String state;
    private String zip;

    @IgnoreField("Example of a field we ignore")
    private String ignoredField;

    public String getIgnoredField() {
        return ignoredField;
    }


Comments

" Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.