Below are instructions on using the examples in Eclipse and AspectJ. (Please note that the examples are specific to AspectJ but neither AspectJ nor the examples are specific to Eclipse.)
- Download, install and run Eclipse 3.2.1
- Download and install AJDT (which includes AspectJ) through the download manager (Help -> Software Updates -> Find and Install -> Search for new updates to install. Add a “New Remote Site” with the URL
http://download.eclipse.org/tools/ajdt/32/dev/update). Please note that the examples require the lastest development build due to a bug in AJDT.
- Download and unzip the examples into a convenient location (you can use the workspace directory for example).
- Within Eclipse, import (right click on the Package Explorer and select Import -> General -> Existing Projects into Workspace) the three example projects (“Example”, “fault.injection” and “javassist”). (If you select the directory where you unzipped the files then the three projects should appear in the dialog.) (It may take a moment or two to set up and build everything. There should be no errors (though there will be warnings).)
- You can run the example with out any faults (right click on “Main.java” in the “Example” project in the Project Explorer and select “Run as” -> “Java Application”).
- To run the examples with different faults requires a bit of setup:
- Select “Run…” from the toolbar (it’s a drop down from the green play-like button).
- In the dialog click on “AspectJ / Java Application” in the left navigation and right click “New”.
- Under “Project” set it to
fault.injectionand under “Main class” set it to
- Switch to the “Arguments” tab (still in the Run dialog) and add to the “VM arguments”: “
-javaagent:lib/aspectjweaver.jar” (without the quotes). (This is the JVMTI agent that will load-time weave the aspects.)
- Click “Run”. At this point the console should show the same information that you saw when you ran “Main.java” directly. This is because the
META-INF/aop.xmldoesn’t have any aspects configured.
- Open the
META-INF/aop.xmlwhich is located in the “fault.injection” project under “classes”. (Because Eclipse tries to mask some complexity from you it’s difficult to get at the “classes” directory in the IDE unless you set up some filters correctly. For simplicity, just open the file in some text editor via the filesystem.)
- Uncomment the example fault that you are interested in (i.e. remove one set of “!–” and “–” from the aspects section of the file) and save the file. When you re-run the application in Eclipse the load-time weaver will weave in the aspect that you uncommented. (If something doesn’t work as expected then check the top of the console output. The weaver may have some information for you.) Please note that in one case there are two aspects (“Read2” and “Read2Helper”) as part of one example.
As I mentioned in the presentation, to apply techniques similar to this in your own environment, I would recommend using Ant to generate an appropriate
META-INF/aop.xml and then run JUnit in a loop to run through different faults.
I tried to structure the projects to decouple them as much as possible. For example, the “fault.injection” project doesn’t directly depend on the “Example” project. (It only depends on the output classes.) I could have gone a step further and generated a JAR out of the “Example” project and used that in the “fault.injection” one. The goal to all of this was to ensure / prove that load-time weaving was being done.
Please don’t hesitate to contact me if you have questions or problems.