ADempiere and AspectJ

From ADempiere
Jump to: navigation, search
This Wiki is read-only for reference purposes to avoid broken links.

Initial contributor: Bepi Esposito Vingiano (bepivin)
With help from: Heng Sin Low (hengsin) and Colin Rooney (croo)

The problem

My problem was to make some customization on M_ classes in ADempiere. The advised method in ADempiere community was to extend ADempiere by callouts and model validator. The Model Validator approach is to extend M_ classes in save and delete methods, but I was searching a way to insert code lines also in other points of the code flow.

Then AspectJ approach was advised by Heng Sin and I have investigated the use of AspectJ into ADempiere.

The solution (one solution)

In this article we will try to inject a simple messagebox into the MInvoice class and in particular into the method CompleteIt. CompleteIt run when we push the button Complete into the window Invoice (Customer) found in the menu Quote-to-Invoice > Sales Invoices.

OK we start with the first (simple) method to use aspects: convert ADempiere project into an AspectJ project and creating aspects into the project itself.

NOTE: converting a project into an AspectJ project is reversible. The conversion simply says to Eclipse to use the AspectJ builder instead of the Java builder. The AspectJ compiler is an extension of the Java compiler. Sources are not modified during this conversion.

First step is to install AspectJ Development Tools

Installing AspectJ Development Tools in Eclipse

NOTE: in my investigation I have found that it's needed a PC with 2GB memory (with 1 GB I had often memory problems building the project).

I use Eclipse Ganymede (Version 3.4.1).

-Go to Help > Software Updates...

-Click on Add Site... button

-Insert http://download.eclipse.org/tools/ajdt/34/update in the Location textbox and click OK

-Click on the checkbox in the row added to the list and click the button Install...

First way: Compile-Time Weaving with Aspects in Adempiere project

-tested and working on ADempiere

-to document

Second way: Compile-Time Weaving with Aspects in own Eclipse project

-tested and working on ADempiere

-to document

Third way: Post-Compile-Time Weaving (or Binary Weaving)

-not tested

Fourth way: Load Time Weaving (cool!)

1)Create an AspectJ project from New > Other


Bepivin aspectj 01.jpg


2)Insert the project name and click on Finish button


Bepivin aspectj 02.jpg

Now you have a project similar to a standard Java project with a ittle AJ on the icon

3)Create a package (same way as a Java project). I've called it 'aspectPackage'

4)Create an aspect. Right click on aspectPackage > New > Other > Aspect

Insert the name (i've used prova - it's Italian for trial) and click the Finish button

Bepivin aspectj 03.jpg

Now you have this skeleton:

package aspectPackage;
public aspect prova {
}

5)Learn AspectJ programming!

6)Copy and paste this code:

package aspectPackage;
 
import org.compiere.model.MInvoice;
import javax.swing.JOptionPane;
 
public aspect prova {
 
   pointcut myPointcut() : execution(* MInvoice.completeIt(..)); 
 
   before() : myPointcut() { 
       JOptionPane.showMessageDialog(null, "before");
   }
 
   after() returning() : myPointcut() { 
       JOptionPane.showMessageDialog(null, "after");
   }	
 
}

Little explanation:

  • It was created a pointcut when MInvoice.completeIt is called. It's called a named pointcut because we have called it myPointcut.
  • The following block is a before advise (it's executed before the pointcut).
  • The last block is an after advise (it's executed after the pointcut).

7)Add the Adempiere project in the Java Build Path of the AspectJ project (right click on the AspectJ project, Properties, Java Build Path)


Bepivin aspectj 04.jpg


This way the aspect code can reference org.compiere.model.MInvoice

8)Create a run configuration of type Aspect Load-Time Weaving Application. The configuration is identical to the Adempiere standard run configuration but in tab 'LTW Aspectpath' you add the project 'Adempiere Aspect Load Time Weaving' (clicking on User Entries, button Add projects)


Bepivin aspectj 05.jpg


9)Click the button Run