aspect-oriented programming in smalltalk

39
Aspect-Oriented Aspect-Oriented Programming in Programming in Smalltalk Smalltalk Kris Gybels Kris Gybels Robert Hirschfeld Robert Hirschfeld

Upload: britanney-hall

Post on 30-Dec-2015

42 views

Category:

Documents


2 download

DESCRIPTION

Aspect-Oriented Programming in Smalltalk. Kris Gybels Robert Hirschfeld. Outline. Introduction to AOP Using AOP for Smalltalk with ... AspectS Andrew Conclusion. Introduction. Aspect-Oriented Programming. - PowerPoint PPT Presentation

TRANSCRIPT

Aspect-Oriented Programming Aspect-Oriented Programming in Smalltalkin Smalltalk

Kris GybelsKris Gybels

Robert HirschfeldRobert Hirschfeld

OutlineOutline

• Introduction to AOPIntroduction to AOP

• Using AOP for Smalltalk with ...Using AOP for Smalltalk with ...– AspectSAspectS– AndrewAndrew

• ConclusionConclusion

IntroductionIntroduction

Aspect-Oriented ProgrammingAspect-Oriented Programming

Improve quality of softwareImprove quality of softwareby introducing new modularization by introducing new modularization

mechanismsmechanismsto deal with cross-cutting concerns.to deal with cross-cutting concerns.

Software EngineeringSoftware Engineering

• Break problems into relatively Break problems into relatively independent smaller onesindependent smaller ones

• Implement and compose subproblems Implement and compose subproblems as modulesas modules

• What we wantWhat we want– Natural mapping between problems and Natural mapping between problems and

modulesmodules– Localization and separation of concernsLocalization and separation of concerns

Why OOP?Why OOP?

• OOP excellent for modeling entities of OOP excellent for modeling entities of problem domainproblem domain

• Problem domain objects map naturally Problem domain objects map naturally to implementation objectsto implementation objects

• Example: e-Library systemExample: e-Library system– Books -> Book classBooks -> Book class– Library clients -> Client classLibrary clients -> Client class– Printers -> Printer classPrinters -> Printer class

Why AOP?Why AOP?

• ExampleExample– ““Concurrent processing” concern?Concurrent processing” concern?– OO: protect all objects with semaphores ... OO: protect all objects with semaphores ...

• OOP: problems when having to take into OOP: problems when having to take into account special ‘aspects’ that cross-cut a account special ‘aspects’ that cross-cut a systemsystem

• Requires code …Requires code …– In different placesIn different places– Not really related to modeling behavior from the Not really related to modeling behavior from the

problem domainproblem domain

Problems with cross-cutting Problems with cross-cutting concernsconcerns

• Concern implementation intertwined with Concern implementation intertwined with implementation of other concernsimplementation of other concerns

• Concern not localizedConcern not localized• Hurts code ...Hurts code ...

– ReadabilityReadability– EvolvabilityEvolvability– MaintainabilityMaintainability– ......

AOP GoalsAOP Goals

• ““Modularize cross-cutting concerns”Modularize cross-cutting concerns”

• Create language/system to expressCreate language/system to express– Points in a program where aspects Points in a program where aspects

influence the program (joinpoints)influence the program (joinpoints)– How the aspect influences the program How the aspect influences the program

therethere

Types of AOP languagesTypes of AOP languages

• High-levelHigh-level– Special aspect-specific languages to Special aspect-specific languages to

express aspects in (e.g. COOL)express aspects in (e.g. COOL)

“Method setTitle: and setYear: on book “Method setTitle: and setYear: on book objects cannot run concurrently”objects cannot run concurrently”->->coordinator Book {coordinator Book {

mutexclusive {setTitle, setYear}mutexclusive {setTitle, setYear}}}

Types of AOP languagesTypes of AOP languages

• General purposeGeneral purpose– General purpose languages useable for General purpose languages useable for

different aspects (e.g. AspectJ, AspectS, different aspects (e.g. AspectJ, AspectS, Andrew, …)Andrew, …)

“When message setTitle: is sent to a book “When message setTitle: is sent to a book object first do a wait on the semaphore”object first do a wait on the semaphore”

Using AOP for SmalltalkUsing AOP for Smalltalk

AspectS & AndrewAspectS & Andrew

• Both based on the AspectJ general-purpose Both based on the AspectJ general-purpose approach: approach: when ... before/after do ...when ... before/after do ...– Joinpoints: key events in the execution of an OO Joinpoints: key events in the execution of an OO

programprogram• Message sendsMessage sends• Message receptionsMessage receptions• State updates/accessesState updates/accesses

– Aspect’s influence: adviceAspect’s influence: advice• Smalltalk codeSmalltalk code

AspectS vs AndrewAspectS vs Andrew

• AspectSAspectS– Uses Smalltalk to express joinpointsUses Smalltalk to express joinpoints

• AndrewAndrew– Uses special language based on Logic Uses special language based on Logic

Meta Programming to express joinpointsMeta Programming to express joinpoints

Using AOP for SmalltalkUsing AOP for Smalltalk

AspectSAspectS

Aspects in AspectSAspects in AspectS

• Implemented as subclasses of a Implemented as subclasses of a specific class, can have:specific class, can have:– regular variablesregular variables– regular methodsregular methods

– special methods used to implement advicespecial methods used to implement advice

for keeping and acting on state particular to the aspect (e.g. Semaphores)

Writing advice: exampleWriting advice: example

AsMorphicMousingAspect>>adviceMouseEnter^ AsBeforeAfterAdvice

qualifier: (AsAdviceQualifier attributes: { #receiverClassSpecific. })pointcut: [ Morph allSubclasses

select: [:each |each includesSelector: #mouseEnter:]

thenCollect: [:each | AsJoinPointDescriptortargetClass: eachtargetSelector: #mouseEnter:]]

beforeBlock: [:receiver :arguments :aspect :client |self

showHeader: '>>> MouseENTER >>>‘receiver: receiverevent: arguments first]

PointcutsPointcuts

• Blocks which compute a collection of Blocks which compute a collection of JoinpointDescriptorsJoinpointDescriptors

• JoinpointDescriptors indicate method JoinpointDescriptors indicate method execution joinpoints the advice execution joinpoints the advice influences: class + selectorinfluences: class + selector

• The full Smalltalk meta system can be The full Smalltalk meta system can be used!used!

AdviceAdvice

• A Smalltalk block executed before or A Smalltalk block executed before or after every joinpoint matching the after every joinpoint matching the pointcutpointcut

• The block is passed context data of the The block is passed context data of the joinpointjoinpoint– actual object that received the messageactual object that received the message– arguments sent with the messagearguments sent with the message

ToolsTools

• No special support for defining and No special support for defining and editing aspects, just use system editing aspects, just use system browsersbrowsers

• Browser extensions for showing the Browser extensions for showing the impact of aspectsimpact of aspects

ToolsTools

ToolsTools

ToolsTools

Using AOP for SmalltalkUsing AOP for Smalltalk

AndrewAndrew

Aspects in AndrewAspects in Andrew

• Similar to AspectS, implemented as Similar to AspectS, implemented as subclasses of a specific classsubclasses of a specific class– regular variables/methodsregular variables/methods– advicesadvices– logic predicateslogic predicates

• Uses logic meta programming to Uses logic meta programming to express pointcutsexpress pointcuts

Why logic pointcuts?Why logic pointcuts?

• Cross-cutting: compute or describe?Cross-cutting: compute or describe?– Computing allows flexible cross-cutsComputing allows flexible cross-cuts– Cross-cutting is best kept descriptiveCross-cutting is best kept descriptive

• Logic programming combines the two Logic programming combines the two propertiesproperties

Logic Meta ProgrammingLogic Meta Programming

• Meta programming for Smalltalk using Meta programming for Smalltalk using logic language: SOULlogic language: SOUL

• Smalltalk programs represented as logic Smalltalk programs represented as logic factsfacts– class(?c)class(?c)– methodInClass(?c, ?selector, ?m)methodInClass(?c, ?selector, ?m)

• LiCoR extensive library of logic rules to LiCoR extensive library of logic rules to reason about Smalltalk programsreason about Smalltalk programs

Logic cross-cuttingLogic cross-cutting

• Adds predicates to reify joinpoints:Adds predicates to reify joinpoints:– reception(?jp, ?selector, ?args)reception(?jp, ?selector, ?args)– send(?jp, ?selector, ?args)send(?jp, ?selector, ?args)– get(?jp, ?instVar, ?value)get(?jp, ?instVar, ?value)– set(?jp, ?instVar newV, ?, ?oldV)set(?jp, ?instVar newV, ?, ?oldV)

Writing advice ...Writing advice ...

before ?jp matching {before ?jp matching {

reception(?jp, #name)reception(?jp, #name)

} do } do

Transcript show: ‘name accessed Transcript show: ‘name accessed through accessor’through accessor’

Code patterns & cross-cuttingCode patterns & cross-cutting

• LMP successfully used to detect patterns in LMP successfully used to detect patterns in codecode– Smalltalk programming idiomsSmalltalk programming idioms– Design patternsDesign patterns– ......

• Can be used to ...Can be used to ...– Clearly capture the pattern underlying a cross-cutClearly capture the pattern underlying a cross-cut– Specialize joinpoints (“Open weaver”)Specialize joinpoints (“Open weaver”)

Accessor exampleAccessor example

• Use LMP to find accessorsUse LMP to find accessors

isAccessor(?class, ?selector, ?varName) ifisAccessor(?class, ?selector, ?varName) if

class(?class),class(?class),

methodNamed(?class, ?selector, ?method),methodNamed(?class, ?selector, ?method),

statements(?method, ?statements),statements(?method, ?statements),

equals(?statements, <return(variable(?equals(?statements, <return(variable(?varName))>)varName))>)

ObserverObserver

after ?jp matching {after ?jp matching {

reception(?jp, ?selector),reception(?jp, ?selector),

withinClass(?jp, [Person]),withinClass(?jp, [Person]),

notify([Person], ?selector, ?property),notify([Person], ?selector, ?property),

viewInterestedIn(?property)viewInterestedIn(?property)

} do} do

views do: [ :view | view changed: ?property asSymbol ]views do: [ :view | view changed: ?property asSymbol ]

ObserverObserver

notify([Person], [#birthDate:], [#birthDate]).notify([Person], [#birthDate:], [#birthDate]).

notify([Person], [#birthDate:], [#age]).notify([Person], [#birthDate:], [#age]).

notify([Person], [#name:], [#name])notify([Person], [#name:], [#name])

ObserverObserver

viewInterestedIn([#age]).viewInterestedIn([#age]).

viewInterestedIn([#name])viewInterestedIn([#name])

Using AOP for SmalltalkUsing AOP for SmalltalkAspectS vs AndrewAspectS vs Andrew

• Pointcuts more Pointcuts more familiar for a familiar for a Smalltalk Smalltalk programmerprogrammer

• Pointcuts require Pointcuts require one to learn new one to learn new languagelanguage

Implementing AOP for Implementing AOP for SmalltalkSmalltalk

Final notes & ConclusionsFinal notes & Conclusions

ConclusionsConclusions

• AspectS vs AndrewAspectS vs Andrew– AspectS’s pointcuts more familiar to AspectS’s pointcuts more familiar to

Smalltalk programmerSmalltalk programmer– Andrew pointcut language provides Andrew pointcut language provides

enhanced readability + extensive code enhanced readability + extensive code reasoning libraryreasoning library

• AspectS & Andrew vs AspectJAspectS & Andrew vs AspectJ– Use of full MOP allows one to find more Use of full MOP allows one to find more

code patterns to cross-cutcode patterns to cross-cut

LinksLinks

• prog.vub.ac.be/~kgybels/andrew/prog.vub.ac.be/~kgybels/andrew/• www.prakinf.tu-ilmenau.de/~hirsch/www.prakinf.tu-ilmenau.de/~hirsch/

Projects/Squeak/AspectSProjects/Squeak/AspectS• VisualWorks 7 distroVisualWorks 7 distro• www.aosd.netwww.aosd.net

[email protected]@vub.ac.be• [email protected]@acm.org