From f1a7345a62bd1f227dd64fcd7c2d519c8911ae5c Mon Sep 17 00:00:00 2001
From: jhugunin
The crosscutting feature you will be adding in part (4) will be
support for caching the bound objects of Group
figure
elements, which may be costly to compute. On the way to that, though,
it's useful to explore the system with some tracing aspects.
Write an aspect to trace whenever a Point
is moved.
-To do this, use the utility class Log
(with an import
-from support.Log
) and call
Problem: Pass tests.Test3a
.
- --Log.log("moving") -
This will write the string "moving", followed by a semicolon -terminator, to the Log. For example, with your aspect enabled, -
- -- --Point p1 = new Point(10, 100); -p1.move(37, 8); -System.out.println(Log.getString()); -
should print out "moving;". +
Tools: Log.log(String)
,
+ thisJoinPoint.toString()
, execution
,
+ within
Test this with the JUnit test case tests.Test3a
.
-Without adding any aspects, this test should fail:
+-$ ajc -Xlint -argfile base.lst -$ java tests.Test3a -..F....... -Time: 0.07 -There was 1 failure: -1) testMovePointLog(tests.Test3a)junit.framework.AssertionFailedError: expected:<set;> but was:<> - at tests.Test3a.testMovePointLog(Test1a.java:30) - at tests.Test3a.main(Test1a.java:16) +Write an aspect to log the execution of all public methods +in the figures package. To do this, use the utility class +
-FAILURES!!! -Tests run: 9, Failures: 1, Errors: 0 -Log
(with an import fromsupport.Log
) +and callLog.log(String)
But with the proper aspect added to the compilation, (in this
-case, answers/Answer3a.java
, but you should feel free to
-use more evocative names), the test should pass
+-$ ajc -Xlint -argfile base.lst answers/Answer3a.java -$ java tests.Test3a -......... -Time: 0.089 - -OK (9 tests) -
Problem: Pass tests.Test3b
.
Answer: +
Tools: target
+-package answers; -import support.Log; -import figures.*; +c. More specialized logging
-aspect Answer3a { - before(): execution(void Point.move(int, int)) { - Log.log("moving"); - } -} -
Problem: Pass tests.Test3c
.
Tools: +
- Write an aspect to trace whenever a Point
is added to
+
Write an aspect to log whenever a Point
is added to
a group (including initially). To do this, use the utility class
Log
(with an import from support.Log
) and
call
Hint: The args
pointcut allows you to select join points
based on the type of a parameter to a method call.
Test this with the JUnit test case tests.Test3b
.
+
Problem: Pass tests.Test3d
.
Tools: inter-type field declaration
+
Make sure that a Point is never added to more than one Group.
+ + + Problem: Pass tests.Test3e
.
Tools: +
+Make sure that a Point is never added to more than one Group. +Include the Group that the Point was previously a part of in the +thrown exception.