User Tools

Site Tools


invariant:ex2

Intro Ex1 Ex2 Ex3 Ex4 Ex5

Invariants: Mandate setters

Task: Signal a warning for assignments to private fields outside of setter methods.

One common coding convention is that no private field should be assigned to outside of setter methods. Write an aspect to signal a warning at compile time for these illegal assignment expressions.

Follow the template given below, where the pointcut picks out join points of private field sets outside of setter methods. “Outside”, here, means that the code for the assignment is outside the text of the setter.

aspect Answer2 {
  declare warning: <pointcut>: "Bad field set";
}
Tools: The set and withincode primitive pointcuts and the void set*(..) signature pattern.

You should get eleven warnings from this aspect. Notice that a lot of them are from within constructors. Actually, the common coding convention is that no private field should be assigned to outside of setter methods or constructors. Modify your answer to signal an actual error at compile time (rather than just a warning) when such an illegal assignment expression exists.

Tools: Another withincode primitive pointcut and the new(..) constructor signature pattern.

After you specify your pointcut correctly, you'll still find that the convention is violated twice in the figures package. You should see two errors in the move method of the Point class.


Don't correct these set statements. Instead, when done, disable your aspect: right-click on it and select Build PathExclude. Continue with Exercise 3.

invariant/ex2.txt · Last modified: 2021/02/05 13:53 (external edit)