290 likes | 398 Views
Course Progress. Lecture 1 Java data binding: Basket example: UML class diagram -> class dictionary without tokens-> language design -> class dictionary with token -> adaptive programming with DJ library lec1-3360-w03.ppt Introduction to AspectJ Intertype declarations Around advice.
E N D
Course Progress • Lecture 1 • Java data binding: Basket example: UML class diagram -> class dictionary without tokens-> language design -> class dictionary with token -> adaptive programming with DJ library • lec1-3360-w03.ppt • Introduction to AspectJ • Intertype declarations • Around advice
Course Progress • Lecture 2 • AspectJ introduction (continued) • Using AspectJ to introduce DJ • lec1a-3360-w03.ppt
Lecture 3 • AspectJ: lecAspectJ-w03.ppt: 56 • assign3-com3360.txt: done • Parsing: lec2-3360-w03.ppt • Theory: lec1b-navig-object-graphs-3360.ppt: done • Patterns: lec2a-PLAP-3360.ppt • Class dictionary for class dictionaries: started
Lecture 4 • Continue with AspectJ, parsing, class dictionary for class dictionaries, patterns for AP
Law of Demeter(Join Point Form) JPT(ID) = [<target> ID] <args> List(ID) <children> List(JPT) [<ret> ID]. List(S) ~ {S}.
JPT(ID) = [<target> ID] <args> List(ID) <children> List(JPT) [<ret> ID]. List(S) ~ {S}. E target t2 args {a1,a2} target t2 ret r1 J r1.foo1() a1.bar() t2.foo2() r3.foo2() target null ret r3
Generic Law of Demeter(Join Point Form) Definition 1: The LoD_JPF requires that for each join point J, target(J) is a potential preferred supplier of J. Definition 2: The set of potential preferred suppliers to a join point J, child to the enclosing join point E, is the union of the objects in the following sets:
Generic Law of Demeter(Join Point Form) • Argument rule: the args of the enclosing join point E, including the target • Associated rule: the associated values of E: the ret values of the children of E before J whose target is the target of E or whose target is null.
aspect LoD extends Violation { pointcut LoD_JPF(): //LoD definition ArgumentRule() || AssociatedRule(); pointcut ArgumentRule(): if(thisEnclosingJoinPoint.getArgs() .contains(thisJoinPoint.getTarget()); pointcut AssociatedRule(): if(thisEnclosingJoinPoint .hasSelfishChild(thisJoinPoint .getTarget())); }
Pseudo Aspect • LoD is a ``pseudo'' aspect because it cannot run in the current implementation of AspectJ, which doesn't allow declare warning to be defined on any pointcut with an if expression.
Join Point Form • The pointcuts ArgumentRule and AssociatedRule select the ``good'' join points. • ArgumentRuleselects those join points whose target is one of the arguments of the enclosing join point;
Join Point Form • AssociatedRule selects those join points whose target is in the set of locally returned ID's, and the ID's created in the siblings of the current node.
Fred (AOSD 02): simplest AOP language: decision points, branches LoD for Fred (D. Orleans) • The set of potential preferred suppliers to a message-send expression M in the body of a branch B is the union of the objects in the following sets: • the argument list A of the decision point E that caused the invocation of B; • the associated values of E, that is, • the results of message-send expressions M' in the body of B before M whose argument lists A' intersect with A; • instances that were created in the control flow of the body of B before M.
Map Dynamic Object Form (DOF) to LoD_JPF • We use LoD_JPF pointcut to check DOF: • Dynamic join point model is mapped to JPT. • Object is mapped to ID. • Method invocations are mapped to JPF join points. The enclosing join point is the parent in the control flow.
Map Lexical Class Form (LCF) to LoD_JPF • We use LoD_JPF to check LCF as follows. • Lexical join point model is mapped to JPT. Lexical join points are nodes in the abstract syntax tree • Class is mapped to ID. • Join points are signatures of call sites. The enclosing join point is the signature of the method in which the call site resides. To run the aspect, a suitable ordering has to be given to the elements of children: • all constructor calls, followed by local method calls, followed by the other join points.
AspectJ code • In AOSD 2003 paper with David Lorenz and Pengcheng Wu
package lawOfDemeter; public abstract class Any { public pointcut scope(): !within(lawOfDemeter..*) && !cflow(withincode(* lawOfDemeter..*(..))); public pointcut StaticInitialization(): scope() && staticinitialization(*); public pointcut MethodCallSite(): scope() && call(* *(..)); public pointcut ConstructorCall(): scope() && call(*.new (..)); public pointcut MethodExecution(): scope() && execution(* *(..)); public pointcut ConstructorExecution(): scope() && execution(*.new (..)); public pointcut Execution(): ConstructorExecution() || MethodExecution(); public pointcut MethodCall(Object thiz, Object target): MethodCallSite() && this(thiz) && target(target);
Class Any continued public pointcut SelfCall(Object thiz, Object target): MethodCall(thiz, target) && if(thiz == target); public pointcut StaticCall(): scope() && call(static * *(..)); public pointcut Set(Object value): scope() && set(* *.*) && args(value); public pointcut Initialization(): scope() && initialization(*.new(..)); }
package lawOfDemeter.objectform; import java.util.*; abstract class ObjectSupplier { protected boolean containsValue(Object supplier){ return targets.containsValue(supplier); } protected void add(Object key,Object value){ targets.put(key,value); } protected void addValue(Object supplier) { add(supplier,supplier); } protected void addAll(Object[] suppliers) { for(int i=0; i< suppliers.length; i++) addValue(suppliers[i]); } private IdentityHashMap targets = new IdentityHashMap(); }
package lawOfDemeter.objectform; public aspect Pertarget extends ObjectSupplier pertarget(Any.Initialization()) { before(Object value): Any.Set(value) { add(fieldIdentity(thisJoinPointStaticPart), value); } public boolean contains(Object target) { return super.containsValue(target) || Percflow.aspectOf().containsValue(target); } private String fieldIdentity(JoinPoint.StaticPart sp) { … } private static HashMap fieldNames = new HashMap(); }
package lawOfDemeter.objectform; aspect Check { private pointcut IgnoreCalls(): call(* java..*.*(..)); private pointcut IgnoreTargets(): get(static * java..*.*); after() returning(Object o):IgnoreTargets() { ignoredTargets.put(o,o); } after(Object thiz,Object target): Any.MethodCall(thiz, target) && !IgnoreCalls() { if (!ignoredTargets.containsKey(target) && !Pertarget.aspectOf(thiz).contains(target)) System.out.println( " !! LoD Object Violation !! " + thisJoinPointStaticPart/*[*/ + at(thisJoinPointStaticPart)/*]*/); } private IdentityHashMap ignoredTargets = new IdentityHashMap();}
package lawOfDemeter.objectform; aspect Percflow extends ObjectSupplier percflow(Any.Execution() || Any.Initialization()){ before(): Any.Execution() { addValue(thisJoinPoint.getThis()); addAll(thisJoinPoint.getArgs()); } after() returning (Object result): Any.SelfCall(Object,Object) || Any.StaticCall() || Any.ConstructorCall() { addValue(result); } }
aspect cache { Hashtable cache=new Hashtable(); // … pointcut getvalue(Item i):target(i) && call(* *.check(..)); // … int around(Item i):getvalue(i){ if(cache.containsKey(i)) return ((Integer)cache.get(i)).intValue(); int v=proceed(i); cache.put(i,new Integer(v)); } }
Three kinds of after: aspect A { pointcut publicCall(): call(public Object *(..)); after() returning (Object o): publicCall() { System.out.println("Returned normally with " + o); } after() throwing (Exception e): publicCall() { System.out.println("Threw an exception: " + e); } after(): publicCall() { System.out.println("Returned or threw an Exception"); } }
The proceed form takes as arguments the context exposed by the around's pointcut, and returns whatever the around is declared to return. So the following around advice will double the second argument to foo whenever it is called, and then halve its result: aspect A { int around(int i): call(int C.foo(Object, int)) && args(i) {int newi = proceed(i*2) return newi/2;} }
If the return value of around advice is typed to Object, then the result of proceed is converted to an object representation, even if it is originally a primitive value. And when the advice returns an Object value, that value is converted back to whatever representation it was originally. So another way to write the doubling and halving advice is: aspect A { Object around(int i): call(int C.foo(Object, int)) && args(i) { Integer newi = (Integer) proceed(i*2); return new Integer(newi.intValue() / 2); } }
package back; import java.util.*; public abstract aspect Back { public interface Source { Vector getTargets(); } public interface Target {} public abstract pointcut associate(Source s, Target t); void around(Source s, Target t): associate(s, t) { Vector targets = s.getTargets(); int sizeBefore = targets.size(); proceed(s, t); int sizeAfter = targets.size(); t.back = sizeBefore<sizeAfter? s: null; } public Source Target.getSource() { return back; } private Source Target.back; } The Back Aspect
package connection; import java.util.Vector; import back.Back; import back.Back.Source; import back.Back.Target; import cache.Caching; import cache.Caching.Cached; aspect SuperimposeBack extends Back { declare parents: Item implements Target; declare parents: Container implements Source; public pointcut associate(Source s,Target t): target(s) && execution(void Container.addItem(Item)) && args(t) ; }