Aspect-Oriented ProgrammingProgramming paradigmsThe problemExampleConsequences of crosscutting codeAspectJTMTerminologyThe Figure Element exampleExample IJoin pointsPointcutsExample pointcut designators IExample pointcut designators IIPointcut designator wildcardsPointcut designators based on typesPointcut designator compositionPointcut designators based on modifiersExample I, repeatedKinds of adviceExample II, with parametersIntroductionExample introductionApproximate syntaxExample aspect IExample aspect IIConcluding remarksThe EndJan 14, 2019Aspect-Oriented ProgrammingDave MatuszekUniversity of Pennsylvania2Programming paradigmsProcedural programmingExecuting a set of commands in a given sequenceFortran, C, CobolFunctional programmingEvaluating a function defined in terms of other functionsLisp, ML, OCamlLogic programmingProving a theorem by finding values for the free variablesPrologObject-oriented programming (OOP)Organizing a set of objects, each with its own set of responsibilitiesSmalltalk, Java, C++ (to some extent)Aspect-oriented programming (AOP)Executing code whenever a program shows certain behaviorsAspectJ (a Java extension)Does not replace O-O programming, but rather complements it3The problemSome programming tasks cannot be neatly encapsulated in objects, but must be scattered throughout the codeExamples:Logging (tracking program behavior to a file)Profiling (determining where a program spends its time)Tracing (determining what methods are called when)Session tracking, session expirationSpecial security managementThe result is crosscuting code--the necessary code “cuts across” many different classes and methods4Exampleclass Fraction { int numerator; int denominator; ... public Fraction multiply(Fraction that) { traceEnter("multiply", new Object[] {that}); Fraction result = new Fraction( this.numerator * that.numerator, this.denominator * that.denominator); result = result.reduceToLowestTerms(); traceExit("multiply", result); return result; } ...}Now imagine similar code in every method you might want to trace5Consequences of crosscutting codeRedundant codeSame fragment of code in many placesDifficult to reason aboutNon-explicit structureThe big picture of the tangling isn’t clearDifficult to changeHave to find all the code involved......and be sure to change it consistently...and be sure not to break it by accidentInefficient when crosscuting code is not neededAdapted from:www.parc.xerox.com/research/csl/projects/ aspectj/downloads/PARC-Workshop-2002.ppt6AspectJTMAspectJ is a small, well-integrated extension to JavaBased on the 1997 PhD thesis by Christina Lopes, A Language Framework for Distributed ProgrammingAspectJ modularizes crosscutting concernsThat is, code for one aspect of the program (such as tracing) is collected together in one placeThe AspectJ compiler is free and open sourceAspectJ works with JBuilder, Forté, Eclipse, probably othersBest online writeup: http://www.eclipse.org/aspectj/Parts of this lecture were taken from the above paper7TerminologyA join point is a well-defined point in the program flowA pointcut is a group of join pointsAdvice is code that is executed at a pointcutIntroduction modifies the members of a class and the relationships between classesAn aspect is a module for handling crosscutting concernsAspects are defined in terms of pointcuts, advice, and introductionAspects are reusable and inheritableEach of these terms will be discussed in greater detail8The Figure Element example9Example IA pointcut named move that chooses various method calls:pointcut move(): call(void FigureElement.setXY(int,int)) || call(void Point.setX(int)) || call(void Point.setY(int)) || call(void Line.setP1(Point)) || call(void Line.setP2(Point));Advice (code) that runs before the move pointcut:before(): move() { System.out.println("About to move");}Advice that runs after the move pointcut:after(): move() { System.out.println("Just successfully moved");}10Join pointsA join point is a well-defined point in the program flowWe want to execute some code (“advice”) each time a join point is reachedWe do not want to clutter up the code with explicit indicators saying “This is a join point”AspectJ provides a syntax for indicating these join points “from outside” the actual codeA join point is a point in the program flow “where something happens”Examples:When a method is calledWhen an exception is thrownWhen a variable is accessed11PointcutsPointcut definitions consist of a left-hand side and a right-hand side, separated by a colonThe left-hand side consists of the pointcut name and the pointcut parameters (i.e. the data available when the events happen)The right-hand side consists of the pointcut itselfExample pointcut:pointcut setter(): call(void setX(int));The name of this pointcut is setterThe pointcut has no parametersThe pointcut itself is call(void setX(int))The pointcut refers to any time the void setX(int) method is called12Example pointcut designators IWhen a particular method body executes:execution(void Point.setX(int)) When a method is called:call(void Point.setX(int)) When an exception handler executes:handler(ArrayOutOfBoundsException) When the object currently executing (i.e. this) is of type SomeType: this(SomeType)13Example pointcut designators IIWhen the target object is of type SomeType target(SomeType) When the executing code belongs to class MyClass within(MyClass) When the join point is in the control flow of a call to a Test's no-argument main methodcflow(call(void Test.main()))14Pointcut designator wildcardsIt is possible to use wildcards to declare pointcuts:execution(* *(..)) Chooses the execution of any method regardless of return or parameter types call(* set(..)) Chooses the call to any method named set regardless of return or parameter typeIn case of overloading there may be more than one such set method; this pointcut picks out calls to all of them15Pointcut designators based on typesYou can select elements based on types. For example,execution(int *()) Chooses the execution of any method with no
View Full Document