From 6a1dcf2e9e9f077b6adb733654c4d3b727a1d1de Mon Sep 17 00:00:00 2001
From: ehilsdal In this tutorial you will solve some canonical programming
+tasks using AspectJ. The tasks progress from writing
+non-functional, development-only aspects to writing aspects that
+augment a deployed program with crosscutting features. This
+follows the same progression most users see in their own adoption
+of AspectJ. This tutorial consists of the attendees solving AspectJ
-programming tasks (with unit tests) in pairs or triples and discussing
-the answers with the group and with the tutorial presenters. The
-exercises work with a figure editor together with JUnit test cases.
-They progress, as most users do in their adoption of AspectJ, from
-non-functional, development-only aspects to aspects which augment a
-deployed program with crosscutting features. Since this is a hands-on tutorial, you will be working with a
+live AspectJ distribution. The example code we will be working
+with is a simple figure editor, along with JUnit tests for each
+exercise. We will break up into groups of two to three people
+per computer to foster discussion within the group as well as
+with the presenters. If you have a laptop running a recent version of Windows,
+MacOS or Linux, feel free to bring it along. We will provide CDs
+and other installation media for a standalone AspectJ system,
+including the figure editor code these exercises are based on and
+unit tests for the exercises. If you don't have a laptop with
+you, don't worry about it. These notes consist of four sections of exercises, a quick
reference to AspectJ syntax, and a UML diagram of a figure editor
-program. While you should feel free to have a quick look through
-these notes before the tutorial, please do not try to seriously read
-or do the exercises; you'll learn a lot more by working through it in
-groups. At the beginning of the tutorial we will make available a binary
-package that includes the tests, the base code, JUnit, and a
-distribution of AspectJ. All it needs is information about where Java
-lives (so set your JAVA_HOME environment variable). It assumes that
-you unzip it in c:\ (on Windows) or in your home directory (on Linux):
-If you put it somewhere else, edit setpaths or setpaths.bat, as
-appropriate. Once all this is done, run All the files in the program are listed in base.lst, including
-test cases and an empty answer aspect,
-Hands-on Aspect-Oriented Programming with AspectJ
+
+
+Hands-on Programming with AspectJ
+
+Overview
-Organization
+Command-line usage
-
-setpaths.bat
or
-source setpaths
to export some other needed environment
-variables. answers/Answer.java
. Therefore, if you write your
-answers there, all you need to do is compile base.lst, either in an
-IDE or with
+program.
While the AspectJ system is well integrated with a number of
+IDEs, it can also be used as a command-line compiler. The
+standalone package we provide (containing the tests, the base
+code, JUnit, and a distribution of AspectJ) needs information
+about where Java lives (so set your JAVA_HOME environment
+variable). It assumes that you unzip it in c:\ (on Windows) or
+in your home directory (on Linux): If you put it somewhere else,
+edit setpaths
or setpaths.bat
, as
+appropriate.
+
Each time you open a new shell window run
+setpaths.bat
or source setpaths
to
+export some other needed environment variables.
In general, all the files in the program are listed in
+base.lst
, including test cases and an empty answer
+aspect, answers/Answer.java
. Therefore, if you
+write your answers there, all you need to do is compile
+base.lst
, either in an IDE or with
$ ajc -argfile base.lst -+
Before you move onto another exercise, though, make sure to copy your answer into a different file so we can discuss the answers together:
-++> copy answers/Answer.java answers/2a.java (Windows) $ cp answers/Answer.java answers/2a.java (Unix) -
After building the system, you should invoke Java on the compiled test class. On the command-line, this this would be
-- -+$ java tests.Test2a -
(For these exercises, when we give examples of execution we will -show the command-line use, but of course if you are using JBuilder, -Forte/NetBeans, Emacs, or Eclipse, use the appropriate compile and -execute tools.)
+ The default test, tests.Test
, performs some
rudimentary tests on figure elements, and so is a useful test to run
-periodically. Looking at the JUnit tests for each exercise may also
-be helpful.
Again, we will be looking at some solutions and having discussion, -which is much more difficult without incremental solutions. So when -you go from one exercise to the next, make sure to save your work in a -file and go on to work in a different file, even if you plan to -duplicate some code.
- -You may use whatever editor or environment you choose to work -through these exercises. We provide a simple code-browser that can -work well as an editor for these short exercises, in addition to -providing better visualization of how aspects affect the system:
- -- --$ ajbrowser base.lst -
With the browser you can edit code (including the
-answers/Answer.java
file), and after saving hit the build
-button to start an ajc compile. We recommend you start up another
-shell, though, to run the JUnit tests (and don't forget to run the
-setpaths
script when you open the new shell): You could
-set up the run button to run a test through the Options menu, but
-we've found this is fairly cumbersome.
Again, we will be looking at solutions and having discussion, +which is much more difficult without incremental solutions. So +when you go from one exercise to the next, save your +work in a file before going on to the next exercise +even if you plan to duplicate some code.
+ +The easiest way to get started with AspectJ is to use it to enforce static invariants.
-Sample Exercise: The main point of this exercise -is to make sure your configuration works. Type in the answer below -into your answer file, make sure you get the desired compile-time -error, remove the offending line, and make sure you pass the JUnit -test.
+ Task: Signal an error for calls to
System.out.println
.
@@ -121,70 +179,69 @@ test.
The way that we are all taught to print "hello world" from Java is
to use System.out.println()
, so that is what we typically
use for one-off debugging traces. It's a common mistake to leave
-these in your system longer than is necessary. Type in the aspect
-below to force an error at compile time if this mistake is made.
+these in your system far longer than is necessary. Type in the aspect
+below to signal an error at compile time if this mistake is made.
Answer: +
+ ++package answers; + +import figures.*; + +aspect Answer1a { + declare error + : get(java.io.PrintStream System.out) && within(figures..*) + : "illegal access to System.out"; +} ++
When you use this on the given system, you'll find one incorrect
trace in SlothfulPoint
.
+ -+$ ajc -argfile base.lst ./figures/SlothfulPoint.java:38 illegal access to System.out 1 error -
Remove the illegal tracing call. -
+ Note that this answer does not say that the call to the
+println()
method is incorrect, rather, that the field get
+of the out
field is illegal. This will also catch those
+users who bind System.out to a static field to save typing.
After you have successfully used this aspect, edit your +program to remove the illegal tracing call.
Make sure your program still passes the JUnit test
tests.Test
(which it should also pass at the beginning of
all exercises) before continuing.
- -+$ java tests.Test .... Time: 0.03 OK (4 tests) -
Answer: -
- -- --package answers; - -import figures.*; - -aspect Answer1a { - declare error - : get(java.io.PrintStream System.out) && within(figures..*) - : "illegal access to System.out"; -} -
Note that this answer does not say that the call to the
-println()
method is incorrect, rather, that the field get
-of the out
field is illegal. This will also catch those
-users who bind System.out to a static field to save typing.
Task: Signal a warning for assignments outside of setter methods.
Tools: set
, withincode
,
-signature void set*(..)
+the void set*(..)
pattern
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.
+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.
This is going to look like
@@ -200,19 +257,19 @@ outside of setter methods. "Outside", here, means that the code for the assignment is outside the text of the setter. Make sure your program still passes the JUnit test
-tests.Test
before continuing. Make sure you get 11
+tests.Test
before continuing. Make sure you get eleven
warnings from this. Wait to fix them until the next exercise.
Task: Allow assignmnents inside of constructors.
- Tools: signature new(..)
Tools: the new(..)
pattern
Look at some of the warnings from the previous exercise. 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 +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.
@@ -225,42 +282,43 @@ pointcut to deal with the constructors. the convention is violated twice in the figures package. You should see the following two errors: -+.\figures\Point.java:37 bad field set .\figures\Point.java:38 bad field set 2 errors -+
Rewrite these two occurrences so as not to violate
the convention. Make sure your program still passes the JUnit test
tests.Test
before continuing.
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.
+The next step in AspectJ adoption is often to augment a test suite by including additional dynamic tests.
-Tutorial attendees typically progress at different speeds through -these questions. Throughout this tutorial, if you finish early, see -what the people around you are doing and if they need help. Feel free -to help them out of naked self-interest; we promise you'll learn a lot -about AspectJ by explaining it.
+Sample Exercise: We've provided the answer to -this exercise below to get you started.
+ Task: Pass tests.Test2a
.
++$ java tests.Test2a .F..F.... Time: 0.04 @@ -287,12 +345,12 @@ There were 2 failures: FAILURES!!! Tests run: 7, Failures: 2, Errors: 0 -
But after compiling in the aspect...
-++$ ajc -argfile base.lst answers/Answer.java $ java tests.Test2a @@ -300,12 +358,12 @@ $ java tests.Test2a Time: 0.04 OK (7 tests) -
Answer:
-+ -+package answers; import figures.*; @@ -317,9 +375,9 @@ aspect Answer2a { } } } -
Task: Pass tests.Test2b
.
Look at tests/Test2b.java
to see exactly what we're
testing for.
Task: Pass tests.Test2c
.
Group.add()
on a
call.
- Task: Pass tests.Test2d
.
Task: Pass tests.Test2e
Task: Pass tests.Test2f
IllegalStateException
if it is violated.
-Tracing is one of the classic AspectJ applications, and is often the first where AspectJ is used on deployed code.
- Task: Pass tests.Test3a
.
Log
(with an import from support.Log
)
and call Log.log(String)
- Task: Pass tests.Test3b
.
+ -+thisJoinPointInfo at targetObject -
Task: Pass tests.Test3c
.
args
pointcut allows you to select join points
based on the type of a parameter to a method call.
-Look at the test case for details about the tested log message. +
Look at the test case for details about the required log message.
- Task: Pass tests.Test3d
.
++boolean Point.hasBeenAdded = false; -
Check and set this flag with the same kind of advice from your
answer to problem (c). Throw an IllegalStateException
if
the point has already been added.
Task: Pass tests.Test3e
.
Tools: -
- Extend your solution to problem (d) by using the string
representation of the Point's containing group as the msg
part of the IllegalStateException
.
Computation of the bounding box of Group
objects
needs to deal with all aggregate parts of the group, and this
computation can be expensive. In this section, we will explore
various ways of reducing this expense.
Optional: In all of these exercises, you should -only deal with points that are added directly to Groups, rather than -those that are added "indirectly" through Lines and Boxes. You should -handle those points contained in Lines and Boxes only if time permits. -
+ Task: Pass tests.Test4a
.
FigureElement.MAX_BOUNDS
.
around advice intercepting the method.
- Task: Pass tests.Test4b
.
Group
object.
- Task: Pass tests.Test4c
.
move()
method of Group
is called.
- Task: Pass tests.Test4d
.
Task: Pass tests.Test4e
.
Congratulations! Not only have you learned about how to +program in AspectJ, you have worked through exercises paralleling +a common AspectJ adoption strategy. You should be able to +pick up AspectJ and use it to improve your own software's +crosscutting modularity.
+ +You can find the current binaries, source, documentation and +an active user community for AspectJ at
+ ++ http://www.eclipse.org/aspectj ++ +