Aspect Oriented Programming For Eclipse Plug-ins
Join the DZone community and get the full member experience.Join For Free
It seems to me that Aspect Oriented Programming never really took off when it was introduced. However, it's a useful way to intercept, or analyse, methods as they happen, in an independent way. Eclipse has a useful suite of AspectJ tools that you can download for your Eclipse installlation. Paired with the benefits of Eclipse's plug-in system, aspects are a nice way of intercepting your RCP application.
The following instructions show how to get up and running with aspects in the Plug-in Development Environment really quickly. Once you have downloaded the Eclipse AspectJ tools, you will also want to include the Equinox Aspect jars in your plug-ins directory. The plug-ins you will need are org.eclipse.equinox.weaving.aspectj and org.eclipse.equinox.weaving.hook
- Create a new OSGi plug-in:
- Right click on the project and choose AspectJ Tools/Convert to AspectJ
- Create a new package within the plugin eg. com.dzone.aspects.aspectTest
- Make a new aspectj Aspect within the package e.g. MyAspect
- In your manifest.mf export the package created in the previous step
- A you write your AspectJ code, you will be advising another plug-in (for example org.eclipse.jdt.junit) You'll need to do some extra setup in order to advise other plug-ins, by adding the
following to your Aspect plug-in manifest.mf.
Note you can only supplement one bundle in an aspect. Therefore, if you want to crosscut another bundle, you’ll need to create a new AspectJ plug-in.
- It also helps to add the plugin that you are advising (org.eclipse.jdt.junit) to your aspect plugin's dependencies. If you don't do it you will get lint warnings from the AspectJ compiler
- In your plugins META-INF directory make a file called aop.xml, consisting of content similar to the following
<?xml version="1.0" encoding="UTF-8"?>
- When executing use the following VM arguments in your Run Configuration
It's as simple as that. Have you any instructions to add to this?
Opinions expressed by DZone contributors are their own.