Download presentation
Presentation is loading. Please wait.
Published byValentine Preston Modified over 9 years ago
2
Why Should You Care About Aspect-Oriented Programming? Karl Lieberherr CCIS
3
How can you benefit immediately? Use Aspect-Oriented Programming (AOP) for developing your Java programs. What you turn in is all Java. –Debugging Aspects –Testing pre- and post-conditions –Profiling Your programs will be better and you can produce them more easily.
4
What are aspects? (Crista Lopes on History of AOP) Aspects are software concerns that affect what happens in the objects but that are more concise, intelligible and manageable when written as separate paragraphs of the imaginary book that describes the application. This definition of aspects aligns well with what users have been using AOP for. The structural and temporal referencing in AOP are essential mechanisms for achieving the separation between the objects and those other concerns.
5
Cross-cutting of concerns ordinary program structure-shy functionality structure synchronization better program paragraph 1 paragraph 2 paragraph 3 Woven meaning of book
6
Concern An issue that the programmer has to deal with –How do I implement use-case NewPartner –How do I implement the synchronization policy? –How do I describe inputs to my application? –Where to go? –What to do?
7
Example Trip Description Map description. Where to go: walk down the valley, passing pond X, until you get to a big farm house A with a red roof. What to do: –Farm house A: Brunch on the first Sunday of the months June to September. –Pond X: Swimming, fireplace, guided nature path.
8
Development Aspects Tracing, Logging, and Profiling Pre- and Post-Conditions LoD checker!
9
Development Aspects aspect SimpleTracing { pointcut tracedCall(): call(void FigureElement.draw(GraphicsContext)); before(): tracedCall() { System.out.println( "Entering: " + thisJoinPoint); } }
10
Development Aspects When debugging, programmers often invest considerable effort in figuring out a good set of trace points to use when looking for a particular kind of problem. When debugging is complete or appears to be complete it is frustrating to have to lose that investment by deleting trace statements from the code. The alternative of just commenting them out makes the code look bad, and can cause trace statements for one kind of debugging to get confused with trace statements for another kind of debugging.
11
Very Specific Profiling aspect SetsInRotateCounting { int rotateCount = 0; int setCount = 0; before(): call(void Line.rotate(double)) { rotateCount++; } before(): call(void Point.set*(int)) && cflow(call(void Line.rotate(double))) { setCount++; } } Shape: from Line.rotate to Point.set We don’t care what is in between.
12
Pre- and Post- Conditions aspect PointBoundsChecking { pointcut setX(int x): (call(void FigureElement.setXY(int, int)) && args(x, *)) || (call(void Point.setX(int)) && args(x)); pointcut setY(int y): (call(void FigureElement.setXY(int, int)) && args(*, y)) || (call(void Point.setY(int)) && args(y)); before(int x): setX(x) { if ( x MAX_X ) throw new IllegalArgumentException("x is out of bounds."); } before(int y): setY(y) { if ( y MAX_Y ) throw new IllegalArgumentException("y is out of bounds."); } }
13
Class graph: Find undefined things System Body Thing * * * definedThings usedThings definedThings= from System bypassing Body to Thing usedThings = from System through Body to Thing * Definition Fig. UML1 def body Ident S D T B
14
M1: Equation System EquationSystem Equation_List Equation Variable equations * lhs rhs Expression Simple Compound Numerical Expression_List * Add op args Fig. Eq1 Ident
15
CS1: UML class diagram Grammar Grammar EParse BParse Production Entry0..* entries Body Part NonTerm 0..* parts Concrete Abstract lhs rhs Fig. G1
16
Java Program: Adaptive Method with DJ class 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: traversal black bold: structure purple: advice red: parameters repUndef is a modular unit of crosscutting implementation. Ad-hoc implementation may cut across 100 classes.
17
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: Adaptive Method with DJ
18
Pattern Language for AP17 Structure-shy Traversal Consequences –Programs become shorter and more powerful. A paradox. With less work we achieve more. Polya’s inventor paradox. –Program will adapt to many changes in class structure.
19
example – context passing workers need to know the caller: capabilities charge backs to customize result caller1 caller2 Service worker 1worker 3 worker 2
20
context-passing aspects workers need to know the caller: capabilities charge backs to customize result caller1 caller2 Service worker 1worker 3 worker 2
21
context-passing aspects pointcut invocations(Caller c): this(c) && call(void Service.doService(String));
22
context-passing aspects pointcut invocations(Caller c): this(c) && call(void Service.doService(String)); pointcut workPoints(Worker w): target(w) && call(void Worker.doTask(Task));
23
pointcut invocations(Caller c): this(c) && call(void Service.doService(String)); pointcut workPoints(Worker w): target(w) && call(void Worker.doTask(Task)); pointcut perCallerWork(Caller c, Worker w): cflow(invocations(c)) && workPoints(w); context-passing aspects
24
abstract aspect CapabilityChecking { pointcut invocations(Caller c): this(c) && call(void Service.doService(String)); pointcut workPoints(Worker w): target(w) && call(void Worker.doTask(Task)); pointcut perCallerWork(Caller c, Worker w): cflow(invocations(c)) && workPoints(w); before (Caller c, Worker w): perCallerWork(c, w) { w.checkCapabilities(c); } context-passing aspects
25
Conclusions Start using AspectJ for developing Java programs Use AOP style in Java (DJ) Make full use of AspectJ
26
Example Travel book –Maps –Map descriptions –Trip n Map description Where to go What to do
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.