Recipe2.8.Selecting the Aspects That Are Woven in a Build Within Eclipse


Recipe 2.8. Selecting the Aspects That Are Woven in a Build Within Eclipse

Problem

You want to vary the aspects that are woven into your application when developing in Eclipse.

Solution

Create a separate AspectJ build configuration for each different selection of aspects that you want to weave into your application. Eclipse then allows you to select the current build configuration it will use to build your AspectJ project so you can easily select the set of aspects you want to apply for a particular build.

Discussion

Recipe 2.3 showed that the AspectJ compiler can use a build confiuguration .lst file to select the classes and aspects it will include in its aspect weaving. By default, an AspectJ project in Eclipse has a single .lst file, naturally called default.lst, that lists all the of the files in your project. Using this default, the AspectJ compile will apply all the aspects to all of classes where indicated by the pointcut logic in those aspects.

For many applications, this default behavior is fine but there are times when you may want to vary the aspects that are applied to a particular application depending on such things as deployment target or feature selection. AspectJ provides a neat way of varying the selection of aspects to be applied using a custom build configuration .lst file that excludes or includes the aspects you wish to apply.

Eclipse provides the means by which a custom AspectJ build configuration can be created and used for a particular project.

When the project created in Recipe 2.7 is run, the output produced on the Console in Eclipse is:

MyAspect before( ) advice In the advice attached to the call point cut Inside foo (int, String)

The following steps create a new build configuration that will exclude the single MyAspect aspect in the project from the build and therefore change the behavior of the application as shown by the amended output on the Console.

As of Eclipse 3.0, with the AJDT Version 1.1.11 or later installed, the following steps have changed when creating a new AspectJ build configuration file. There is also now a new format for build configuration files used by the Eclipse AJDT plug-in, the .ajproperties file.

You can create a new build configuration in Eclipse 3.0 by clicking on Project Active Build Configuration Save as... , entering a new name for your build configuration, and then by following step 8 onwards. This will create a new .ajproperties file as opposed to the more traditional .lst file.

You can also convert an existing .lst file to the newer .ajproperties format by right-clicking on the .lst file and selecting Save as .ajproperties file. If you need to go back to the .lst format, you can right-click on a .ajproperties file and select Save as .lst file.

  1. Ensure that your AspectJ project is selected in the Package Explorer view.

  2. Click on File New Other....


  3. The New AspectJ Build Configuration File dialog will appear, as shown in Figure 2-9.

    Figure 2-9. Entering the details for the new AspectJ build configuration file


  4. Check that the correct project is selected and enter a name for the new build configuration file, for this example use the name excludeAspects.lst.

  5. Deselect the Include all source files from project checkbox and click on Finish.

  6. The new excludeAspects.lst file will be added to your project and opened in the file editor.

  7. Select the files you want to include in this build configuration, as shown in Figure 2-10. In this example, you want to build only the classes and exclude all of the aspects so only the MyClass.java file is selected.

    Figure 2-10. Editing the AspectJ build configuration file in Eclipse


  8. Save your changes to the build configuration file.

  9. You need to tell the AspectJ compiler to switch to your configuration for this project. Find the Build AspectJ Project/Select Configuration button on the Eclipse toolbar, as shown in Figure 2-11.

    Figure 2-11. The Build AspectJ Project/Select Configuration button in Eclipse


  10. Click on the small down triangle to the right of the button to pop up a list of the available build configurations for this project, as shown in Figure 2-12.

    Figure 2-12. Selecting the new excludeAspects.lst AspectJ build configuration


  11. Select the new build configuration and the AspectJ compiler will automatically rebuild your project according to the new settings.

    In Eclipse 3.0 with AJDT 1.1.11 or later, you can quickly exclude or include a single source file from the current active configuration; then, you can right-click on the source file and click on Exclude from "%CURRENT_CONFIGURATION%" or Include from "%CURRENT_CONFIGURATION%" if the file is already excluded.


After building under the new excludeAspects.lst configuration, the Console output will show the following when the application is run again:

Inside foo (int, String)

The aspects have been removed from your application when building with this configuration. To switch back to the full default build configuration, click on the Selecting down triangle and select the <all project files> option.

See Also

Eclipse and Eclipse Cookbook by Steve Holzner (O'Reilly); Recipe 2.9 shows how to use the Ant AspectJ tasks to vary the aspects applied to a specific build of your application.



AspectJ Cookbook
Aspectj Cookbook
ISBN: 0596006543
EAN: 2147483647
Year: 2006
Pages: 203
Authors: Russ Miles

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net