SlideShare a Scribd company logo
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?

More Related Content

What's hot (16)

PDF
A novel methodology for test scenario generation based on control flow analys...
eSAT Publishing House
 
PPTX
Activity sequence -class and swimlane
Hassnat Ahmed
 
PPT
Chapter 2 - Basic Elements of Java
Adan Hubahib
 
PPTX
fUML-Driven Design and Performance Analysis of Software Agents for Wireless S...
Luca Berardinelli
 
PPTX
Coding and testing In Software Engineering
Satya Bhushan Verma
 
PDF
Unit 1-problem solving with algorithm
rajkumar1631010038
 
PPT
9781111530532 ppt ch02
Terry Yoast
 
PDF
Lecture 3 cst205 cst281-oop
ktuonlinenotes
 
PDF
Design and analysis of computer algorithms
Krishna Chaytaniah
 
PPT
9781111530532 ppt ch03
Terry Yoast
 
PPT
Chapter 13 - Recursion
Adan Hubahib
 
PPT
Test design techniques
Pragya Rastogi
 
PDF
Verification challenges and methodologies - SoC and ASICs
Dr. Shivananda Koteshwar
 
PPTX
"A short and knowledgeable concept about Algorithm "
CHANDAN KUMAR
 
PPT
9781111530532 ppt ch04
Terry Yoast
 
PPTX
The ipo model
jeanrummy
 
A novel methodology for test scenario generation based on control flow analys...
eSAT Publishing House
 
Activity sequence -class and swimlane
Hassnat Ahmed
 
Chapter 2 - Basic Elements of Java
Adan Hubahib
 
fUML-Driven Design and Performance Analysis of Software Agents for Wireless S...
Luca Berardinelli
 
Coding and testing In Software Engineering
Satya Bhushan Verma
 
Unit 1-problem solving with algorithm
rajkumar1631010038
 
9781111530532 ppt ch02
Terry Yoast
 
Lecture 3 cst205 cst281-oop
ktuonlinenotes
 
Design and analysis of computer algorithms
Krishna Chaytaniah
 
9781111530532 ppt ch03
Terry Yoast
 
Chapter 13 - Recursion
Adan Hubahib
 
Test design techniques
Pragya Rastogi
 
Verification challenges and methodologies - SoC and ASICs
Dr. Shivananda Koteshwar
 
"A short and knowledgeable concept about Algorithm "
CHANDAN KUMAR
 
9781111530532 ppt ch04
Terry Yoast
 
The ipo model
jeanrummy
 

Similar to Aspect Oriented Programming (20)

PPTX
Aspect Oriented Programming
Rajesh Ganesan
 
PDF
Aspect-oriented programming with AspectJ (as part of the the PTT lecture)
Ralf Laemmel
 
PPT
45 aop-programming
daotuan85
 
PPT
Aop2007
Tuhin_Das
 
PPTX
Spring AOP in Nutshell
Onkar Deshpande
 
PPTX
Aspect Oriented Programming
Shreya Chatterjee
 
ODP
Aspect-Oriented Technologies
Esteban Abait
 
PDF
SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ
Ted Leung
 
PDF
AOP
Joshua Yoon
 
PPTX
Introduction to Aspect Oriented Programming
Amir Kost
 
PPTX
AOP on Android
Tibor Srámek
 
PPTX
Aspect Oriented Programming
Rodger Oates
 
PPTX
Spring aop
sanskriti agarwal
 
PDF
Spring aop
Hamid Ghorbani
 
PPTX
Spring AOP
Radhakrishna Mutthoju
 
PPT
ASPECT ORIENTED PROGRAMING(aop)
kvsrteja
 
PDF
AOP (Aspect-Oriented Programming) spring boot
PLAYAFIFI
 
PPT
Aspect Oriented Software Development
Jignesh Patel
 
PPTX
UML for Aspect Oriented Design
Edison Lascano
 
Aspect Oriented Programming
Rajesh Ganesan
 
Aspect-oriented programming with AspectJ (as part of the the PTT lecture)
Ralf Laemmel
 
45 aop-programming
daotuan85
 
Aop2007
Tuhin_Das
 
Spring AOP in Nutshell
Onkar Deshpande
 
Aspect Oriented Programming
Shreya Chatterjee
 
Aspect-Oriented Technologies
Esteban Abait
 
SeaJUG Dec 2001: Aspect-Oriented Programming with AspectJ
Ted Leung
 
Introduction to Aspect Oriented Programming
Amir Kost
 
AOP on Android
Tibor Srámek
 
Aspect Oriented Programming
Rodger Oates
 
Spring aop
sanskriti agarwal
 
Spring aop
Hamid Ghorbani
 
ASPECT ORIENTED PROGRAMING(aop)
kvsrteja
 
AOP (Aspect-Oriented Programming) spring boot
PLAYAFIFI
 
Aspect Oriented Software Development
Jignesh Patel
 
UML for Aspect Oriented Design
Edison Lascano
 
Ad

Recently uploaded (20)

PDF
Market Insight : ETH Dominance Returns
CIFDAQ
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PDF
NewMind AI Weekly Chronicles – July’25, Week III
NewMind AI
 
PPTX
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PPTX
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
PDF
Generative AI vs Predictive AI-The Ultimate Comparison Guide
Lily Clark
 
PDF
RAT Builders - How to Catch Them All [DeepSec 2024]
malmoeb
 
PDF
The Future of Artificial Intelligence (AI)
Mukul
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PDF
Researching The Best Chat SDK Providers in 2025
Ray Fields
 
PPTX
AI Code Generation Risks (Ramkumar Dilli, CIO, Myridius)
Priyanka Aash
 
PPTX
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
PDF
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
PPTX
Agile Chennai 18-19 July 2025 | Workshop - Enhancing Agile Collaboration with...
AgileNetwork
 
PDF
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
Market Insight : ETH Dominance Returns
CIFDAQ
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
NewMind AI Weekly Chronicles – July’25, Week III
NewMind AI
 
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
Generative AI vs Predictive AI-The Ultimate Comparison Guide
Lily Clark
 
RAT Builders - How to Catch Them All [DeepSec 2024]
malmoeb
 
The Future of Artificial Intelligence (AI)
Mukul
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
Researching The Best Chat SDK Providers in 2025
Ray Fields
 
AI Code Generation Risks (Ramkumar Dilli, CIO, Myridius)
Priyanka Aash
 
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
Agile Chennai 18-19 July 2025 | Workshop - Enhancing Agile Collaboration with...
AgileNetwork
 
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
Ad

Aspect Oriented Programming

  • 1. Aspect oriented programming IoT Team Hortolândia, 07/10/2018 AOP
  • 2. 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
  • 3. 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
  • 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) 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
  • 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 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
  • 9. 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()))
  • 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 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
  • 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 – 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.
  • 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