SDSU CS 683 Emerging Technologies
Spring Semester, 2003
AspectJ Syntax 3
Previous    Lecture Notes Index    Next    
© 2003, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 03-Feb-03

Contents of Doc 5, AspectJ Syntax 3



Reference

The AspectJ Programming Guide

Reading

The AspectJ Programming Guide

Section 1 Getting Started with AspectJ
Section 2 The AspectJ Language


Doc 5, AspectJ Syntax 3 Slide # 2

Non-Singleton Aspects


aspect perthis(Pointcut) { blah }
An instance of the aspect is created for every object that is the executing object of any join points of the Pointcut
aspect pertarget(Pointcut) { blah }
An instance of the aspect is created for every object that is the target object of any join points of the Pointcut
ajc would not compile examples with pertarget
aspect percflow(Pointcut) { blah }
An instance of the aspect is created for flow of control of the join points of the Pointcut

Created when flow in entered
aspect percflowbelow(Pointcut) { blah }
An instance of the aspect is created for flow of control of the join points of the Pointcut

Created when flow in entered


Doc 5, AspectJ Syntax 3 Slide # 3
Non-Singleton Hello Class for Examples

public class Hello {
    void b() {
      System.out.println("In b");
   }
} 


Doc 5, AspectJ Syntax 3 Slide # 4

percflow


public  aspect HelloAspect percflow(methodCall() ) {
   
   private int callCount = 0;
   private static int totalCount = 0;
   
   public int getCount() {
      return callCount;
   }
   
   public int getTotalCount() {
      return totalCount;
   }
   
   public static void main(String[] arguments ) {
      Hello example = new Hello();
      example.b();
      HelloAspect aspect = HelloAspect.aspectOf();
      System.out.println( "Object Count is " + aspect.getCount()
         + " Total count " + aspect.getTotalCount() );
   }
   
   pointcut methodCall() : call(* *(..) );
   
   before(): methodCall() {
      callCount++;
      totalCount++;
   }
Output of java HelloAspect
In b
Object Count is 1 Total count 6

Doc 5, AspectJ Syntax 3 Slide # 5

perthis

public  aspect HelloAspect perthis(methodCall() ) {
   private int callCount = 0;
   private static int totalCount = 0;
   public int getCount() {
      return callCount;
   }
   public int getTotalCount() {
      return totalCount;
   }
   public static void main(String[] arguments ) {
      Hello example = new Hello();
      example.b();
      example.b();
      (new Hello()).b();
      HelloAspect aspect = HelloAspect.aspectOf(example);
      System.out.println( "Object Count is " + aspect.getCount()
         + " Total count " + aspect.getTotalCount() );
   }
   pointcut methodCall() : call(* *(..) );
   before(): methodCall() {
      callCount++;
      totalCount++;
   }
} 
java HelloAspect
In b
In b
In b
Object Count is 2 Total count 3


Doc 5, AspectJ Syntax 3 Slide # 6

Privileged Aspects


Privileged aspects have access to all members of other classes & aspects

public class Hello {
    private void b() {
      System.out.println("In b");
   }
} 

public privileged  aspect HelloAspect {
   public static void main(String[] arguments ) {
      Hello example = new Hello();
      example.b();
   }
} 


Doc 5, AspectJ Syntax 3 Slide # 7
How this Works

Code generated by ajc for Hello.java

/*   Generated by AspectJ version 1.0.6 */
   
public class Hello {
  private void b() {
    System.out.println("In b");
  } 
   
  public Hello() {
    super();
  }
    
  public final void b$ajc$backdoor() {
    this.b();
  } 
} 


Doc 5, AspectJ Syntax 3 Slide # 8

Information about Join Points


thisJoinPoint and thisJoinPointStaticPart
Provide information about the join point selected by a pointcut

Download the AspectJ documentation and view the JavaDoc for the org.aspectj.lang classes

Example

public class Hello {
    int b(int foo, String bar) {
      System.out.println("In b");
      return foo + 1;
   }
} 



Doc 5, AspectJ Syntax 3 Slide # 9
Aspect
import org.aspectj.lang.*;
import org.aspectj.lang.reflect.CodeSignature;
public privileged  aspect HelloAspect {
   public static void main(String[] arguments ) {
      Hello example = new Hello();
      int answer = example.b(5, "cat");
   }
   Object around() : call(* b(..) ) {
      System.out.println("Intercepted message: " +
         thisJoinPointStaticPart.getSignature().getName());
      System.out.println("In class: " +
         thisJoinPointStaticPart.getSignature().getDeclaringType().getName());
      printParameters(thisJoinPoint);
      Object result = proceed();
      System.out.println( "Result " + result);
      return result;
   }
   private void printParameters(JoinPoint jp) {
      System.out.println("Arguments: " );
      Object[] args = jp.getArgs();
      String[] names = ((CodeSignature)jp.getSignature()).getParameterNames();
      Class[] types = ((CodeSignature)jp.getSignature()).getParameterTypes();
      for (int i = 0; i < args.length; i++) {
         System.out.println(" " + i + ". " + names[i] +
            " : " + types[i].getName() +
            " = " + args[i]);
      }
   }
 }

Doc 5, AspectJ Syntax 3 Slide # 10
Result of running java HelloAspect

Intercepted message: b
In class: Hello
Arguments:
0. foo : int = 5
1. bar : java.lang.String = cat
In b
Result 6


Doc 5, AspectJ Syntax 3 Slide # 11

Order & Conflicts Among Aspects


Multiple pieces of advice may apply to the same join point

Advice precedence determines the order to apply the advice


Doc 5, AspectJ Syntax 3 Slide # 12
Determining precedence

Advice in Different Aspects


   aspect B { blah
   }
   aspect A dominates B { blah
   }


   abstract aspect B {
   }
   public aspect A extends B {
   }




Doc 5, AspectJ Syntax 3 Slide # 13
Determining precedence

Advice in the same Aspect




Circularities

aspect A {
   before(): execution(void main(String[] args)) {}
   after(): execution(void main(String[] args)) {}
   before(): execution(void main(String[] args)) {}
}

Circularities in using the rules are compile errors


Doc 5, AspectJ Syntax 3 Slide # 14
Effects of precedence

At a particular join point, advice is ordered by precedence

around advice
If calls proceed then advice with next precedence is run
Otherwise advice with lower precedence at this join point is not run

before advice
If throws an exception advice with lower precedence is not run



Copyright ©, All rights reserved.
2003 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
OpenContent license defines the copyright on this document.

Previous    visitors since 03-Feb-03    Next