slides for gregor kiczales two versions –short version: crosscutting capabilities for java and...

44
Slides for Gregor Kiczales • Two versions – short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) – long version: Controlling tangling and scattering ...

Upload: silvia-mathews

Post on 03-Jan-2016

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

Slides for Gregor Kiczales

• Two versions– short version: Crosscutting capabilities for Java

and AspectJ through DJ (4 viewgraphs only)– long version: Controlling tangling and

scattering ...

Page 2: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

Crosscutting Capabilities for Java and AspectJ through DJ

Demeter Team

Page 3: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

Class graph: Find undefined things

System

Body

Thing

* *

*definedThings

usedThings

definedThings= from System bypassing Body to ThingusedThings = from System through Body to Thing

*

Definition

Fig. UML1

def

body

Ident

S

D

T

B

Page 4: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

Java Program: Aspectual Method with DJclass System{

String id = “from Thing to edu.neu.ccs.demeter.Ident”;

void repUndef(ClassGraph cg){

checkDefined(cg, getDefThings(cg));}

HashSet getDefThings(ClassGraph cg){

String definedThings =

"from System bypassing Body to Thing";

Visitor v = new Visitor(){

HashSet return_val = new HashSet();

void before(Thing v1){

return_val.add(cg.fetch(v1, id) );}

public Object getReturnValue(){return return_val;}};

cg.traverse(this, definedThings, v);

return (HashSet)v.getReturnValue();

} green: traversalblack bold: structurepurple: advicered: parameters

repUndef is a modular unitof crosscutting implementation. Ad-hoc implementation maycut across 100 classes.

Page 5: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

void checkDefined(ClassGraph cg, final HashSet classHash){

String usedThings =

”from System through Body to Thing";

cg.traverse(this, usedThings, new Visitor(){

void before(Thing v){ Ident vn = cg.fetch(v, vi);

if (!classHash.contains(vn)){

System.out.println("The object "+ vn

+ " is undefined.");

}}});}

}

Java Program: Aspectual Method with DJ

Page 6: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

What DJ adds to AspectJ

• Point cut definitions based on connectivity in class graph.

• Define point cuts by specifying a (traversal) program based on class graph.

• Point cut reduction (high-level point cut designator): free programmer from details of class graph. Free programmer from detail of some aspect.

Page 7: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

Control Tangling and Scattering of Class Structure, Traversals and

Traversal AdviceFeb. PI Meeting

Page 8: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

Aspect-Oriented Programming

• Separating the following cross-cutting concerns: – Object Structure– Traversals through Objects– Advice on Traversals– New behaviors based on collaborating objects

• Focus on those four concerns only. They appear frequently.

Page 9: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

Why crosscutting?

Route1:BusRoute

:BusStopListbusStops

CentralSquare:BusStop

:PersonList

waiting

Paul:Person Seema:Person

:BusListbuses

Bus15:DieselPowered

:PersonList

passengers

Joan:Person

Eric:Personr++; r++;

r=0;

overall graph: object structure; green graph: traversal; purple: advice

Page 10: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

Why aspects: Oblivious

• Object Structure– does not have to know about traversals and

advice on traversals

• Traversals– don’t have to know about advice on traversals

• Advice on Traversals– has to know minimally about object structure

and traversals

Page 11: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

Ad-hoc Implementationof three concerns

• Leads to lots of tangled code with numerous disadvantages

• The question is not how to eliminate the tangling but how to reduce it

• AOP is about tangling control the implementation of crosscutting concerns

• Crosscutting will always lead to some tangling at code level

Page 12: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

Example

• Check whether all used entities are defined.

• Object structure, traversal (basically an introduction), advice on traversal

Page 13: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

Find undefined things

System

Body

Thing

* *

*definedThings

usedThings

definedThings= from System bypassing Body to ThingusedThings = from System through Body to Thing

*

Definition

Page 14: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

Name map

Roles CS1 CS2 M1

System ClassG Grammar Equation-System

Body Body Body Expression

Thing ClassName NonTerm Variable

Definition ClassDef Production Equation

Page 15: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

High-level description

• It is useful to have a high-level description of the collaboration besides the Java source code. Useful documentation.

• Ultimately we are interested in the executable form of the collaboration (Java source code).

Page 16: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

Collaboration with strategiescollaboration checkDef {

role System {

out repUndef(){(uses getDefThings, checkDefined)};

getDefThings(){(uses definedThings)};

checkDefined(){(uses usedThings)};

in definedThings =

from System bypassing Body to Thing;

in usedThings =

from System through Body to Thing; }

role Body { }

role Thing { }

role Definition { }

}

Page 17: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

Use of collaboration: Adapter

Need to provide the expected methods (in methods) and provide name map.• name map:

– System : EquationSystem– Definition : Equation– Body : Expression– Thing : Variable

• expected methods: – in definedThings // use default– in usedThings // use default

Page 18: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

What is an aspect?

• An aspect is a modular unit of crosscutting implementation.

• A Java method is a modular unit.• Can we make a Java method an aspect?• Yes, we call such methods aspectual

methods.• They cut across many classes in an ad-hoc

implementation.

Page 19: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

Java Program: Aspectual Methodclass System{

String id = “from Thing to edu.neu.ccs.demeter.Ident”;

void repUndef(ClassGraph cg){

checkDefined(cg, getDefThings(cg));}

HashSet getDefThings(ClassGraph cg){

String definedThings =

"from System bypassing Body to Thing";

Visitor v = new Visitor(){

HashSet return_val = new HashSet();

void before(Thing v1){

return_val.add(cg.fetch(v1, id) );}

public Object getReturnValue(){return return_val;}};

cg.traverse(this, definedThings, v);

return (HashSet)v.getReturnValue();

} green: traversalblack bold: structurepurple: advicered: parameters

Page 20: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

void checkDefined(ClassGraph cg, final HashSet classHash){

String usedThings =

”from System through Body to Thing";

cg.traverse(this, usedThings, new Visitor(){

void before(Thing v){ Ident vn = cg.fetch(v, id);

if (!classHash.contains(vn)){

System.out.println("The object "+ vn

+ " is undefined.");

}}});}

}

Java Program: Aspectual Method

Page 21: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

After applying name map

• For now we manually edit the Java program.

Page 22: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

Java Program with less tanglingclass EquationSystem{

String id = “from Variable to edu.neu.ccs.demeter.Ident”;

void repUndef(ClassGraph cg){

checkDefined(cg, getDefThings(cg));}

HashSet getDefThings(ClassGraph cg){

String definedThings =

"from EquationSystem bypassing Expression to Variable";

Visitor v = new Visitor(){

HashSet return_val = new HashSet();

void before(Variable v1){

return_val.add(cg.fetch(v1, id) );}

public Object getReturnValue(){return return_val;}};

cg.traverse(this, definedThings, v);

return (HashSet)v.getReturnValue();

}green: traversalblack bold: structurepurple: advicered: parameters

Page 23: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

void checkDefined(ClassGraph cg, final HashSet classHash){

String usedThings =

”from EquationSystem through Expression to Variable";

cg.traverse(this, usedThings, new Visitor(){

void before(Variable v){ Ident vn = cg.fetch(v, id);

if (!classHash.contains(vn)){

System.out.println("The object "+ vn

+ " is undefined.");

}}});}

}

Java Program with less tangling

Page 24: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

Tangling is localizedScattering eliminated

• Instead of having code spread across several classes, it is localized in one class.

• Java program is describing the abstract pattern behind the computation of interest: checking whether used entities are defined.

• Tangling control through abstraction of patterns. We abstract away from structure.

Page 25: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

CS1: UML class diagram ClassG

ClassG

EParse

BParseClassDef

Entry0..*

entries

BodyPart

ClassName0..*

parts

Concrete Abstract

className

definedThings = from ClassG bypassing Body to ClassName

Page 26: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

CS1:UML class diagram ClassG

ClassG

EParse

BParseClassDef

Entry0..*

entries

BodyPart

ClassName0..*

parts

Concrete Abstract

className

usedThings = from ClassG through Body to ClassName

Page 27: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

M1: Equation SystemEquationSystem

Equation_List

EquationVariable

equations

*lhs

rhs

ExpressionSimple

Compound

Numerical

Expression_List

* Addop

args

Fig. Eq1

Ident

Page 28: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

M1: Equation System

definedThings = from EquationSystem bypassing Expression to Variable

EquationSystem

Equation_List

Equation Variable

equations

*lhs

rhs

ExpressionSimple

Compound

Numerical

Expression_List

*Addop

args

Fig. Eq2

Ident

S

D

T

B

Page 29: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

M1: Equation System

usedThings = from EquationSystem through Expression to Variable

EquationSystem

Equation_List

Equation Variable

equations

*lhs

rhs

ExpressionSimple

Compound

Numerical

Expression_List*

Addop

args

Fig. Eq3

Ident

S

D

T

B

Page 30: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

Equation System Objectequations

lhs

rhs

Fig. Eq4

es:EquationSystem els:Equation_List

e1:Equation v1:Variable

i1:Ident

v2:Variable i2:Ident

Page 31: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

M1: Equation System

EquationSystem = <equations> List(Equation).Equation = <lhs> Variable “=“ <rhs> Expression “.”.Variable = Ident.Expression : Simple | Compound.Simple : Variable | Numerical.Compound = “(“ Op <args> List(Expression) “)”.Op : Add | Mul.Add = “+”.Mul = “*”.Numerical = float.

Example:x = 1.0 .y = (+ x 4.0).z = (* x y).

usedThings = from EquationSystem through Expression to Variable

Page 32: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

M1: Equation System

EquationSystem = <equations> List(Equation).Equation = <lhs> Variable “=“ <rhs> Expression “.”.Variable = Ident.Expression : Simple | Compound.Simple : Variable | Numerical.Compound = “(“ Op <args> List(Expression) “)”.Op : Add | Mul.Add = “+”.Mul = “*”.Numerical = float.

Example:x = 1.0 .y = (+ x 4.0).z = (* x y).

definedThings= from EquationSystem bypassing Expression to Variable

Page 33: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

CS1: UML class diagram Grammar

Grammar

EParse

BParseProduction

Entry0..*

entries

BodyPart

NonTerm0..*

parts

Concrete Abstract

lhs

rhs

Fig. G1

Page 34: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

CS1: UML class diagram Grammar

Grammar

EParse

BParseProduction

Entry0..*

entries

BodyPart

NonTerm0..*

parts

Concrete Abstract

lhs

definedThings = from Grammar bypassing Body to NonTerm

rhs

Fig. G2

S

D

T

B

Page 35: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

CS1:UML class diagram Grammar

Grammar

EParse

BParseProduction

Entry0..*

entries

BodyPart

NonTerm0..*

parts

Concrete Abstract

lhs

usedThings = from Grammar through Body to NonTerm

rhs

Fig. G3

S

D

T

B

Page 36: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

DJ

• Is a Java package that supports AOP for the three concerns: class structure, traversals, and traversal advice.

• Tested by 50+ users.

• Connection to AspectJ: both can be used simultaneously.

Page 37: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

Concepts needed(DJ classes)

• ClassGraph

• Strategy

• ObjectGraph

• ObjectGraphSlice

• Visitor

Page 38: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

Adaptive Programming

Strategy

ObjectGraph

defines family of

ClassGraph

is use-case basedabstraction of

Bold namesrefer to DJclasses.

Page 39: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

Adaptive Programming

Strategy

ObjectGraph

defines traversals of

ObjectGraphSlice

plus Strategy defines

Page 40: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

Adaptive Programming

Visitor

Traversal

advises

Page 41: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

AspectJ (Xerox) DJ (NEU)

• Abstract pointcut– set of execution points

– where to watch

• Advice– what to do

• Concrete pointcut– set notation using

regular expressions

• Abstract object slice– set of entry/exit points

– where to go

• Visitor– what to do

• Actual object slice– traversal strategies

Page 42: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

AOP AP

• Program with aspects that correspond to the concerns of the programmer.

• Relieve the programmer from the details of some concern.

• Create robustness to changes in an aspect.

• AP is about point cut reduction.

• Example: structure-shyness

Page 43: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

Technology EvolutionObject-Oriented Programming

Adaptive Programming

Aspect-Oriented Programming

Tangled structure/behaviorsrobustness to structure changes

Tangled concerns in general (synchronization, etc.)

Other Technologies

Aspect-Oriented Programming II

robustness to aspect changes

Page 44: Slides for Gregor Kiczales Two versions –short version: Crosscutting capabilities for Java and AspectJ through DJ (4 viewgraphs only) –long version: Controlling

What DJ adds to AspectJ

• Point cut definitions based on connectivity in class graph.

• Define point cuts by specifying a (traversal) program based on class graph.

• Point cut reduction (high-level point cut designator): free programmer from details of class graph.