SlideShare una empresa de Scribd logo
1 de 48
Descargar para leer sin conexión
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

Más contenido relacionado

Más 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
 

Más 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

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
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 
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
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Hararemasabamasaba
 
%+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
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 
%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
 
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
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...masabamasaba
 

Último (20)

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
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
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-...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
%+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...
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
%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
 
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 🔝✔️✔️
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 

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