Aspects can be used to weave into the original code and manipulate it according to our requirement.
Let’s see how we can use Aspects in Equinox environment.
Let us understand when you can need to use Aspect when you are doing some kind of eclipse/Equinox/osgi development.
I will take a very simple example. Suppose Your application shows a view which is provided by some 3rd party and this view shows some Treeviewer. This TreeViewer has its own content provider and label provider. Now you have a requirement in which you have change the image show for every tree node at runtime depending on some business requirement.
To work on above scenario you would need to have control on label provide or you would like to override it and then do some kind of tweak to provide images at runtime and show it.
What I will do is write an aspect where I will use the pointcut , getImage() method of that original label provider and provide my own image to it. And I am done.
Following Setup is required for this:
- Install the latest version of AJDT from the AJDT update site.
- latest build of Equinox Aspects, which contains plugins regarding equinox weaving with aspectJ
Steps to understand this with an example:
- Create a sample plugin project, having a view and a treeviewer with label provider.
- Export the above sample project as deployable plugin, so that is can be used as 3rd party jar/contribution of view.
- Now put the above sample deployable jar as target in your eclipse.
- Create a new plugin project, were we will use the 3rd party sample plugin to show its view and it tree.
- Make some changes in Menifest.mf to add some dependencies and attributes:
- ‘Eclipse-SupplimentBundle’ attribute conatins bundle which are to be weaved.
- Create the ViewAspect.aj in package we have mentioned in Manifest.mf.
Here you can see, I have created a pointcut which is concerned about getImage() method of ViewLabelProvider. I am returning my image, instead of original image for Tree node.
- Now its Time to run our application.
Configure your runtime
- Tell Equinox to use org.eclipse.equinox.weaving.hook as a framework extension (in the "VM arguments" of your launch configuration, add -Dosgi.framework.extensions=org.eclipse.equinox.weaving.hook).
- We are done now. We can run this application, We will see that Sample view Treeviewer will take my custom image now.
You can Access the code related to above mentioned scenario at GitHub:
Feel Free to comment and provide your responses to understand the topic more.
CHEERS!!!