Saturday, June 27, 2009

Terracotta like implementation :: code

So this is what we thought:

The user object we remove all the field variable initializations and replace them with our custom code. So, the user says

this.list = new ArrayList(),

and during the byte code construction we will mke this equivalent to

this.list= new MyList().

Now the successive updates now happen over MYList which records the deltas.

So, say our object is UserAttributes::

public class UserAttributes implements Interceptable {

@LIST(3)
private ArrayList alias;

public List getAlias() {
return alias;
}

public void setAlias(ArrayList alias) {
this.alias = alias;
}

public static void main(String[] argv) {
UserAttributes u = new UserAttributes();
u.alias = new ArrayList();
System.out.println("alias::" + u.alias + " class:" + u.alias.getClass());
}
}


Now to replace the newArrayList() where ever possible we write a point cut as follows in InAspect.aj file

public aspect INAspect {

pointcut listCut(List value, LIST in) : set(@LIST * * ) && args(value) && @annotation(in);

@SuppressWarnings("unchecked")
void around(List value, LIST in) :listCut(value,in) {
System.out.println("**annotation " + thisJoinPoint + " ann:"
+ in.value());
value = new MyList(in.value());
value.add("val 1");
proceed(value, in);
}
}

Now the sop of the UserAttributes gives the out put as MyList class with one entry in it. Let me put few words abt the listCut.

  1. set(@LIST * * )&& @annotation(in) => cut all the modifying fields of the fields annotated with LIST annotation. Pickeup the annotation into in variable
  2. pointcut listCut(List value...args(value)=> picks up the modifying data as value variable. If u modify the value then it makes modification in the userAttributes class and thats what happens in the advice: around...
  3. proceed continues with the remaining point cuts (if any)


No comments:

Post a Comment