CS 683 Emerging Technologies Spring Semester, 2003 AspectJ Syntax 3 |
||
---|---|---|
© 2003, All Rights Reserved, SDSU & Roger Whitney San Diego State University -- This page last updated 03-Feb-03 |
Non-Singleton Aspects
aspect perthis(Pointcut) { blah }
Non-Singleton Hello Class for Examples
public class Hello { void b() { System.out.println("In b"); } }
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
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
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(); } }
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(); } }
Information about Join Points
thisJoinPoint and thisJoinPointStaticPart
public class Hello { int b(int foo, String bar) { System.out.println("In b"); return foo + 1; } }
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]); } } }
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
Order & Conflicts Among Aspects
Multiple pieces of advice may apply to the same join point
Advice precedence determines the order to apply the advice
Determining precedence
Advice in Different Aspects
aspect B { blah } aspect A dominates B { blah }
abstract aspect B { } public aspect A extends B { }
Determining precedence
Advice in the same Aspect
aspect A { before(): execution(void main(String[] args)) {} after(): execution(void main(String[] args)) {} before(): execution(void main(String[] args)) {} }
Effects of precedence
At a particular join point, advice is ordered by precedence
around advice