Tuesday, August 18, 2015

How to create a Hello World with IntelliJ and Aspect J

General

Since the whole AOP programming method is new to me AND it took me a while to understand how to work with AspectJ and IntelliJ I've decided to create this brief tutorial on how to setup simple Hello World project using AspectJ and IntellJ.

The Ingredients 

  1. IntelliJ Ultimate (I used version 14, you can get it from here: https://www.jetbrains.com/idea/download/)
    1. NOTICE: You must have the ultimate edition. I was not able to use AspectJ with community edition!
    2. I hope you know how to install IntellJ, basically it's next next next.
  2. AspectJ installation (I've used version 1.8.6, you can get the installation JAR here http://www.eclipse.org/downloads/download.php?file=/tools/aspectj/aspectj-1.8.6.jar)
    1. You need to install the jar by double clicking on it in Windows or just run the command from command line 'java -jar aspectj-1.8.6.jar'
    2. NOTICE: Remember where the aspect installtion path since we'll need it later. In windows the default is c:\ (My installation was to c:\aspectj1.8)

So, let's start...

Configuration

  1. Start the IntelliJ
  2. Click on Create new project
  3. Choose Java project
  4. Don't use template, just click next
  5. Name the project AspectJDemoHelloWorld
  6. Click Finish
  7. Open the settings (in windows Ctrl+Alt+S)
  8. Type compiler in the top search field, Chooe the Java Compiler. You should see something like this
  9. Click on Use compiler and choose Ajc (That is the AspetJ compiler)
  10. Click on the 'Path to Ajc compiler' browse button (near the Test button) and choose the 'aspectjtools.jar' at the lib directory of the AspectJ installation (In my case, C:\aspectj1.8\lib\aspectjtools.jar)
  11. Click on the Test button. You should see something like this 
  12. In the search field, type plugins. You should something like this
  13. Click on the search field inside the plugins window and type AspectJ. Verify both plugins are enabled
  14. Press ok
  15. Open the project preferences (Ctrl + Alt + Shift + S in windows)
  16. Click on Modules in the left navigation pane and then click Dependencies.
  17. Click on the plus sign and choose JARs or directories.
  18. Go to the installation folder of the AspectJ and choose  the file 'aspectjrt.jar'
  19. Press OK.
  20. That's for the configuration part

Coding


  1. Go to your src directory
  2. Create a new JAVA class named Hello, Paste in the following
  3. public class Hello {
    
        public static void main(String[] args) {
            sayHello();
        }
    
        public static void sayHello() {
            System.out.print("Hello ");
        }
    }
    
    







  4. Create a new Aspect. Right click and choose Aspect (This ability was enabled due to adding of aspectjrt.jar)
  5. Name the Aspect World and click ok
  6. Paste the following in the aspect

  7. public aspect World {

        pointcut greeting() : execution (* Hello.sayHello(..));
    
        after() returning() : greeting() {
            System.out.println("World");
        }
    }
  8. Click on Build --> Rebuild project
  9. Right click on Hello.java and choose Run
  10. You should see 'Hello World' in the System console
If you only see Hello, that means the Aspect code was not compiled meaning, you should check the Ajc compiler is the compiler chosen (see step 7 in the configuration part)

2 comments: