extending the field access pointcuts of aspectj to arrays ics 2006 – taipei, december 2006 kung...

24
Extending the Field Acce ss Pointcuts of AspectJ to Arrays ICS 2006 – Taipei, December 2006 Kung Chen and Chin-Hung Chien* National Chengchi University Hon Hai Precision Industry Co., Ltd *

Upload: toby-lloyd

Post on 19-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Extending the Field Access Pointcuts of AspectJ to Arrays ICS 2006 – Taipei, December 2006 Kung Chen and Chin-Hung Chien* National Chengchi University

Extending the Field Access Pointcuts of AspectJ to Arrays

ICS 2006 – Taipei, December 2006

Kung Chen and Chin-Hung Chien*National Chengchi University

Hon Hai Precision Industry Co., Ltd*

Page 2: Extending the Field Access Pointcuts of AspectJ to Arrays ICS 2006 – Taipei, December 2006 Kung Chen and Chin-Hung Chien* National Chengchi University

Agenda

• Background: Aspect-Oriented Programming (AOP) in AspectJ

• Motivation

• Design Considerations

• Specification of the New Pointcuts

• Implementation

• Conclusions

Page 3: Extending the Field Access Pointcuts of AspectJ to Arrays ICS 2006 – Taipei, December 2006 Kung Chen and Chin-Hung Chien* National Chengchi University

Basic Mechanisms of AOP

where

Do what

aspect

Class Classadvice

weaving

pointcut

pointcut

pointcut

• Aspect• Pointcut• Advice

• Weaving

Implement Crosscutting concerns modularly

Page 4: Extending the Field Access Pointcuts of AspectJ to Arrays ICS 2006 – Taipei, December 2006 Kung Chen and Chin-Hung Chien* National Chengchi University

Aspects in AspectJaspect Name [extends a] { pointcut1; pointcut2; … advice1; advice2; …

fields methods}

Crosscutting points

Actions to take in matched pointcuts

Pointcut: selecting a collection of join points

Page 5: Extending the Field Access Pointcuts of AspectJ to Arrays ICS 2006 – Taipei, December 2006 Kung Chen and Chin-Hung Chien* National Chengchi University

Join Points Model of AspectJ, 1

• Join point is a well-defined point in a program’s execution

• Method call:public void move(int dx, int dy) { setX(_x + dx); setY(_y + dy); }

Method call join point:

Page 6: Extending the Field Access Pointcuts of AspectJ to Arrays ICS 2006 – Taipei, December 2006 Kung Chen and Chin-Hung Chien* National Chengchi University

Join Points Model of AspectJ, 2

• Our focus: Field reference and set join points:

public void setX(int newx) { x = newx; ..println(x); }

field set join point:field reference join point:

Page 7: Extending the Field Access Pointcuts of AspectJ to Arrays ICS 2006 – Taipei, December 2006 Kung Chen and Chin-Hung Chien* National Chengchi University

Agenda

• Background: Aspect-Oriented Programming (AOP) in AspectJ

• Motivation

• Design Considerations

• Specification of the New Pointcuts

• Implementation

• Conclusions

Page 8: Extending the Field Access Pointcuts of AspectJ to Arrays ICS 2006 – Taipei, December 2006 Kung Chen and Chin-Hung Chien* National Chengchi University

Field Access Join Points of AspectJ

• Join Points– field reference– field set

• Pointcut Designators– get(aFieldSignature)– set(aFieldSignature)

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(); }}

• Example

Page 9: Extending the Field Access Pointcuts of AspectJ to Arrays ICS 2006 – Taipei, December 2006 Kung Chen and Chin-Hung Chien* National Chengchi University

Limitations in Using Field Access Pointcuts

• What if the fields we are interested in are arrays?

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 }

set(* *.ar)

after() returning (int[] a) : get(* *.ar)

Page 10: Extending the Field Access Pointcuts of AspectJ to Arrays ICS 2006 – Taipei, December 2006 Kung Chen and Chin-Hung Chien* National Chengchi University

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 se

t or retrieved, and– the value being set to or retrieved from the arr

ay element.

ar[0] = 200;

Page 11: Extending the Field Access Pointcuts of AspectJ to Arrays ICS 2006 – Taipei, December 2006 Kung Chen and Chin-Hung Chien* National Chengchi University

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)”

Page 12: Extending the Field Access Pointcuts of AspectJ to Arrays ICS 2006 – Taipei, December 2006 Kung Chen and Chin-Hung Chien* National Chengchi University

Problems with Harbulot’s Work

• Ambiguous matching of join points

arrayset() && args(i, s) && target(a)

arrayset() && args(i1,i2,s) && target(a)

ss[0][1] = “two join points”;

sss[0][1][2] = “two join points, too”;

Mismatch

Mismatch

actually two assignments to two 1-D arrays on the byte-code level

actually three assignments to

three 1-D arrays on the byte-code level

Match

Page 13: Extending the Field Access Pointcuts of AspectJ to Arrays ICS 2006 – Taipei, December 2006 Kung Chen and Chin-Hung Chien* National Chengchi University

Agenda

• Background: Aspect-Oriented Programming (AOP) in AspectJ

• Motivation

• Design Considerations

• Specification of the New Pointcuts

• Implementation

• Conclusions

Page 14: Extending the Field Access Pointcuts of AspectJ to Arrays ICS 2006 – Taipei, December 2006 Kung Chen and Chin-Hung Chien* National Chengchi University

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.

Page 15: Extending the Field Access Pointcuts of AspectJ to Arrays ICS 2006 – Taipei, December 2006 Kung Chen and Chin-Hung Chien* National Chengchi University

Requirements

• Our arrayset field set pointcut must be flexible enough to select all of them, either individually or as a group.

• Assignments to a multi-dimensional array can take several forms.

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];… }

Page 16: Extending the Field Access Pointcuts of AspectJ to Arrays ICS 2006 – Taipei, December 2006 Kung Chen and Chin-Hung Chien* National Chengchi University

Agenda

• Background: Aspect-Oriented Programming (AOP) in AspectJ

• Motivation

• Design Considerations

• Specification of the New Pointcuts

• Implementation

• Conclusions

Page 17: Extending the Field Access Pointcuts of AspectJ to Arrays ICS 2006 – Taipei, December 2006 Kung Chen and Chin-Hung Chien* National Chengchi University

New Field Access Pointcuts• Conservative extension of the standard field poin

tcuts:– 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

arrayset(* Watch.*)

arrayget(* Watch.*)

Page 18: Extending the Field Access Pointcuts of AspectJ to Arrays ICS 2006 – Taipei, December 2006 Kung Chen and Chin-Hung Chien* National Chengchi University

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.

Page 19: Extending the Field Access Pointcuts of AspectJ to Arrays ICS 2006 – Taipei, December 2006 Kung Chen and Chin-Hung Chien* National Chengchi University

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.

Page 20: Extending the Field Access Pointcuts of AspectJ to Arrays ICS 2006 – Taipei, December 2006 Kung Chen and Chin-Hung Chien* National Chengchi University

Agenda

• Background: Aspect-Oriented Programming (AOP) in AspectJ

• Motivation

• Design Considerations

• Specification of the New Pointcuts

• Implementation

• Conclusions

Page 21: Extending the Field Access Pointcuts of AspectJ to Arrays ICS 2006 – Taipei, December 2006 Kung Chen and Chin-Hung Chien* National Chengchi University

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

Page 22: Extending the Field Access Pointcuts of AspectJ to Arrays ICS 2006 – Taipei, December 2006 Kung Chen and Chin-Hung Chien* National Chengchi University

FSM Based Shadow Matcher

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()

#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

class C { … Object a[][][] = new Object[2][2][2]; … a[0][1][2] = new String("foo"); … }

Page 23: Extending the Field Access Pointcuts of AspectJ to Arrays ICS 2006 – Taipei, December 2006 Kung Chen and Chin-Hung Chien* National Chengchi University

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.

Page 24: Extending the Field Access Pointcuts of AspectJ to Arrays ICS 2006 – Taipei, December 2006 Kung Chen and Chin-Hung Chien* National Chengchi University

Q & A