$30 off During Our Annual Pro Sale. View Details »

Magic Development with Aspects (Big Android BBQ Europe 2016)

Magic Development with Aspects (Big Android BBQ Europe 2016)

Xavier Gouchet

August 16, 2016
Tweet

More Decks by Xavier Gouchet

Other Decks in Programming

Transcript

  1. Magic development
    with Aspects
    Initiation to Aspect Oriented Programming
    Big Android BBQ Europe 2016

    View Slide

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

    View Slide


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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide


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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide


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

    View Slide

  17. 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

    View Slide

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

    View Slide


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

    View Slide

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

    View Slide

  21. Advice
    New behavior
    (=code)
    @xgouchet

    View Slide

  22. JoinPoint
    Almost every selectable step
    in bytecode execution
    @xgouchet

    View Slide

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

    View Slide


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

    View Slide

  25. Bytecode
    weaving
    AspectJ
    @xgouchet

    View Slide

  26. Code Weaving
    @xgouchet

    View Slide


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

    View Slide

  28. View Slide


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

    View Slide

  30. 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);
    }
    }

    View Slide

  31. 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);
    }
    }

    View Slide


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

    View Slide

  33. 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

    View Slide


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

    View Slide

  35. 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

    View Slide

  36. More about AspectJ…
    ◎ Aspects are singleton by default
    ◎ Inter-type declarations
    ○ Change class hierarchy
    ○ Add fields
    ◎ Declare aspects precedence
    ◎ Patch jar libraries
    @xgouchet

    View Slide

  37. 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

    View Slide

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

    View Slide


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

    View Slide

  40. 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/
    @xgouchet

    View Slide

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

    View Slide