From: ehilsdal
Problem: Add warnings for assignments outside of setter methods. +
+ + Tools: set
, withincode
,
+signature void set*(..)
+
One common coding convention is that no private field should be set outside of setter methods. Write an aspect to warn at compile time when such an illegal assignment expression exists.
@@ -157,53 +164,21 @@ pointcuts. ) Make sure your program still passes the JUnit test
tests.Test
before continuing, and that you see all of the
-following warning messages. You'll notice a LOT of warnings here.
-Wait to fix them until the next exercise...
+following warning messages. Make sure you get 11 warnings from this.
+Wait to fix them until the next exercise.
-.\figures\Box.java:17:9: bad field set (warning) - _p0 = new Point(x0, y0); - ^ -.\figures\Box.java:18:9: bad field set (warning) - _p1 = new Point(x0+width, y0); - ^ -.\figures\Box.java:19:9: bad field set (warning) - _p2 = new Point(x0+width, y0+height); - ^ -.\figures\Box.java:20:9: bad field set (warning) - _p3 = new Point(x0, y0+height); - ^ -.\figures\Group.java:16:23: bad field set (warning) - this._members = new ArrayList(); - ^ -.\figures\Line.java:15:9: bad field set (warning) - _p1 = p1; - ^ -.\figures\Line.java:16:9: bad field set (warning) - _p2 = p2; - ^ -.\figures\Point.java:15:9: bad field set (warning) - _x = x; - ^ -.\figures\Point.java:16:9: bad field set (warning) - _y = y; - ^ -.\figures\Point.java:28:9: bad field set (warning) - _x += dx; - ^ -.\figures\Point.java:29:9: bad field set (warning) - _y += dy; - ^ -+
Problem: Allow assignmnents inside of constructors. +
- Tools: signature new(..)
Look at some of the warnings. Notice that a lot of them are from
within constructors. Actually, the common coding convention is that
-no private field should be set outside of setter methods or
-constructors. Modify your answer (in a new file) to signal an
+no private field should be set outside of setter methods
(If you see more, go back to 1b; you may be capturing sets to -too many fields.) -
-Rewrite these two occurrences so as not to violate
the convention. Make sure your program still passes the JUnit test
tests.Test
before continuing.
In part (c), you rewrote the code to fit the convention enforced -by the aspect. It may be that this code doesn't violate the convention -of your mythical organization. Try to instead fix the pointcut so it -doesn't signal an error for these two assignments, and then change -your code back to making the assignments.
- - Make sure your program still passes the JUnit test
-tests.Test
before continuing.
At this point, check the people to your left and right. If -they're stuck somewhere, see if you can help them.
+You've taken your first steps. At this point, check the people to +your left and right. If they're stuck somewhere, see if you can help +them.
@@ -259,11 +219,18 @@ they're stuck somewhere, see if you can help them. Problem: Pass tests.Test2a
.
+
Tools: args
, before
+
THERE IS AN ANSWER BELOW. LOOK AT IT. TYPE IT IN. +
+ Write an aspect to throw an IllegalArgumentException
whenever an attempt is made to set one of Point
's
-int
fields to a value that is either too large (greater
-than FigureElement.MAX_VALUE
) or too small (less than
-FigureElement.MIN_VALUE
).
int
fields to a value that is less than zero.
This should make the test case of tests.Test2a
pass,
which wouldn't without your aspect. So before compiling in the