Magic Development with Aspects (Big Android BBQ...

Magic Development with Aspects (Big Android BBQ Europe 2016)

Xavier F. Gouchet

August 16, 2016

Annual Pro sale · Save $30

Have presentations of your own?

Share with private links, password protection, and more control over your work.

Transcript

41 slides
  1. About... Xavier F. Gouchet Android Architect Jenkins/Sonar Admin Bad Puns

    Advocate @xgouchet on Github, StackOverflow, Twitter, …
  2. “ “Let's start at the very beginning (A very good

    place to start)” Julie Andrews @xgouchet
  3. It’s NOT ... ◎ A new programming language ◎ Something

    to replace OOP ◎ The solution to all your problems ! @xgouchet
  4. It is a tool to ... ◎ Enhance modularity ◎

    Separate cross cutting concerns ◎ Focus on business logic ◎ Reduce noise in the source code @xgouchet
  5. “ “Let me tell you a story (to chill the

    bones)” Iron Maiden @xgouchet
  6. void foo() { Log.v("Tag", "foo"); beginTransaction(); try { bar(); //

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

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

    ... setTransactionSuccessful(); } catch (Exception e) { reportError(e); } finally { endTransaction(); } Log.v("Tag", "foo done"); } Before / After
  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
  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
  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
  12. 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
  13. CSS Attributes Changes in appearance applied to … Elements Nodes

    in the source code , defined by … Selectors Rules and patterns @xgouchet
  14. “ “If you know how to use CSS, Then you

    know how to use AOP” …me @xgouchet
  15. Pointcuts Rules and patterns JoinPoints Positions in the source code

    , defined by … Advices Changes in behavior applied to … Aspects @xgouchet
  16. 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); } }
  17. 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); } }
  18. “ “I wanna find some answers I wanna ask for

    some help ” Bruce Springsteen @xgouchet
  19. 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.<init>(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
  20. 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
  21. More about AspectJ… ◎ Aspects are singleton by default ◎

    Inter-type declarations ◦ Change class hierarchy ◦ Add fields ◎ Declare aspects precedence ◎ Patch jar libraries @xgouchet
  22. 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
  23. Writing Aspects… ◎ Inheritance, abstracts, generics, inner classes… ◎ Unit

    tests ◎ Android flavors ◎ AOP is real programming @xgouchet
  24. 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

More Decks by Xavier F. Gouchet

Other Decks in Programming