240 likes | 353 Views
Extending the Field Access Pointcuts of AspectJ to Arrays. Kung Chen and Chin-Hung Chien* National Chengchi University Hon Hai Precision Industry Co., Ltd*. ICS 2006 – Taipei, December 2006. Agenda. Background: Aspect-Oriented Programming (AOP) in AspectJ Motivation
E N D
Extending the Field Access Pointcuts of AspectJ to Arrays Kung Chen and Chin-Hung Chien* National Chengchi University Hon Hai Precision Industry Co., Ltd* ICS 2006 – Taipei, December 2006
Agenda • Background: Aspect-Oriented Programming (AOP) in AspectJ • Motivation • Design Considerations • Specification of the New Pointcuts • Implementation • Conclusions
Basic Mechanisms of AOP • Aspect • Pointcut • Advice • Weaving Implement Crosscutting concerns modularly advice Class Class pointcut pointcut pointcut aspect where weaving Do what
Aspects in AspectJ aspect Name [extends a] { pointcut1; pointcut2; … advice1; advice2; … fields methods } Crosscutting points Actions to take in matched pointcuts Pointcut: selecting a collection of join points
Method call join point: Join Points Model of AspectJ, 1 • Join point is a well-defined point in a program’s execution • Method call: publicvoid move(int dx, int dy) { setX(_x + dx); setY(_y + dy); }
field set join point: field reference join point: Join Points Model of AspectJ, 2 • Our focus: Field reference and set join points: publicvoid setX(int newx) { x = newx; ..println(x); }
Agenda • Background: Aspect-Oriented Programming (AOP) in AspectJ • Motivation • Design Considerations • Specification of the New Pointcuts • Implementation • Conclusions
Example aspect GuardedX { static final int MAX_CHANGE = 100; before(int newval): set(static int T.x)&& args(newval) { if (Math.abs(newval - T.x) > MAX_CHANGE) throw new RuntimeException(); }} Field Access Join Points of AspectJ • Pointcut Designators • get(aFieldSignature) • set(aFieldSignature) • Join Points • field reference • field set
Limitations in Using Field Access Pointcuts • What if the fields we are interested in are arrays? set(* *.ar) 01 public class FieldPointcuts {02 static int ar[];03 public static void main(04 String[] args) {05 ar = new int[] {100}; //set06 ar[0] = 200; //get 07 }08 } after() returning (int[] a) : get(* *.ar)
What We Expect • Field access pointcuts can also be applied to array elements and further expose information on • the index values of the array element being set or retrieved, and • the value being set to or retrieved from the array element. ar[0] = 200;
Related Work • Bruno Harbulot proposed the following arrayset pointcut designator: before(int i, Object s, Object[] a): arrayset() && args(i, s) && target(a) { System.out.println (" Arrayset: ["+i+"/"+(a.length-1)+"] = "+s) ; } • The idea is to treat array element set as a call to a “set(int index, object newValue)”
Mismatch Match Mismatch Problems with Harbulot’s Work • Ambiguous matching of join points arrayset() && args(i, s) && target(a) actually two assignments to two 1-D arrays on the byte-code level ss[0][1] = “two join points”; actually three assignments to three 1-D arrays on the byte-code level arrayset() && args(i1,i2,s) && target(a) sss[0][1][2] = “two join points, too”;
Agenda • Background: Aspect-Oriented Programming (AOP) in AspectJ • Motivation • Design Considerations • Specification of the New Pointcuts • Implementation • Conclusions
How We Address This Problem • Fix the array target(s) of interest • only need to focus on arrays which are fields of some class • arrays local to a method are irrelevant • Include a specification of field signature in our array set/get pointcuts.
Requirements • Assignments to a multi-dimensional array can take several forms. • Our arrayset field set pointcut must be flexible enough to select all of them, either individually or as a group. 01 class Watch { 02 String [][][] sss= new [2][2][2];03 String [][] ss= {{“x”,”y”},{“w”,”z”}}; 04 String [] s= {“abc”, “def”};05 sss[0] = ss;06 sss[0][1] = s;07 sss[0][1][1] = “change”;08 ss [0] = s;09 ss[0][1] = “Me too”;10 s[0] = ss[1][1];… }
Agenda • Background: Aspect-Oriented Programming (AOP) in AspectJ • Motivation • Design Considerations • Specification of the New Pointcuts • Implementation • Conclusions
arrayset(* Watch.*) arrayget(* Watch.*) New Field Access Pointcuts • Conservative extension of the standard field pointcuts: • arrayset(aFieldSignature) • arrayget(aFieldSignature) • Orthogonal to other AspectJ pointcuts • target(), within() and withincode() • All array field set join points are treated as having a variable arguments: • the sequence of index values • the value the field is being set to
Context Exposure, 1 • Using the args() pointcut aspect Monitor { before(int ix1, int ix2, int newVal): arrayset(* Watch.*) &&args(ix1, ix2, newVal) { //advice if (newVal > B.bounds[ix1, ix2]) { ArraySetSignature sig = (ArraySetSignature)tjp.getSignature(); String field = sig.getFieldType() + sig.getName(); throws new RuntimeException("Bad change"+ field) }} 01 class Watch { 02 String [][][] sss= new [2][2][2];03 String [][] ss= {{“x”,”y”},{“w”,”z”}}; 04 String [] s= {“abc”, “def”};05 sss[0] = ss;06 sss[0][1] = s;07 sss[0][1][1] = “change”;08 ss [0] = s;09 ss[0][1] = “Me too”;10 s[0] = ss[1][1];… } • Selective matching – assignments in Line 6 and 9 are captured.
Context Exposure, 2 • Can also use the method thisJoinPoint.getArgs() void around() : arrayset( * data.*.* ) { try { ArraySetSignature sig = (ArraySetSignature)tjp.getSignature();Object[] args = tjp.getArgs(); Integer rhsValue = (Integer)args[args.length-1]; if (rhsValue.intValue() > MAX) { Log.add("Warning: " + sig.getName()); for (int i=0; i<args.length-1; i++) { Log.add("["+ args[i] +"]"); Log.add(" exceeds MAX"); } proceed(); } catch(IndexOutOfBoundException e) {} } • Provides great flexibility.
Agenda • Background: Aspect-Oriented Programming (AOP) in AspectJ • Motivation • Design Considerations • Specification of the New Pointcuts • Implementation • Conclusions
Implementation Using the AspectBench Compiler(abc) for AspectJ • We follow the standard steps outlined by the abc team to develop this extension. • But the following steps are non-trivial: • Identification of join point shadow • Extension of the pointcut matcher of abc
FSM Based Shadow Matcher class C { … Object a[][][] = new Object[2][2][2]; …a[0][1][2] = new String("foo"); … } #1: $r2 = r0.<C:java.lang.Object[][][]a>#2: $r3 = $r2[0]#3: $r4 = $r3[1]#4: $r5 = new java.lang.String#5: specialinvoke $r5.<java.lang.String: void <init>(java.lang.String)>("foo")#6: $r4[2] = $r5 Cond. #1: instanceOf(getRhs(ca), ArrayFieldRef) Cond. #2: instanceOf(getRhs(ca), ArrayRef) && equalBase(getRhs(ca), ima) && instanceOf (getLhs(ca), Local) Cond. #3: instanceOf(getLhs(ca), ArrayRef) && equalBase(getLhs(ca), ima) Cond. #4: !hasNext()
Conclusions • The field access pointcuts of AspectJ can be extended to expose index-value context for array fields. • A FSM-based implementation is presented using the abc compiler for AspectJ. • You are welcome to download it and try it yourself.