Why Should You Care About Aspect

Download Report

Transcript Why Should You Care About Aspect

Why Should You Care About
Aspect-Oriented Programming?
Karl Lieberherr
CCIS
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.
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.
Cross-cutting of concerns
ordinary program
better program
structure-shy
functionality
paragraph 1
paragraph 2
structure
paragraph 3
synchronization
Woven meaning of book
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?
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.
Development Aspects
• Tracing, Logging, and Profiling
• Pre- and Post-Conditions
• LoD checker!
Development Aspects
aspect SimpleTracing {
pointcut tracedCall():
call(void FigureElement.draw(GraphicsContext));
before(): tracedCall() { System.out.println(
"Entering: " + thisJoinPoint); } }
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.
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.
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 < MIN_X || x > MAX_X ) throw
new
IllegalArgumentException("x is out of bounds."); }
before(int y): setY(y) { if ( y < MIN_Y || y > MAX_Y ) throw
new
IllegalArgumentException("y is out of bounds."); }
Fig. UML1
Class graph:
Find undefined things
System
Ident
definedThings
*
usedThings *
S
T
def
*
Definition
body
D
Thing
*
Body
B
definedThings= from System bypassing Body to Thing
usedThings = from System through Body to Thing
Fig. Eq1
M1: Equation System
EquationSystem
equations
Equation_List
Ident
*
Variable
lhs
Equation
Numerical
rhs
Expression
Expression_List
Simple
args
*
op
Compound
Add
Fig. G1
CS1: UML class diagram Grammar
0..*
Entry
EParse
entries
Grammar
BParse
Production
Body
rhs
parts
0..*
Concrete
Abstract
Part
lhs
NonTerm
Java Program: Adaptive Method
with DJ
class System{
String id = “from Thing to edu.neu.ccs.demeter.Ident”;
void repUndef(ClassGraph cg){
repUndef is a
checkDefined(cg, getDefThings(cg));}
HashSet getDefThings(ClassGraph cg){
modular unit
String definedThings =
of crosscutting
"from System bypassing Body to Thing"; implementation.
Visitor v = new Visitor(){
Ad-hoc
HashSet return_val = new HashSet();
implementation may
void before(Thing v1){
cut across
return_val.add(cg.fetch(v1, id) );}
100 classes.
public Object getReturnValue(){return return_val;}};
cg.traverse(this, definedThings, v);
return (HashSet)v.getReturnValue();
green: traversal
}
black bold: structure
purple: advice
red: parameters
Java Program: Adaptive Method
with DJ
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.");
}}});}
}
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.
Pattern Language for AP
17
example – context passing
caller1
Service
caller2
workers need to know the caller:
• capabilities
• charge backs
• to customize result
worker 1
worker 2
worker 3
context-passing aspects
caller1
Service
caller2
workers need to know the caller:
• capabilities
• charge backs
• to customize result
worker 1
worker 2
worker 3
context-passing aspects
pointcut invocations(Caller c):
this(c) && call(void Service.doService(String));
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));
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));
pointcut perCallerWork(Caller c, Worker w):
cflow(invocations(c)) && workPoints(w);
context-passing aspects
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);
}
}
Conclusions
• Start using AspectJ for developing Java
programs
• Use AOP style in Java (DJ)
• Make full use of AspectJ
Example
• Travel book
– Maps
– Map descriptions
– Trip n
• Map description
• Where to go
• What to do