Aspect oriented programming
IoT Team
Hortolândia, 07/10/2018
AOP
The problem: Cross-cutting concerns
 In aspect-oriented software development, cross-cutting concerns are aspects of a program that affect
other concerns. These concerns often cannot be cleanly decomposed from the rest of the system in both
the design and implementation, and can result in either scattering (code duplication), tangling (significant
dependencies between systems), or both.
https://en.wikipedia.org/wiki/Cross-cutting_concern
Examples of concerns that tend to be cross-cutting
 Examples:
– 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 expiration
– Special security management
 The result is crosscutting code--the necessary code “cuts across” many different classes
and methods
Consequences
 Redundant code
– Same fragment of code in many places
 Difficult to reason about
– Non-explicit structure
– The big picture of the tangling isn’t clear
 Difficult to change
– Have to find all the code involved...
– ...and be sure to change it consistently
– ...and be sure not to break it by accident
 Inefficient when crosscutting code is not needed
The solution: AOP
 In computing, aspect-oriented programming (AOP) is a programming paradigm that aims
to increase modularity by allowing the separation of cross-cutting concerns. It does so by
adding additional behavior to existing code (an advice) without modifying the code itself,
instead separately specifying which code is modified via a "pointcut" specification, such as
"log all function calls when the function's name begins with 'set'". This allows behaviors that
are not central to the business logic (such as logging) to be added to a program without
cluttering the code, core to the functionality. AOP forms a basis for aspect-oriented software
development.
»https://en.wikipedia.org/wiki/Aspect-oriented_programming
AspectJ
 AspectJ(tm) is a simple and practical extension to the Java(tm) programming language that
adds to Java aspect-oriented programming (AOP) capabilities. AOP allows developers to
reap the benefits of modularity for concerns that cut across the natural units of modularity.
In object-oriented programs like Java, the natural unit of modularity is the class. In AspectJ,
aspects modularize concerns that affect more than one class.
» https://www.eclipse.org/aspectj/doc/released/faq.php#q:whatisaj
AspectJ - Terminology
 A join point is a well-defined point in the program flow
 A pointcut is a group of join points
 Advice is code that is executed at a pointcut
 Introduction modifies the members of a class and the relationships between classes
 An aspect is a module for handling crosscutting concerns
– Aspects are defined in terms of pointcuts, advice, and introduction
– Aspects are reusable and inheritable
Pointcut
 Pointcut definitions consist of a left-hand side and a right-hand side, separated by a colon
– The 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 itself
 Example pointcut:
pointcut setter(): call(void setX(int));
– The name of this pointcut is setter
– The pointcut has no parameters
– The pointcut itself is call(void setX(int))
– The pointcut refers to any time the void setX(int) method is called
Pointcut – more examples
 When 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)
 When 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 method
– cflow(call(void Test.main()))
Pointcut – wildcards
 It 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 type
 In case of overloading there may be more than one such set method; this pointcut picks out calls to all of them
 You can select elements based on types. For example,
– execution(int *())
 Chooses the execution of any method with no parameters that returns an int
– call(* setY(long))
 Chooses the call to any setY method that takes a long as an argument, regardless of return type or declaring type
– call(* Point.setY(int))
 Chooses the call to any of Point’s setY methods that take an int as an argument, regardless of return type
– call(*.new(int, int))
 Chooses the call to any classes’ constructor, so long as it takes exactly two ints as arguments
Advices
 Action taken by an aspect at a particular join point
 AspectJ has several kinds of advice; here are some of them:
– Before advice runs as a join point is reached, before the program proceeds with the join
point
– After advice on a particular join point runs after the program proceeds with that join point
 after returning advice is executed after a method returns normally
 after throwing advice is executed after a method returns by throwing an exception
 after advice is executed after a method returns, regardless of whether it returns
normally or by throwing an exception
– Around advice on a join point runs as the join point is reached, and has explicit control
over whether the program proceeds with the join point
Advices - examples
 You can access the context of the join point:
 pointcut setXY(FigureElement fe, int x, int y):
call(void FigureElement.setXY(int, int))
&& target(fe)
&& args(x, y);
 after(FigureElement fe, int x, int y) returning: setXY(fe, x, y) {
System.out.println(fe + " moved to (" + x + ", " + y + ").");
}
Advices – capturing context
– AspectJ provides a special reference variable, thisJoinPoint, that contains reflective
information about the current join point for the advice to use.
– The thisJoinPoint variable can only be used in the context of advice, just like this can
only be used in the context of non-static methods and variable initializers.
– In advice, thisJoinPoint is an object of type org.aspectj.lang.JoinPoint.
Weaving
 Compile-time weaving: The AspectJ compiler takes as input both the source code of our
aspect and our application and produces a woven class files as output
 Post-compile weaving: This is also known as binary weaving. It is used to weave existing
class files and JAR files with our aspects
 Load-time weaving: This is exactly like the former binary weaving, with a difference that
weaving is postponed until a class loader loads the class files to the JVM
Demo
 Tic-tac-toe application
Thank you!!
Questions?

Aspect Oriented Programming

  • 1.
    Aspect oriented programming IoTTeam Hortolândia, 07/10/2018 AOP
  • 2.
    The problem: Cross-cuttingconcerns  In aspect-oriented software development, cross-cutting concerns are aspects of a program that affect other concerns. These concerns often cannot be cleanly decomposed from the rest of the system in both the design and implementation, and can result in either scattering (code duplication), tangling (significant dependencies between systems), or both. https://en.wikipedia.org/wiki/Cross-cutting_concern
  • 3.
    Examples of concernsthat tend to be cross-cutting  Examples: – 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 expiration – Special security management  The result is crosscutting code--the necessary code “cuts across” many different classes and methods
  • 4.
    Consequences  Redundant code –Same fragment of code in many places  Difficult to reason about – Non-explicit structure – The big picture of the tangling isn’t clear  Difficult to change – Have to find all the code involved... – ...and be sure to change it consistently – ...and be sure not to break it by accident  Inefficient when crosscutting code is not needed
  • 5.
    The solution: AOP In computing, aspect-oriented programming (AOP) is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. It does so by adding additional behavior to existing code (an advice) without modifying the code itself, instead separately specifying which code is modified via a "pointcut" specification, such as "log all function calls when the function's name begins with 'set'". This allows behaviors that are not central to the business logic (such as logging) to be added to a program without cluttering the code, core to the functionality. AOP forms a basis for aspect-oriented software development. »https://en.wikipedia.org/wiki/Aspect-oriented_programming
  • 6.
    AspectJ  AspectJ(tm) isa simple and practical extension to the Java(tm) programming language that adds to Java aspect-oriented programming (AOP) capabilities. AOP allows developers to reap the benefits of modularity for concerns that cut across the natural units of modularity. In object-oriented programs like Java, the natural unit of modularity is the class. In AspectJ, aspects modularize concerns that affect more than one class. » https://www.eclipse.org/aspectj/doc/released/faq.php#q:whatisaj
  • 7.
    AspectJ - Terminology A join point is a well-defined point in the program flow  A pointcut is a group of join points  Advice is code that is executed at a pointcut  Introduction modifies the members of a class and the relationships between classes  An aspect is a module for handling crosscutting concerns – Aspects are defined in terms of pointcuts, advice, and introduction – Aspects are reusable and inheritable
  • 8.
    Pointcut  Pointcut definitionsconsist of a left-hand side and a right-hand side, separated by a colon – The 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 itself  Example pointcut: pointcut setter(): call(void setX(int)); – The name of this pointcut is setter – The pointcut has no parameters – The pointcut itself is call(void setX(int)) – The pointcut refers to any time the void setX(int) method is called
  • 9.
    Pointcut – moreexamples  When 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)  When 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 method – cflow(call(void Test.main()))
  • 10.
    Pointcut – wildcards It 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 type  In case of overloading there may be more than one such set method; this pointcut picks out calls to all of them  You can select elements based on types. For example, – execution(int *())  Chooses the execution of any method with no parameters that returns an int – call(* setY(long))  Chooses the call to any setY method that takes a long as an argument, regardless of return type or declaring type – call(* Point.setY(int))  Chooses the call to any of Point’s setY methods that take an int as an argument, regardless of return type – call(*.new(int, int))  Chooses the call to any classes’ constructor, so long as it takes exactly two ints as arguments
  • 11.
    Advices  Action takenby an aspect at a particular join point  AspectJ has several kinds of advice; here are some of them: – Before advice runs as a join point is reached, before the program proceeds with the join point – After advice on a particular join point runs after the program proceeds with that join point  after returning advice is executed after a method returns normally  after throwing advice is executed after a method returns by throwing an exception  after advice is executed after a method returns, regardless of whether it returns normally or by throwing an exception – Around advice on a join point runs as the join point is reached, and has explicit control over whether the program proceeds with the join point
  • 12.
    Advices - examples You can access the context of the join point:  pointcut setXY(FigureElement fe, int x, int y): call(void FigureElement.setXY(int, int)) && target(fe) && args(x, y);  after(FigureElement fe, int x, int y) returning: setXY(fe, x, y) { System.out.println(fe + " moved to (" + x + ", " + y + ")."); }
  • 13.
    Advices – capturingcontext – AspectJ provides a special reference variable, thisJoinPoint, that contains reflective information about the current join point for the advice to use. – The thisJoinPoint variable can only be used in the context of advice, just like this can only be used in the context of non-static methods and variable initializers. – In advice, thisJoinPoint is an object of type org.aspectj.lang.JoinPoint.
  • 14.
    Weaving  Compile-time weaving:The AspectJ compiler takes as input both the source code of our aspect and our application and produces a woven class files as output  Post-compile weaving: This is also known as binary weaving. It is used to weave existing class files and JAR files with our aspects  Load-time weaving: This is exactly like the former binary weaving, with a difference that weaving is postponed until a class loader loads the class files to the JVM
  • 15.
  • 16.
  • 17.