Top Banner
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
29

Course Progress Lecture 1 –Java data binding: Basket example: UML class diagram -> class dictionary without tokens-> language design -> class dictionary.

Dec 13, 2015

Download

Documents

Allan Simmons
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Course Progress Lecture 1 –Java data binding: Basket example: UML class diagram -> class dictionary without tokens-> language design -> class dictionary.

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

Page 2: Course Progress Lecture 1 –Java data binding: Basket example: UML class diagram -> class dictionary without tokens-> language design -> class dictionary.

Course Progress

• Lecture 2– AspectJ introduction (continued)– Using AspectJ to introduce DJ– lec1a-3360-w03.ppt

Page 3: Course Progress Lecture 1 –Java data binding: Basket example: UML class diagram -> class dictionary without tokens-> language design -> class dictionary.

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

Page 4: Course Progress Lecture 1 –Java data binding: Basket example: UML class diagram -> class dictionary without tokens-> language design -> class dictionary.

Lecture 4

• Continue with AspectJ, parsing, class dictionary for class dictionaries, patterns for AP

Page 5: Course Progress Lecture 1 –Java data binding: Basket example: UML class diagram -> class dictionary without tokens-> language design -> class dictionary.

Law of Demeter(Join Point Form)

JPT(ID) =

[<target> ID]

<args> List(ID)

<children> List(JPT)

[<ret> ID].

List(S) ~ {S}.

Page 6: Course Progress Lecture 1 –Java data binding: Basket example: UML class diagram -> class dictionary without tokens-> language design -> class dictionary.

JPT(ID) =

[<target> ID]

<args> List(ID)

<children> List(JPT)

[<ret> ID].

List(S) ~ {S}.Jr1.foo1()a1.bar()t2.foo2()r3.foo2()

Etarget t2args {a1,a2}

target t2ret r1

target nullret r3

Page 7: Course Progress Lecture 1 –Java data binding: Basket example: UML class diagram -> class dictionary without tokens-> language design -> class dictionary.

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:

Page 8: Course Progress Lecture 1 –Java data binding: Basket example: UML class diagram -> class dictionary without tokens-> language design -> class dictionary.

• 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.

Generic Law of Demeter(Join Point Form)

Page 9: Course Progress Lecture 1 –Java data binding: Basket example: UML class diagram -> class dictionary without tokens-> language design -> class dictionary.

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

Page 10: Course Progress Lecture 1 –Java data binding: Basket example: UML class diagram -> class dictionary without tokens-> language design -> class dictionary.

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.

Page 11: Course Progress Lecture 1 –Java data binding: Basket example: UML class diagram -> class dictionary without tokens-> language design -> class dictionary.

Join Point Form

• The pointcuts ArgumentRule and AssociatedRule select the ``good'' join points.

• ArgumentRule selects those join points whose target is one of the arguments of the enclosing join point;

Page 12: Course Progress Lecture 1 –Java data binding: Basket example: UML class diagram -> class dictionary without tokens-> language design -> class dictionary.

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.

Page 13: Course Progress Lecture 1 –Java data binding: Basket example: UML class diagram -> class dictionary without tokens-> language design -> class dictionary.

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.

Fred (AOSD 02): simplest AOP language: decision points, branches

Page 14: Course Progress Lecture 1 –Java data binding: Basket example: UML class diagram -> class dictionary without tokens-> language design -> class dictionary.

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.

Page 15: Course Progress Lecture 1 –Java data binding: Basket example: UML class diagram -> class dictionary without tokens-> language design -> class dictionary.

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.

Page 16: Course Progress Lecture 1 –Java data binding: Basket example: UML class diagram -> class dictionary without tokens-> language design -> class dictionary.

AspectJ code

• In AOSD 2003 paper with David Lorenz and Pengcheng Wu

Page 17: Course Progress Lecture 1 –Java data binding: Basket example: UML class diagram -> class dictionary without tokens-> language design -> class dictionary.

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

Page 18: Course Progress Lecture 1 –Java data binding: Basket example: UML class diagram -> class dictionary without tokens-> language design -> class dictionary.

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

Class Any continued

Page 19: Course Progress Lecture 1 –Java data binding: Basket example: UML class diagram -> class dictionary without tokens-> language design -> class dictionary.

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

Page 20: Course Progress Lecture 1 –Java data binding: Basket example: UML class diagram -> class dictionary without tokens-> language design -> class dictionary.

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

Page 21: Course Progress Lecture 1 –Java data binding: Basket example: UML class diagram -> class dictionary without tokens-> language design -> class dictionary.

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

Page 22: Course Progress Lecture 1 –Java data binding: Basket example: UML class diagram -> class dictionary without tokens-> language design -> class dictionary.

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

Page 23: Course Progress Lecture 1 –Java data binding: Basket example: UML class diagram -> class dictionary without tokens-> language design -> class dictionary.

Help for caching homework

Page 24: Course Progress Lecture 1 –Java data binding: Basket example: UML class diagram -> class dictionary without tokens-> language design -> class dictionary.

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

Page 25: Course Progress Lecture 1 –Java data binding: Basket example: UML class diagram -> class dictionary without tokens-> language design -> class dictionary.

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

Page 26: Course Progress Lecture 1 –Java data binding: Basket example: UML class diagram -> class dictionary without tokens-> language design -> class dictionary.

The proceed form takes as arguments the context exposedby 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;} }

Page 27: Course Progress Lecture 1 –Java data binding: Basket example: UML class diagram -> class dictionary without tokens-> language design -> class dictionary.

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

Page 28: Course Progress Lecture 1 –Java data binding: Basket example: UML class diagram -> class dictionary without tokens-> language design -> class dictionary.

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

Page 29: Course Progress Lecture 1 –Java data binding: Basket example: UML class diagram -> class dictionary without tokens-> language design -> class dictionary.

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