Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Aspect Oriented Programming Hidden Toolkit That You Already Have

14 views

Published on

Any developer that worked on a software projects, considered at one point few things like how fast his/her function is, how much test coverage do they have, or if there is a better way to recompile their code without a need to restart JVM. All of these questions can be answered by Aspect Oriented Programming.

Aspect Oriented Programming (AOP) is a cross-cutting concern of writing code across modules to bring a common feature to variety of methods, classes and packages. To illustrate, AOP allows developers to see which parts of actual code was triggered by a test, or to see performance metrics of a certain function. These examples can also be extended to how JVM sees source code and goes beyond Hot Swap in recompiling in runtime using Aspect Oriented Programming.

This talk’s main goal is to show a real power of AOP with focus on how it is already used in the industry, and how any engineer can write their own AOP application, or Java Agent. As a part of this talk, the speaker will cover common Open Source Java Agents such as JaCoCo and JMH, and touch based on paid-base Java Agents such as JRebel or AppDynamics.

Recording: https://youtu.be/XmuwLYdYDAk

Published in: Software
  • Be the first to comment

Aspect Oriented Programming Hidden Toolkit That You Already Have

  1. 1. Aspect Oriented Programming: Hidden Toolkit That You Already Have Dmitry Vinnik Senior Software Engineer dvinnik@salesforce.com JEEConf 2017
  2. 2. Talk Outline ● AOP Overview ● Anatomy of AspectJ ● Spring AOP ● Java Agents Overview ● AOP Implementations
  3. 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.
  4. 4. Aspect Oriented Programming Overview
  5. 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. 6. Hidden Toolkit: AOP Simple Example 1: 1 2: 1 ... 39: 63245986 40: 102334155 Process: 1837 ms Code Sample: Output:
  7. 7. Hidden Toolkit: AspectJ Overview
  8. 8. Hidden Toolkit: AspectJ Overview Defining Aspect 1) Definition 3) Advice 2) Pointc ut
  9. 9. Hidden Toolkit: AspectJ Pointcuts Overview
  10. 10. Hidden Toolkit: AspectJ Pointcuts Anatomy of Pointcut 1) Definition 2) Ty pe 3) Expressi on
  11. 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. 12. Hidden Toolkit: AspectJ Pointcuts - Continue 1) Signature 1) TypePattern Pointcut Expressions 3) Pointcut/Combination
  13. 13. Hidden Toolkit: AspectJ Advices Overview
  14. 14. Hidden Toolkit: AspectJ Advice Anatomy of AspectJ Advice 1) Advice Type 2) Pointcut Binding 3) Main Logic
  15. 15. Hidden Toolkit: AspectJ Advice - Continue Types of Advices 1) Before 1) After 1) Around
  16. 16. Hidden Toolkit: AspectJ Advice - Continue 1. Single Pointcut 2. Joint Pointcuts 3. Inline Pointcut 4. Parameterized Pointcut Pointcut Binding
  17. 17. Hidden Toolkit: AspectJ Additional Information
  18. 18. Hidden Toolkit: AspectJ - Advanced 1) Generics 2) Abstract Aspects 3) Declare Parents 4) Declare Soft Generics and Declarations
  19. 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:
  20. 20. Hidden Toolkit: AspectJ Annotations Development Style
  21. 21. Hidden Toolkit: AspectJ - Annotations Usage 1) Aspect Initialization 1) Pointcut Definition Annotations Development Style - Aspect Core
  22. 22. Hidden Toolkit: AspectJ - Annotations Usage 1) Before 1) After 1) Around Annotations Development Style - Advices
  23. 23. Hidden Toolkit: Spring AOP Spring AOP Overview
  24. 24. Hidden Toolkit: Spring AOP - Configuration @Configuration and XML XML@Configuration
  25. 25. Hidden Toolkit: Spring AOP - AspectJ Annotation Native AspectJ Support: 1. Aspect 2. Pointcut 3. Before 4. After 5. Around Spring AspectJ Support
  26. 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. 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. 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. 29. Hidden Toolkit: Anatomy of Java Agents Overview
  30. 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. 31. Hidden Toolkit: Building Java Agents Defining Class Transformer
  32. 32. AOP Implementations Overview
  33. 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
  34. 34. AOP Implementations Code Coverage
  35. 35. AOP Implementation: Code Coverage JaCoCo[1] Report: Class Info:
  36. 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. 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
  38. 38. AOP Implementations Benchmarks
  39. 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. 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?
  41. 41. AOP Implementations Improved Compilation
  42. 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. 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. 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?
  45. 45. AOP Implementations Other Solutions
  46. 46. AOP Implementations: Other Solutions ● Atlassian Clover (Code Coverage) ● JCov (Code Coverage) ● AppDynamics (Benchmark, Monitoring) ● Statsd-JVM (Profiler) ● JRebel (Improved Compilation)
  47. 47. Q/A
  48. 48. About Speaker Twitter: @DmitryVinnik LinkedIn: in/dmitry-vinnik/ Email: dvinnik@salesforce.com Dmitry Vinnik

×