Slide 1

Slide 1 text

Magic development with Aspects Initiation to Aspect Oriented Programming App Dev Con 2017 - Amsterdam

Slide 2

Slide 2 text

About... Xavier F. Gouchet Android Architect Jenkins/Sonar Admin Bash / Python tools dev Bad Puns Advocate @xgouchet on Github, StackOverflow, Twitter, …

Slide 3

Slide 3 text

Goal for the afternoon What’s AOP The theory, and motivation How does it work A bit of technical details on byte code and compilation Concrete examples The part where you do the work and I relax… or not. @xgouchet

Slide 4

Slide 4 text

“ “Let's start at the very beginning (A very good place to start)” Julie Andrews @xgouchet

Slide 5

Slide 5 text

It’s NOT ... ◎ A new programming language ◎ Something to replace OOP ◎ The solution to all your problems ! @xgouchet

Slide 6

Slide 6 text

It is a tool to ... ◎ Enhance modularity ◎ Separate cross cutting concerns ◎ Focus on business logic ◎ Reduce noise in the source code @xgouchet

Slide 7

Slide 7 text

Cross-cutting concerns ? Any concern that is not business logic and appears everywhere @xgouchet

Slide 8

Slide 8 text

Cross-cutting concerns Logging Performances Security Transactions Validation Serialisation Cache Notification Multithreading Memoization @xgouchet

Slide 9

Slide 9 text

“ “Let me tell you a story (to chill the bones)” Iron Maiden @xgouchet

Slide 10

Slide 10 text

void foo() { Log.v("Tag", "foo"); beginTransaction(); try { bar(); // ... setTransactionSuccessful(); } catch (Exception e) { reportError(e); } finally { endTransaction(); } Log.v("Tag", "foo done"); } Before / After

Slide 11

Slide 11 text

void foo() { Log.v("Tag", "foo"); beginTransaction(); try { bar(); // ... setTransactionSuccessful(); } catch (Exception e) { reportError(e); } finally { endTransaction(); } Log.v("Tag", "foo done"); } Before / After

Slide 12

Slide 12 text

void foo() { Log.v("Tag", "foo"); beginTransaction(); try { bar(); // ... setTransactionSuccessful(); } catch (Exception e) { reportError(e); } finally { endTransaction(); } Log.v("Tag", "foo done"); } Before / After

Slide 13

Slide 13 text

void foo() { Log.v("Tag", "foo"); beginTransaction(); try { bar(); // ... setTransactionSuccessful(); } catch (Exception e) { reportError(e); } finally { endTransaction(); } Log.v("Tag", "foo done"); } Before / After

Slide 14

Slide 14 text

void foo() { Log.v("Tag", "foo"); beginTransaction(); try { bar(); // ... setTransactionSuccessful(); } catch (Exception e) { reportError(e); } finally { endTransaction(); } Log.v("Tag", "foo done"); } Before / After

Slide 15

Slide 15 text

void foo() { Log.v("Tag", "foo"); beginTransaction(); try { bar(); // ... setTransactionSuccessful(); } catch (Exception e) { reportError(e); } finally { endTransaction(); } Log.v("Tag", "foo done"); } Before / After

Slide 16

Slide 16 text

@Trace @SafeTransaction void foo() { bar(); // ... } Before / After

Slide 17

Slide 17 text

“ “I've been browsing… (... inspectin’)” Weird Al Yankovic @xgouchet

Slide 18

Slide 18 text

input[type=text] { font-family: monospace; } blockquote:before { content: '\201C'; font-size: 300%; font-family: serif; } #nav-home p { color: #FF4630; cursor: pointer; } .contact img:hover { border: 1px #004080; } CSS + HTML

Slide 19

Slide 19 text

CSS Attributes Changes in appearance applied to … Elements Nodes in the source code , defined by … Selectors Rules and patterns @xgouchet

Slide 20

Slide 20 text

“ “If you know how to use CSS, Then you know how to use AOP” …me @xgouchet

Slide 21

Slide 21 text

Pointcuts Rules and patterns JoinPoints Positions in the source code , defined by … Advices Changes in behavior applied to … Aspects @xgouchet

Slide 22

Slide 22 text

Advice New behavior (=code) @xgouchet

Slide 23

Slide 23 text

JoinPoint Almost every selectable step in bytecode execution @xgouchet

Slide 24

Slide 24 text

Pointcuts Description of a (list of) joinpoints where an advice should be applied @xgouchet

Slide 25

Slide 25 text

“ “You can knit a sweater by the fireside” The Beatles @xgouchet

Slide 26

Slide 26 text

Bytecode weaving AspectJ @xgouchet

Slide 27

Slide 27 text

Code Weaving class Foo {…} class Bar {…} class Baz {…} class Bal {…} @xgouchet

Slide 28

Slide 28 text

“ “It's time to get our hands dirty” Natalie Grant @xgouchet

Slide 29

Slide 29 text

github.com/xgouchet/magic-appdevcon

Slide 30

Slide 30 text

Logging The “Hello World” of AOP @xgouchet

Slide 31

Slide 31 text

public Foo(); Signature: ()V Code: Stack=1, Locals=1, Args_size=1 0: aload_0 1: invokespecial #1; //Method java/lang/Object."":()V 4: return LineNumberTable: line 1: 0 public java.lang.String getBar(); Signature: ()Ljava/lang/String; Code: Stack=1, Locals=1, Args_size=1 0: aload_0 1: getfield #2; //Field bar:Ljava/lang/String; 4: areturn LineNumberTable: line 5: 0 public void setBar(java.lang.String); Signature: (Ljava/lang/String;)V Code: Stack=2, Locals=2, Args_size=2 0: aload_0 1: aload_1 2: putfield #2; //Field bar:Ljava/lang/String; 5: return LineNumberTable: line 8: 0 line 9: 5 } JD-GUI : http://jd.benow.ca/ But what’s going on really ? @xgouchet

Slide 32

Slide 32 text

Monitoring Let’s measure things… @xgouchet

Slide 33

Slide 33 text

Retry Because sh*t happens @xgouchet

Slide 34

Slide 34 text

Threads Because #PerfMatters @xgouchet

Slide 35

Slide 35 text

Compile Checks Too many rules to remember @xgouchet

Slide 36

Slide 36 text

Aspects Order Finer control of all your aspects @xgouchet

Slide 37

Slide 37 text

Memoization Because computing is hard @xgouchet

Slide 38

Slide 38 text

Object Pool Hack your constructors @xgouchet

Slide 39

Slide 39 text

It’s just the beginning of the journey - Many other pointcuts - Inter-type declarations - Change class hierarchy - Add fields - Patch jar files @xgouchet

Slide 40

Slide 40 text

“ “It’s a kind of magic” Queen @xgouchet

Slide 41

Slide 41 text

Wizardry or Witchcraft @Aspect public class PreventNPEAspect { @Pointcut("within(com.example.model.*)") public void withinModelClass() {} @Around("withinModelClass() && execution(* *(..))") public void advice(ProceeJoinPoint jp) { Object[] args = jp.getArgs(); for (int i = 0; i < args.length; i++) { if (args[i] == null) { args[i] = ""; } } jp.proceed(args); } }

Slide 42

Slide 42 text

Wizardry or Witchcraft @Aspect public class PreventNPEAspect { @Around("execution(@PreventStringNPE * *(..))") public void advice(ProceeJoinPoint jp) { Object[] args = jp.getArgs(); for (int i = 0; i < args.length; i++) { if (args[i] == null) { args[i] = ""; } } jp.proceed(args); } }

Slide 43

Slide 43 text

“ “I wanna find some answers I wanna ask for some help ” Bruce Springsteen @xgouchet

Slide 44

Slide 44 text

FAQ ◎ What about stack traces ? Caused by: java.lang.RuntimeException at com.sample.aspectMyAspect.myAdvice(MyAspect.java:666) at com.app.model.Foo.bar(Foo.java:69) at com.app.model.Foo.(Foo.java:42) at java.lang.Class.newInstance(Native Method) <9 more> ◎ What about breakpoints ? Stepping through your code is ok ◎ What about proguard ? Obfuscation performed after weaving @xgouchet

Slide 45

Slide 45 text

“ “Forever in debt to your priceless advice” Nirvana @xgouchet

Slide 46

Slide 46 text

Bad practices and code smells ◎ Pokemon (gotta catch’em all) ◎ Wizard of Oz (pay no attention to the man behind the curtain) ◎ Dr Octopus (my left tentacle doesn’t know what my right is doin’) ◎ Inception (aspects within aspects …) ◎ MacGyver (the swiss army knife @xgouchet

Slide 47

Slide 47 text

Things to remember ◎ Focus on Cross Cutting concerns ◎ If you can do it more easily with OOP don’t use AOP ◎ You can only weave code you own @xgouchet

Slide 48

Slide 48 text

Writing Aspects… ◎ Inheritance, abstracts, generics, inner classes… ◎ Unit tests ◎ Android flavors ◎ AOP is real programming @xgouchet

Slide 49

Slide 49 text

“ “There’s somethin’ we can use, so don't say no” Toni Basil @xgouchet

Slide 50

Slide 50 text

Useful links ◎ Android AspectJ plugin ○ https://github.com/deezer/Android-Aspectj-Plugin ◎ Counsel ○ https://github.com/deezer/Counsel ◎ AspectJ Documentation ○ https://eclipse.org/aspectj/doc/next/progguide/ ◎ Java Bytecode Viewer : JD-GUI ○ http://jd.benow.ca/ @xgouchet

Slide 51

Slide 51 text

https://speakerdeck.com/xgouchet Thanks for listening! Any question ? Deezer’s hiring http://jobs.deezer.com