יום חמישי, 27 ביוני 2013

simple aspectj

A simple aspectj program:

The class to be aspected

  1: package Learn.aspectj;
  2: 
  3: public class TestCls {
  4:   public TestCls()
  5:   {
  6: 
  7:   }
  8: 
  9:   public void GetCurrentThreadID ()
 10:   {
 11:      long threadId = Thread.currentThread().getId();
 12: 
 13:      System.out.print("The Thread ID = " + threadId + "\r\n");
 14:   }
 15: }
 16: 

The test class


  1: package Learn.aspectj;
  2: 
  3: public class MainCls {
  4: 
  5:   /**
  6:    * @param args
  7:    */
  8:   public static void main(String[] args) {
  9:     // TODO Auto-generated method stub
 10: 
 11:     System.out.print("This is my test \r\n ");
 12:     
 13:     TestCls theTestCls = new TestCls();
 14:     
 15:     theTestCls.GetCurrentThreadID();
 16:     
 17:   }
 18: 
 19: }

The aspect declaration


  1: package Learn.aspectj;
  2: 
  3: public aspect ChangeToOtherThread2 {
  4: 
  5:   pointcut DoInOtherThreadpc():
  6:       call(void TestCls.GetCurrentThreadID()); 
  7:    
  8:   before(): DoInOtherThreadpc() {
  9:       System.out.println("before call to DoInOtherThreadpc");
 10:   }
 11:   
 12:   after(): DoInOtherThreadpc() {
 13:       System.out.println("after call to DoInOtherThreadpc");
 14:   }    
 15: }
 16: 

The eclipse support :
Capture43


Capture44


The result:
This is my test
before call to DoInOtherThreadpc
The Thread ID = 1
after call to DoInOtherThreadpc
Resources:
http://www.eclipse.org/aspectj/doc/next/progguide/starting-aspectj.html#pointcuts

אין תגובות:

הוסף רשומת תגובה