I've been a zone leader with DZone since 2008, and I'm crazy about community. Every day I get to work with the best that JavaScript, HTML5, Android and iOS has to offer, creating apps that truly make at difference, as principal front-end architect at Avego. James is a DZone Zone Leader and has posted 639 posts at DZone. You can read more from them at their website. View Full User Profile

Aspect Oriented Programming For Eclipse Plug-ins

03.23.2010
| 6592 views |
  • submit to reddit

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

  1. Create a new OSGi plug-in:

  2. Right click on the project and choose AspectJ Tools/Convert to AspectJ Project

  3. Create a new package within the plugin eg. com.dzone.aspects.aspectTest
  4. Make a new aspectj Aspect within the package e.g. MyAspect
  5. In your manifest.mf export the package created in the previous step

    Export-Package: com.dzone.aspects
  6. 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.

    Eclipse-SupplementBundle: org.eclipse.jdt.junit

    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.
  7. 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
  8. 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"?>
    <aspectj>
    <aspects>
    <aspect
    name="com.dzone.aspects.aspectTest.MyAspect" />
    </aspects>
    <weaver options="-verbose"/>
    </aspectj>
  9. When executing use the following VM arguments in your Run Configuration

    -Dosgi.framework.extensions=org.eclipse.equinox.weaving.hook
    -Dorg.aspectj.osgi.verbose=true

It's as simple as that. Have you any instructions to add to this?

Tags:

Comments

Thomas Eichberger replied on Wed, 2010/03/24 - 2:05am

Interesting and helpful!

Mounir Beggas replied on Sun, 2010/09/19 - 4:13pm

Thinks, it's very interesting and helpful I think about, step 6, that you can supplement many bundles by only one aspect (I tried that)

Jun Yu replied on Wed, 2013/08/28 - 10:27pm in response to: Mounir Beggas


hi

Step 6:after 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.
 Eclipse-SupplementBundle: org.eclipse.jdt.junitafter i add the line to manifest.mf the advise is still there.  what's the problem?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.