Download - For John

Transcript
Page 1: For John

For John

Page 2: For John

DAJ (temporary name for AspectJ with traversals)

• planned to be added to AspectJ

• provides an efficient implementation of traversals using the AP library

• we have also added traversals to Java but implementation is very slow (using reflection)

• AspectJ + traversals > Java + traversals because AspectJ > Java

Page 3: For John

Adding Demeter Traversals to AspectJ

• Because the AspectJ join point model is a generalization of Demeter’s join point model, we can use the AspectJ pointcuts to express the traversal pointcuts for visitors.

• declare ClassGraph

• declare TraversalGraph

• declare Behavior

Page 4: For John

Traversal(t) pointcut

• picks out all join points between the start and the end of the traversal that are needed for the traversal only, i.e. the node visits and the edge visits.

Page 5: For John

Traversal Syntax Exampleaspect MyTraversal {

ClassGraph defaultCG = new ClassGraph();

ClassGraph cg1 = new ClassGraph(defaultCG, “from CompoundFile to * bypassing ->*,tail,*”);

declare traversal t1: “from CompoundFile to SimpleFile”;

declare traversal t2(cg1): “from CompoundFile to *”;}

Page 6: For John

Traversal Syntax Exampleaspect MyTraversal { declare visitors : VisitorInterface1;

ClassGraph defaultCG = new ClassGraph();ClassGraph cg1 = new ClassGraph(defaultCG,

“from CompoundFile to * bypassing ->*,tail,*”);declare traversal t1(defaultCG):

“from CompoundFile to SimpleFile”;declare traversal t2(cg1, VisitorInterface1):

“from CompoundFile to *”;}

Page 7: For John

Generate pointcuts / advice

interface VisitorInterface1 {

void before(Object host);

void after(B host);

void after(C source, D target);

void around(A host);

void start();

void finish();

}

Page 8: For John

M1: Equation System

used = from EquationSystem via -> *,rhs,* to Variable

EquationSystem

Equation_List

Equation Variable

equations

*lhs

rhs

ExpressionSimple

Compound

Numerical

Expression_List*

Addop

args

Ident

S

D

T

B

Page 9: For John

Motivation for edge advice

class LargestVisitor { Expression exp;

void before_rhs(Object s, Expression t){

exp = t;

};

void before(Variable v) {

System.out.println(exp, v)

}

}

from EquationSystem via -> *,rhs,* to Variable

before_rhs < before !

for each used variable v print the largest expression that contains v

Page 10: For John

Motivation for edge advice

class MarkVariableVisitor { boolean through_lhs; …

void before(Variable v) {

if (through_lhs) { … } else

if (through_rhs) { … }

};

void around_lhs(Subtraversal t){

through_lhs = true;

t.proceed();

through_lhs = false;

}; …

}

from EquationSystem to Variable around < before !

for each variable print whetherit is used or defined

Page 11: For John

Self-Contained Example

• Java classes

• Traversal file

• Use traversal defined

Page 12: For John

Basket Example

Basket

FruitPencil

p f

Orange

Weight Color

w c

String

sint i

Page 13: For John

BasketMain.javaclass Weight{

Weight(int _i) { i = _i;}

int i;

int get_i() { return i; }

}

class Pencil {}

class Color {

Color(String _s) { s = _s;}

String s;

}

class Basket {

Basket(Fruit _f, Pencil _p) { f = _f; p = _p; }

Fruit f;

Pencil p;

}

class Fruit {

Fruit(Weight _w) { w = _w; }

Weight w;

}

class Orange extends Fruit {

Orange(Color _c) { super(null); c=_c;}

Orange(Color _c, Weight _w) { super(_w); c = _c;}

Color c;

}

Page 14: For John

BasketMain.javaclass BasketMain {

static public void main(String args[]) throws Exception {

Basket b = new Basket(

new Orange(

new Color("orange"),

new Weight(5)),

new Pencil());

int totalWeight = b.totalWeight();

System.out.println("Total weight of basket = " +

totalWeight); }}

Page 15: For John

Traversals

• Count the total weight within the basket

• Traversal Strategy: “From Basket to Weight”

• Visitor: Add up all the values within Weight

Page 16: For John

Basket Example Traversal Graph

Basket

FruitPencil

p f

Orange

Weight Color

w c

String

sint i

Page 17: For John

Two versions

• define visitor using– AspectJ pointcuts and advice– Java visitor class complete with initialization,

finalization and return value processing

Page 18: For John

BasketTraversal.trv

// traversals for basket

aspect BasketTraversal {

declare ClassGraph default;

declare ClassGraph myClassGraph : default,

"from Basket to * bypassing {->*,*,java.lang.String }");

declare traversal t2(myClassGraph) : "from Basket to Weight";

declare TraversalGraph t2 : myClassGraph, "from Basket to Weight";

}

Page 19: For John

generated: BasketTraversal.java

public aspect BasketTraversal {

// traversal t2 : {source: Basket -> target: Weight} with { }

public void Basket.t2(){

if (f != null) t2_crossing_f();

}

public void Basket.t2_crossing_f() { f.t2();}

public void Fruit.t2(){

if (w != null) t2_crossing_w();

}

public void Fruit.t2_crossing_w() { w.t2();}

public void Weight.t2(){

}

public void Orange.t2(){

super.t2();

}

pointcut pointcut_t2() : call(public void t2*());

before () : pointcut_t2 () {

System.out.println(thisJoinPoint);

}

} // BasketTraversal

for testingtraversal code

Page 20: For John

BasketMainCount.java// the aspect for counting the total weight of the basket

aspect BasketMainCount {

static int returnVal;

int Basket.totalWeight() {

returnVal = 0;

t2();

return returnVal;

}

pointcut t2WeightPC(Weight weight) : call(* *t2*()) && target(weight);

before(Weight weight) : t2WeightPC(weight) {

returnVal += weight.get_i();

}

}

Page 21: For John

BasketTraversal.trv

// traversals for basket

aspect BasketTraversal {

declare ClassGraph default;

declare ClassGraph myClassGraph : default,

"from Basket to * bypassing {->*,*,java.lang.String }");

declare traversal Integer t2(myClassGraph, SumVis) :

"from Basket to Weight";

}

“visitor in Java” version

Page 22: For John

BasketTraversal.trv

// traversals for basket

aspect BasketTraversal {

declare ClassGraph default;

declare ClassGraph myClassGraph : default,

"from Basket to * bypassing {->*,*,java.lang.String }");

declare TraversalGraph tg : myClassGraph, “from A to B”; // tg()

declare Behavior void b1 : tg, Vis; //b1()

declare Behavior Integer summing : myCg, “from A to B”, Vis; //b2()

declare traversal Integer t2(myClassGraph, SumVis) :

"from Basket to Weight";

}

!!!!!!!!!!!!!!! Better “visitor in Java” version

Page 23: For John

BasketMainCount.java// the aspect for counting the total weight of the basket

aspect BasketMainCount {

int Basket.totalWeight() {

Integer retVal = summing();

return retVal.intValue();

}

class SumVis {

int retVal = 0;

void before (Weight w) {retVal += weight.get_i();}

Object returnVal() {return new Integer(retVal);}

}

Page 24: For John

Compilation Process DetailTraversal Files

AspectJ Files

DAJ

StubJavaFiles

AspectJCompiler

CreateClassGraph.java

Class Files

TraversalImplementationJava Files

AspectJCompiler

TraversalImplementationand User Class Files

JVM