SlideShare uma empresa Scribd logo
1 de 48
Baixar para ler offline
Aspect Oriented Programming:
Hidden Toolkit That You Already Have
Dmitry Vinnik
Senior Software Engineer
dvinnik@salesforce.com
JEEConf 2017
Talk Outline
● AOP Overview
● Anatomy of AspectJ
● Spring AOP
● Java Agents Overview
● AOP Implementations
Talk Motivation
Everyday challenges: profiling, code coverage, HotSwap, and monitoring.
Aspect Oriented Programing (AOP) is a solution to all these problems.
AOP is a Hidden Toolkit that everyone knows, but very few use.
This talk aims to change that.
Aspect Oriented Programming
Overview
Aspect Oriented Programming (AOP)
Overview
AOP is Cross-Cutting Concerns paradigm
Answers Questions:
● What is my code coverage?
● Is my JVM even running?
● Ahh, do I have to restart my app again?
● How long does this process take?
Hidden Toolkit: AOP Simple Example
1: 1
2: 1
...
39: 63245986
40: 102334155
Process: 1837 ms
Code Sample: Output:
Hidden Toolkit: AspectJ
Overview
Hidden Toolkit: AspectJ Overview
Defining Aspect
1) Definition
3) Advice
2)
Pointc
ut
Hidden Toolkit: AspectJ
Pointcuts Overview
Hidden Toolkit: AspectJ Pointcuts
Anatomy of Pointcut
1) Definition
2)
Ty
pe
3)
Expressi
on
Hidden Toolkit: AspectJ Pointcuts - Continue
Methods/Constructors:
1) call(Signature)
2) execution(Signature)
Fields (specific to AspectJ):
1) get(Signature)
2) set(Signature)
Exceptions Handler:
1) handler(TypePattern)
Types of Pointcuts
Hidden Toolkit: AspectJ Pointcuts - Continue
1) Signature
1) TypePattern
Pointcut Expressions
3) Pointcut/Combination
Hidden Toolkit: AspectJ
Advices Overview
Hidden Toolkit: AspectJ Advice
Anatomy of AspectJ Advice
1) Advice Type
2) Pointcut
Binding
3) Main
Logic
Hidden Toolkit: AspectJ Advice - Continue
Types of Advices
1) Before
1) After
1) Around
Hidden Toolkit: AspectJ Advice - Continue
1. Single Pointcut
2. Joint Pointcuts
3. Inline Pointcut
4. Parameterized Pointcut
Pointcut Binding
Hidden Toolkit: AspectJ
Additional Information
Hidden Toolkit: AspectJ - Advanced
1) Generics
2) Abstract Aspects
3) Declare Parents
4) Declare Soft
Generics and Declarations
Hidden Toolkit: AspectJ Execution
1) Switching to ‘ajc’ compiler
Aspect Process started
1: 1
2: 1
...
39: 63245986
40: 102334155
Aspect Process took: 1845 ms
2) Run function bound by the Aspect
3) Output:
Hidden Toolkit: AspectJ
Annotations Development Style
Hidden Toolkit: AspectJ - Annotations Usage
1) Aspect Initialization
1) Pointcut Definition
Annotations Development Style - Aspect Core
Hidden Toolkit: AspectJ - Annotations Usage
1) Before
1) After
1) Around
Annotations Development Style - Advices
Hidden Toolkit: Spring AOP
Spring AOP Overview
Hidden Toolkit: Spring AOP - Configuration
@Configuration and XML
XML@Configuration
Hidden Toolkit: Spring AOP - AspectJ Annotation
Native AspectJ Support:
1. Aspect
2. Pointcut
3. Before
4. After
5. Around
Spring AspectJ Support
Hidden Toolkit: Spring AOP - Schema-Based Aspects
XML Aspects:
1. Aspect
2. Pointcut
3. Before
4. After
5. Around
Spring XML-based Aspects
Hidden Toolkit: Spring AOP - AspectJ vs. Schema
AspectJ Annotations:
● (+) DRY by-design
● (+) Pointcut combination
● (+) Reusable outside of Spring
● (-) Requires >Java 5 support
Spring XML:
● (+) Best for Enterprise services
● (+) Summary of enabled aspects
● (-) No separation of concerns
● (-) No Pointcut combinations
Comparing Two Approaches
Hidden Toolkit: Spring AOP - Limitations
● No Field-level interception allowed (i.e. #set(), #get())
● Limited support for objects not managed by Spring
● No support for certain general pointcuts (ex. handler, call)
Limitation of Spring In Comparison With Native AspectJ
Hidden Toolkit:
Anatomy of Java Agents
Overview
Hidden Toolkit: Anatomy of Java Agents
Building Java Agents
1) Premain Java Agent class
2) Implement ClassFileTransformer (next slide)
3) Compile java agent and run as:
java -javaagent:simple-agent.jar -jar test-app.jar
Hidden Toolkit: Building Java Agents
Defining Class Transformer
AOP Implementations
Overview
AOP Implementations
Main Types:
1. Code Coverage: JaCoCo, Clover
2. Benchmarks/Profilers: JMH, AppDynamics
3. Improved Compilation: HotswapAgent, JRebel
4. Application Monitoring: AppDynamics
Main Types of AOP Implementations
AOP Implementations
Code Coverage
AOP Implementation: Code Coverage
JaCoCo[1]
Report:
Class Info:
AOP Implementation: Code Coverage - Continue
● No setup required (i.e. Java Agent)
● Produces detailed reports of Code Coverage
● Highlights coverage down to code lines
● Instruments Running/Pre-Execution code
Why AOP with JaCoCo?
AOP Implementation: Code Coverage - Continue
● Classes are instrumented on the fly by Java Agent
a. Performs in-memory pre-processing of all files
b. Instruments using java.lang.instrument.Instrumentation
● Collects coverage data into .exec files
● Process collected data into .html reports
How It Actually Works
AOP Implementations
Benchmarks
AOP Implementation: Benchmarks
# Benchmark mode: Throughput, ops/time
# Benchmark:
org.sample.MyBenchmark.testMethod
# Run progress: 0.00% complete, ETA
00:06:40
# Fork: 1 of 10
# Warmup Iteration 1: 0.414 ops/s
# Warmup Iteration 2: 0.416 ops/s
# Warmup Iteration 3: 0.417 ops/s
Java Microbenchmark Harness, JMH[1]
Code Sample: Output:
AOP Implementation: Benchmark - Continue
● Reliable measure of individual units (ex. microservices)
● Supported by Oracle
● Highly customizable:
• State
• Time Unit
• Mode
Why AOP with JMH?
AOP Implementations
Improved Compilation
AOP Implementation: Improved Compilation
Default Java Hotswap handles re-compilation of java files.
Does Not Handle:
● Renaming/Addition/Deletion of Methods
● Changes to Static/Non-Static Fields
● Changes to Enum Classes
● Changes to Class Hierarchy (interfaces/superclasses)
Java Hotswap
AOP Implementation: Improved Compilation
● Java Agent that handles imperfections of Java Hotswap.
● Can be run as a javaagent on a start of JVM:
• java -XXaltjvm=dcevm -javaagent:hotswap-agent.jar test-app.jar
● Can be attached to running JVM via com.sun.tools.attach.VirtualMachine calls
● Currently in beta version
Note: Java agents like HotswapAgent do not directly change main binaries, but
rather modify proxied copy of the binaries.
HotswapAgent[1]
AOP Implementation: Improved Compilation
● No setup required (i.e. Java Agent)
● Productivity Booster (especially in enterprise)
● More Control over JVM
● Verbose Logging
Why AOP with HotswapAgent?
AOP Implementations
Other Solutions
AOP Implementations: Other Solutions
● Atlassian Clover (Code Coverage)
● JCov (Code Coverage)
● AppDynamics (Benchmark, Monitoring)
● Statsd-JVM (Profiler)
● JRebel (Improved Compilation)
Q/A
About Speaker
Twitter: @DmitryVinnik
LinkedIn: in/dmitry-vinnik/
Email: dvinnik@salesforce.com
Dmitry Vinnik

Mais conteúdo relacionado

Mais de Dmitry Vinnik

The 10,000 Steps of Open Source Project Health
The 10,000 Steps of Open Source Project HealthThe 10,000 Steps of Open Source Project Health
The 10,000 Steps of Open Source Project HealthDmitry Vinnik
 
Better Start: Enforcing Best Engineering Practices with Kotlin
Better Start: Enforcing Best Engineering Practices with KotlinBetter Start: Enforcing Best Engineering Practices with Kotlin
Better Start: Enforcing Best Engineering Practices with KotlinDmitry Vinnik
 
Testing Svelte with Jest: Validate Your Components Quickly!
Testing Svelte with Jest: Validate Your Components Quickly!Testing Svelte with Jest: Validate Your Components Quickly!
Testing Svelte with Jest: Validate Your Components Quickly!Dmitry Vinnik
 
Developing Lightning Components for Communities.pptx
Developing Lightning Components for Communities.pptxDeveloping Lightning Components for Communities.pptx
Developing Lightning Components for Communities.pptxDmitry Vinnik
 
Hands on React Native: From Zero to Hero
Hands on React  Native:  From Zero to HeroHands on React  Native:  From Zero to Hero
Hands on React Native: From Zero to HeroDmitry Vinnik
 
Remote Work: Gateway to Freedom
Remote Work: Gateway to FreedomRemote Work: Gateway to Freedom
Remote Work: Gateway to FreedomDmitry Vinnik
 
Kindness Engineering: Focusing on What Matters
Kindness Engineering: Focusing on What MattersKindness Engineering: Focusing on What Matters
Kindness Engineering: Focusing on What MattersDmitry Vinnik
 
Gauge + Taiko, BDD for Web Revived
Gauge + Taiko, BDD for Web RevivedGauge + Taiko, BDD for Web Revived
Gauge + Taiko, BDD for Web RevivedDmitry Vinnik
 
Modern Web Testing: Going Beyond Selenium
Modern Web Testing: Going Beyond SeleniumModern Web Testing: Going Beyond Selenium
Modern Web Testing: Going Beyond SeleniumDmitry Vinnik
 
Do you even Function? Guiding Through Functional Interfaces
Do you even Function? Guiding Through Functional InterfacesDo you even Function? Guiding Through Functional Interfaces
Do you even Function? Guiding Through Functional InterfacesDmitry Vinnik
 
From Robotium to Appium: Choose your Journey
From Robotium to Appium: Choose your Journey From Robotium to Appium: Choose your Journey
From Robotium to Appium: Choose your Journey Dmitry Vinnik
 
Stress Driven Development, and How to Avoid It
Stress Driven Development, and How to Avoid ItStress Driven Development, and How to Avoid It
Stress Driven Development, and How to Avoid ItDmitry Vinnik
 
Uphill Battle of Mobile Visual Regression
Uphill Battle of Mobile Visual RegressionUphill Battle of Mobile Visual Regression
Uphill Battle of Mobile Visual RegressionDmitry Vinnik
 
Engineer in Test: Bridging the Gap
Engineer in Test: Bridging the GapEngineer in Test: Bridging the Gap
Engineer in Test: Bridging the GapDmitry Vinnik
 
Domain Driven Testing: Know What You’re Doing
Domain Driven Testing: Know What You’re DoingDomain Driven Testing: Know What You’re Doing
Domain Driven Testing: Know What You’re DoingDmitry Vinnik
 
Back to the CompletableFuture: Concurrency in Action
Back to the CompletableFuture: Concurrency in ActionBack to the CompletableFuture: Concurrency in Action
Back to the CompletableFuture: Concurrency in ActionDmitry Vinnik
 
Modern Web Testing: Going Beyond Selenium
Modern Web Testing: Going Beyond Selenium Modern Web Testing: Going Beyond Selenium
Modern Web Testing: Going Beyond Selenium Dmitry Vinnik
 
Build Tests to Build Websites
Build Tests to Build WebsitesBuild Tests to Build Websites
Build Tests to Build WebsitesDmitry Vinnik
 

Mais de Dmitry Vinnik (18)

The 10,000 Steps of Open Source Project Health
The 10,000 Steps of Open Source Project HealthThe 10,000 Steps of Open Source Project Health
The 10,000 Steps of Open Source Project Health
 
Better Start: Enforcing Best Engineering Practices with Kotlin
Better Start: Enforcing Best Engineering Practices with KotlinBetter Start: Enforcing Best Engineering Practices with Kotlin
Better Start: Enforcing Best Engineering Practices with Kotlin
 
Testing Svelte with Jest: Validate Your Components Quickly!
Testing Svelte with Jest: Validate Your Components Quickly!Testing Svelte with Jest: Validate Your Components Quickly!
Testing Svelte with Jest: Validate Your Components Quickly!
 
Developing Lightning Components for Communities.pptx
Developing Lightning Components for Communities.pptxDeveloping Lightning Components for Communities.pptx
Developing Lightning Components for Communities.pptx
 
Hands on React Native: From Zero to Hero
Hands on React  Native:  From Zero to HeroHands on React  Native:  From Zero to Hero
Hands on React Native: From Zero to Hero
 
Remote Work: Gateway to Freedom
Remote Work: Gateway to FreedomRemote Work: Gateway to Freedom
Remote Work: Gateway to Freedom
 
Kindness Engineering: Focusing on What Matters
Kindness Engineering: Focusing on What MattersKindness Engineering: Focusing on What Matters
Kindness Engineering: Focusing on What Matters
 
Gauge + Taiko, BDD for Web Revived
Gauge + Taiko, BDD for Web RevivedGauge + Taiko, BDD for Web Revived
Gauge + Taiko, BDD for Web Revived
 
Modern Web Testing: Going Beyond Selenium
Modern Web Testing: Going Beyond SeleniumModern Web Testing: Going Beyond Selenium
Modern Web Testing: Going Beyond Selenium
 
Do you even Function? Guiding Through Functional Interfaces
Do you even Function? Guiding Through Functional InterfacesDo you even Function? Guiding Through Functional Interfaces
Do you even Function? Guiding Through Functional Interfaces
 
From Robotium to Appium: Choose your Journey
From Robotium to Appium: Choose your Journey From Robotium to Appium: Choose your Journey
From Robotium to Appium: Choose your Journey
 
Stress Driven Development, and How to Avoid It
Stress Driven Development, and How to Avoid ItStress Driven Development, and How to Avoid It
Stress Driven Development, and How to Avoid It
 
Uphill Battle of Mobile Visual Regression
Uphill Battle of Mobile Visual RegressionUphill Battle of Mobile Visual Regression
Uphill Battle of Mobile Visual Regression
 
Engineer in Test: Bridging the Gap
Engineer in Test: Bridging the GapEngineer in Test: Bridging the Gap
Engineer in Test: Bridging the Gap
 
Domain Driven Testing: Know What You’re Doing
Domain Driven Testing: Know What You’re DoingDomain Driven Testing: Know What You’re Doing
Domain Driven Testing: Know What You’re Doing
 
Back to the CompletableFuture: Concurrency in Action
Back to the CompletableFuture: Concurrency in ActionBack to the CompletableFuture: Concurrency in Action
Back to the CompletableFuture: Concurrency in Action
 
Modern Web Testing: Going Beyond Selenium
Modern Web Testing: Going Beyond Selenium Modern Web Testing: Going Beyond Selenium
Modern Web Testing: Going Beyond Selenium
 
Build Tests to Build Websites
Build Tests to Build WebsitesBuild Tests to Build Websites
Build Tests to Build Websites
 

Último

8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfVishalKumarJha10
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationShrmpro
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfproinshot.com
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
Generic or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsBert Jan Schrijver
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfayushiqss
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesVictorSzoltysek
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 

Último (20)

8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions Presentation
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
Generic or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisions
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 

Aspect Oriented Programming Hidden Toolkit That You Already Have

  • 1. Aspect Oriented Programming: Hidden Toolkit That You Already Have Dmitry Vinnik Senior Software Engineer dvinnik@salesforce.com JEEConf 2017
  • 2. Talk Outline ● AOP Overview ● Anatomy of AspectJ ● Spring AOP ● Java Agents Overview ● AOP Implementations
  • 3. Talk Motivation Everyday challenges: profiling, code coverage, HotSwap, and monitoring. Aspect Oriented Programing (AOP) is a solution to all these problems. AOP is a Hidden Toolkit that everyone knows, but very few use. This talk aims to change that.
  • 5. Aspect Oriented Programming (AOP) Overview AOP is Cross-Cutting Concerns paradigm Answers Questions: ● What is my code coverage? ● Is my JVM even running? ● Ahh, do I have to restart my app again? ● How long does this process take?
  • 6. Hidden Toolkit: AOP Simple Example 1: 1 2: 1 ... 39: 63245986 40: 102334155 Process: 1837 ms Code Sample: Output:
  • 8. Hidden Toolkit: AspectJ Overview Defining Aspect 1) Definition 3) Advice 2) Pointc ut
  • 10. Hidden Toolkit: AspectJ Pointcuts Anatomy of Pointcut 1) Definition 2) Ty pe 3) Expressi on
  • 11. Hidden Toolkit: AspectJ Pointcuts - Continue Methods/Constructors: 1) call(Signature) 2) execution(Signature) Fields (specific to AspectJ): 1) get(Signature) 2) set(Signature) Exceptions Handler: 1) handler(TypePattern) Types of Pointcuts
  • 12. Hidden Toolkit: AspectJ Pointcuts - Continue 1) Signature 1) TypePattern Pointcut Expressions 3) Pointcut/Combination
  • 14. Hidden Toolkit: AspectJ Advice Anatomy of AspectJ Advice 1) Advice Type 2) Pointcut Binding 3) Main Logic
  • 15. Hidden Toolkit: AspectJ Advice - Continue Types of Advices 1) Before 1) After 1) Around
  • 16. Hidden Toolkit: AspectJ Advice - Continue 1. Single Pointcut 2. Joint Pointcuts 3. Inline Pointcut 4. Parameterized Pointcut Pointcut Binding
  • 18. Hidden Toolkit: AspectJ - Advanced 1) Generics 2) Abstract Aspects 3) Declare Parents 4) Declare Soft Generics and Declarations
  • 19. Hidden Toolkit: AspectJ Execution 1) Switching to ‘ajc’ compiler Aspect Process started 1: 1 2: 1 ... 39: 63245986 40: 102334155 Aspect Process took: 1845 ms 2) Run function bound by the Aspect 3) Output:
  • 21. Hidden Toolkit: AspectJ - Annotations Usage 1) Aspect Initialization 1) Pointcut Definition Annotations Development Style - Aspect Core
  • 22. Hidden Toolkit: AspectJ - Annotations Usage 1) Before 1) After 1) Around Annotations Development Style - Advices
  • 23. Hidden Toolkit: Spring AOP Spring AOP Overview
  • 24. Hidden Toolkit: Spring AOP - Configuration @Configuration and XML XML@Configuration
  • 25. Hidden Toolkit: Spring AOP - AspectJ Annotation Native AspectJ Support: 1. Aspect 2. Pointcut 3. Before 4. After 5. Around Spring AspectJ Support
  • 26. Hidden Toolkit: Spring AOP - Schema-Based Aspects XML Aspects: 1. Aspect 2. Pointcut 3. Before 4. After 5. Around Spring XML-based Aspects
  • 27. Hidden Toolkit: Spring AOP - AspectJ vs. Schema AspectJ Annotations: ● (+) DRY by-design ● (+) Pointcut combination ● (+) Reusable outside of Spring ● (-) Requires >Java 5 support Spring XML: ● (+) Best for Enterprise services ● (+) Summary of enabled aspects ● (-) No separation of concerns ● (-) No Pointcut combinations Comparing Two Approaches
  • 28. Hidden Toolkit: Spring AOP - Limitations ● No Field-level interception allowed (i.e. #set(), #get()) ● Limited support for objects not managed by Spring ● No support for certain general pointcuts (ex. handler, call) Limitation of Spring In Comparison With Native AspectJ
  • 29. Hidden Toolkit: Anatomy of Java Agents Overview
  • 30. Hidden Toolkit: Anatomy of Java Agents Building Java Agents 1) Premain Java Agent class 2) Implement ClassFileTransformer (next slide) 3) Compile java agent and run as: java -javaagent:simple-agent.jar -jar test-app.jar
  • 31. Hidden Toolkit: Building Java Agents Defining Class Transformer
  • 33. AOP Implementations Main Types: 1. Code Coverage: JaCoCo, Clover 2. Benchmarks/Profilers: JMH, AppDynamics 3. Improved Compilation: HotswapAgent, JRebel 4. Application Monitoring: AppDynamics Main Types of AOP Implementations
  • 35. AOP Implementation: Code Coverage JaCoCo[1] Report: Class Info:
  • 36. AOP Implementation: Code Coverage - Continue ● No setup required (i.e. Java Agent) ● Produces detailed reports of Code Coverage ● Highlights coverage down to code lines ● Instruments Running/Pre-Execution code Why AOP with JaCoCo?
  • 37. AOP Implementation: Code Coverage - Continue ● Classes are instrumented on the fly by Java Agent a. Performs in-memory pre-processing of all files b. Instruments using java.lang.instrument.Instrumentation ● Collects coverage data into .exec files ● Process collected data into .html reports How It Actually Works
  • 39. AOP Implementation: Benchmarks # Benchmark mode: Throughput, ops/time # Benchmark: org.sample.MyBenchmark.testMethod # Run progress: 0.00% complete, ETA 00:06:40 # Fork: 1 of 10 # Warmup Iteration 1: 0.414 ops/s # Warmup Iteration 2: 0.416 ops/s # Warmup Iteration 3: 0.417 ops/s Java Microbenchmark Harness, JMH[1] Code Sample: Output:
  • 40. AOP Implementation: Benchmark - Continue ● Reliable measure of individual units (ex. microservices) ● Supported by Oracle ● Highly customizable: • State • Time Unit • Mode Why AOP with JMH?
  • 42. AOP Implementation: Improved Compilation Default Java Hotswap handles re-compilation of java files. Does Not Handle: ● Renaming/Addition/Deletion of Methods ● Changes to Static/Non-Static Fields ● Changes to Enum Classes ● Changes to Class Hierarchy (interfaces/superclasses) Java Hotswap
  • 43. AOP Implementation: Improved Compilation ● Java Agent that handles imperfections of Java Hotswap. ● Can be run as a javaagent on a start of JVM: • java -XXaltjvm=dcevm -javaagent:hotswap-agent.jar test-app.jar ● Can be attached to running JVM via com.sun.tools.attach.VirtualMachine calls ● Currently in beta version Note: Java agents like HotswapAgent do not directly change main binaries, but rather modify proxied copy of the binaries. HotswapAgent[1]
  • 44. AOP Implementation: Improved Compilation ● No setup required (i.e. Java Agent) ● Productivity Booster (especially in enterprise) ● More Control over JVM ● Verbose Logging Why AOP with HotswapAgent?
  • 46. AOP Implementations: Other Solutions ● Atlassian Clover (Code Coverage) ● JCov (Code Coverage) ● AppDynamics (Benchmark, Monitoring) ● Statsd-JVM (Profiler) ● JRebel (Improved Compilation)
  • 47. Q/A
  • 48. About Speaker Twitter: @DmitryVinnik LinkedIn: in/dmitry-vinnik/ Email: dvinnik@salesforce.com Dmitry Vinnik