AOP on Android
-
Upload
tibor-sramek -
Category
Software
-
view
1.134 -
download
0
Transcript of AOP on Android
![Page 1: AOP on Android](https://reader033.fdocuments.us/reader033/viewer/2022050614/58ef6a211a28ab8f248b4623/html5/thumbnails/1.jpg)
Aspect-oriented programming
The next level of abstraction on Android
Tibor Šrámek
AUGUST 26, 2015
![Page 2: AOP on Android](https://reader033.fdocuments.us/reader033/viewer/2022050614/58ef6a211a28ab8f248b4623/html5/thumbnails/2.jpg)
2CONFIDENTIAL
Topics
Cross-cutting concerns1
Separation of cross-cutting concerns2
Basics of aspect-oriented programming3
AspectJ4
Examples5
![Page 3: AOP on Android](https://reader033.fdocuments.us/reader033/viewer/2022050614/58ef6a211a28ab8f248b4623/html5/thumbnails/3.jpg)
3CONFIDENTIAL
CROSS-CUTTING CONCERNS
![Page 4: AOP on Android](https://reader033.fdocuments.us/reader033/viewer/2022050614/58ef6a211a28ab8f248b4623/html5/thumbnails/4.jpg)
4CONFIDENTIAL
Cross-cutting concerns
Module 1
Module 3
Module 2
Event
Tracking
Caching
Logging
Error
tracking
Event
tracking 2
Event
tracking 3
Module 4
Imagine a simple system
![Page 5: AOP on Android](https://reader033.fdocuments.us/reader033/viewer/2022050614/58ef6a211a28ab8f248b4623/html5/thumbnails/5.jpg)
5CONFIDENTIAL
Cross-cutting concerns
Module 1 Module 2 Module 3 Module 4 Module 5
original functionality functionality 1 functionality 2 functionality 3
functionality 4 functionality 5 functionality 6 functionality 7
Tangled & redundant code
![Page 6: AOP on Android](https://reader033.fdocuments.us/reader033/viewer/2022050614/58ef6a211a28ab8f248b4623/html5/thumbnails/6.jpg)
6CONFIDENTIAL
AOP
![Page 7: AOP on Android](https://reader033.fdocuments.us/reader033/viewer/2022050614/58ef6a211a28ab8f248b4623/html5/thumbnails/7.jpg)
7CONFIDENTIAL
What is AOP?
Module 1 Module 2 Module 3 Module 4 Module 5
original functionality functionality 1 functionality 2 functionality 3
functionality 4 functionality 5 functionality 6 functionality 7
![Page 8: AOP on Android](https://reader033.fdocuments.us/reader033/viewer/2022050614/58ef6a211a28ab8f248b4623/html5/thumbnails/8.jpg)
8CONFIDENTIAL
What is AOP?
Modules in one layer
CCCs in a separate layer extracted from the
original modules (AOP is the glue)
![Page 9: AOP on Android](https://reader033.fdocuments.us/reader033/viewer/2022050614/58ef6a211a28ab8f248b4623/html5/thumbnails/9.jpg)
9CONFIDENTIAL
What is AOP?
•AOP is an extension of OOP
• It increases modularity by the separation of CCCs
•Decreases code redundancy
•Adds functionality using injection without
modifying the original code
•Besides CCC separation it gives us power…
![Page 10: AOP on Android](https://reader033.fdocuments.us/reader033/viewer/2022050614/58ef6a211a28ab8f248b4623/html5/thumbnails/10.jpg)
10CONFIDENTIAL
ASPECTJ
![Page 11: AOP on Android](https://reader033.fdocuments.us/reader033/viewer/2022050614/58ef6a211a28ab8f248b4623/html5/thumbnails/11.jpg)
11CONFIDENTIAL
What is AspectJ?
•It’s an AOP extension for Java
•Very powerful
•Easy to learn and use
•Java code is valid AspectJ code
•AspectJ = Java + some magic
![Page 12: AOP on Android](https://reader033.fdocuments.us/reader033/viewer/2022050614/58ef6a211a28ab8f248b4623/html5/thumbnails/12.jpg)
12CONFIDENTIAL
AOP terminology
•Advice - injected code
•Join point - a point of execution
•Pointcut - point of injection (set of JPs)
•Aspect - advice + pointcut
•Weaving - process of injection
![Page 13: AOP on Android](https://reader033.fdocuments.us/reader033/viewer/2022050614/58ef6a211a28ab8f248b4623/html5/thumbnails/13.jpg)
13CONFIDENTIAL
Weaving
Weaving can be static (compile-time)
or dynamic (runtime)
![Page 14: AOP on Android](https://reader033.fdocuments.us/reader033/viewer/2022050614/58ef6a211a28ab8f248b4623/html5/thumbnails/14.jpg)
14CONFIDENTIAL
Android integration
// build.gradle
apply plugin: 'android-aspectj'
buildscript {
dependencies {
classpath 'com.uphyca.gradle:gradle-android-aspectj-plugin:0.9.12'
}
}
// an annotation style AspectJ example aspect
@Aspect
public class LoggerAspect {
@Before("call(* MyClass.myMethod())")
public void onMyMethodCalled() {
Logger.log("My method is called.");
}
}
![Page 15: AOP on Android](https://reader033.fdocuments.us/reader033/viewer/2022050614/58ef6a211a28ab8f248b4623/html5/thumbnails/15.jpg)
15CONFIDENTIAL
Advice types
•Before code will be injected before the given join point
•Aftercode will be injected after the given join point
•Around code will be injected instead of the given join point
• AfterReturning, AfterThrowing
@Before("myPointcut()")
public void myAdvice() {
// do something here, e.g. null-guarding
}
@After("call(int *.*View+.get*(..))")
public void myAdvice(JoinPoint joinPoint) {
// A JoinPoint object contains information
about the execution context(method signature,
arguments, containing class, etc.)
}
@Around("myPointcut1() && !myPointCut2()")
public void myAdvice(ProceedingJoinPoint point) {
// Pre processing work..
point.proceed();
// Post processing work..
// Around is perfect e.g. to skip a method
if a parameter is null or to replace
a buggy method in a library
}
![Page 16: AOP on Android](https://reader033.fdocuments.us/reader033/viewer/2022050614/58ef6a211a28ab8f248b4623/html5/thumbnails/16.jpg)
16CONFIDENTIAL
Pointcuts
•Method: call() & execution()
•Constructor: call() & execution()
•Field access: get() & set()
"execution(* *.myMethod(..))"
"call(private MyType.new(..))"
"get(String MyClass.name)"
Signature:
<modifiers> <return_value> <type>.<method_name>(parameter_list)
Signature:
<modifiers> <class_type>.new(<parameter_list>)
Signature:
<field_type> <class_type>.<field_name>
![Page 17: AOP on Android](https://reader033.fdocuments.us/reader033/viewer/2022050614/58ef6a211a28ab8f248b4623/html5/thumbnails/17.jpg)
17CONFIDENTIAL
Advanced pointcuts
•Class initialization
•Object initialization
•Advice execution
•Exception handling
•Annotations
•Etc. (virtually any point in the execution)
![Page 18: AOP on Android](https://reader033.fdocuments.us/reader033/viewer/2022050614/58ef6a211a28ab8f248b4623/html5/thumbnails/18.jpg)
18CONFIDENTIAL
Aspects
Aspects can:
•contain regular fields & methods
(e.g. for counting)
•track private fields & methods
(don’t have to break encapsulation)
•inherit from other aspects
(e.g. for reusing advices)
![Page 19: AOP on Android](https://reader033.fdocuments.us/reader033/viewer/2022050614/58ef6a211a28ab8f248b4623/html5/thumbnails/19.jpg)
19CONFIDENTIAL
Aspects
Aspects also can:
•be inner classes
•be instantiated
•declare advice execution order
•implement inter-type declarations
![Page 20: AOP on Android](https://reader033.fdocuments.us/reader033/viewer/2022050614/58ef6a211a28ab8f248b4623/html5/thumbnails/20.jpg)
20CONFIDENTIAL
USE CASES
![Page 21: AOP on Android](https://reader033.fdocuments.us/reader033/viewer/2022050614/58ef6a211a28ab8f248b4623/html5/thumbnails/21.jpg)
21CONFIDENTIAL
Common applications of AOP
•Logging
•Tracking (event, error, performance)
•Persistence
•Validation
•Synchronization
•Security
![Page 22: AOP on Android](https://reader033.fdocuments.us/reader033/viewer/2022050614/58ef6a211a28ab8f248b4623/html5/thumbnails/22.jpg)
22CONFIDENTIAL
AspectJ
Is it good for anything else besides the
separation of CCCs?
•Skip or replace methods
•Prevent or handle errors
•Dependency injection
•Separation of architectural layers
![Page 23: AOP on Android](https://reader033.fdocuments.us/reader033/viewer/2022050614/58ef6a211a28ab8f248b4623/html5/thumbnails/23.jpg)
23CONFIDENTIAL
Dependency injection with AOP
Consider Dagger, if you want to satisfy a dependency
you have to:
•make a module
•make a component
•modify the dependent class
![Page 24: AOP on Android](https://reader033.fdocuments.us/reader033/viewer/2022050614/58ef6a211a28ab8f248b4623/html5/thumbnails/24.jpg)
24CONFIDENTIAL
Dependency injection with AOP
With AOP you only have to do this:
@Aspect
class DependencyInjector {
@After("execution(Dependent.new(..))")
public void inject(JoinPoint p) {
Dependent dependent = (Dependent) p.getThis();
dependent.dependency = new Dependency();
}
}
![Page 25: AOP on Android](https://reader033.fdocuments.us/reader033/viewer/2022050614/58ef6a211a28ab8f248b4623/html5/thumbnails/25.jpg)
25CONFIDENTIAL
Architecture with AOP
Imagine a simple architecture@Aspect
public class MyAspect {
private MyView view;
private MyPresenter presenter;
private MyProvider provider;
@After("execution(MyView.onCreate(..))")
private void init(JoinPoint jp) {
view = (MyView) jp.getThis();
presenter = new MyPresenter();
provider = new MyProvider();
}
@After("execution(MyView.myButtonClicked(..))")
private void myButtonClicked() {
presenter.createRequest();
}
@AfterReturning(pointcut = "execution(MyPresenter.createRequest(..))",
returning = "request")
private void requestCreated(Request request) {
provider.sendRequest(request);
}
@After("execution(MyProvider.onResponse(..))")
private void responseArrived(JoinPoint jp) {
String result = presenter.processResponse((Response) jp.getArgs()[0]);
view.showResult(result);
}
}
![Page 26: AOP on Android](https://reader033.fdocuments.us/reader033/viewer/2022050614/58ef6a211a28ab8f248b4623/html5/thumbnails/26.jpg)
26CONFIDENTIAL
Architecture with AOP
A system like this is:
•Modular (e.g. multiple presenters for a view)
•Flexible (e.g. don’t have to use interfaces)
•Easy to test (don’t have to mock anything,
test only what you want)
![Page 27: AOP on Android](https://reader033.fdocuments.us/reader033/viewer/2022050614/58ef6a211a28ab8f248b4623/html5/thumbnails/27.jpg)
27CONFIDENTIAL
Tips & tricks
You have to implement something
• that probably will be removed in the future?
Put it to an aspect! You will have to just delete the aspect.
• for only one build variant? Put it to an aspect in that variant!
In many cases you will have to implement only the differences.
Unleash your imagination…
(but don’t try to build the whole system with aspects)
![Page 28: AOP on Android](https://reader033.fdocuments.us/reader033/viewer/2022050614/58ef6a211a28ab8f248b4623/html5/thumbnails/28.jpg)
28CONFIDENTIAL
OUTRO
![Page 29: AOP on Android](https://reader033.fdocuments.us/reader033/viewer/2022050614/58ef6a211a28ab8f248b4623/html5/thumbnails/29.jpg)
29CONFIDENTIAL
AOP supported languages
• Java
• Objective-C
• .NET (C# / VB.NET)
• C / C++
• Python
• Perl
• PHP
• JavaScript
• Groovy
• Ruby
• ActionScript
• Ada
• AutoHotkey
• COBOL
• ColdFusion
• Lisp
• Delphi
• Haskell
• Logtalk
• Lua
• Matlab
• Prolog
• Racket
• Smalltalk
• Etc.
![Page 30: AOP on Android](https://reader033.fdocuments.us/reader033/viewer/2022050614/58ef6a211a28ab8f248b4623/html5/thumbnails/30.jpg)
30CONFIDENTIAL
Active users of AOP
•Microsoft
•Oracle
•IBM
•SAP
•Siemens
•Red Hat
•Epam
There is a whole paradigm called Aspect-oriented software development…
![Page 31: AOP on Android](https://reader033.fdocuments.us/reader033/viewer/2022050614/58ef6a211a28ab8f248b4623/html5/thumbnails/31.jpg)
31CONFIDENTIAL
Useful links
• http://www.eclipse.org/aspectj
• http://fernandocejas.com/2014/08/03/aspect-oriented-programming-in-android
• https://github.com/uPhyca/gradle-android-aspectj-plugin
• https://www.youtube.com/watch?v=cq7wpLI0hco
• Google is your friend
![Page 32: AOP on Android](https://reader033.fdocuments.us/reader033/viewer/2022050614/58ef6a211a28ab8f248b4623/html5/thumbnails/32.jpg)
32CONFIDENTIAL
Literature
• Robert E. Filman, Tzilla Elrad, Siobhan Clarke, Mehmet Aksit: Aspect-Oriented Software
Development. Addison-Wesley Professional, October 2004, 800 pp. ISBN: 0321219767.
• Renaud Pawlak, Jean-Philippe Retaillé, Lionel Seinturier: Foundations of AOP for J2EE Development.
Apress, September 2005, 352 pp. ISBN: 1590595076.
• Ramnivas Laddad: AspectJ in Action: Practical Aspect-Oriented Programming. Manning Publications,
July 2003, 512 pp. ISBN: 1930110936.
• Siobhan Clarke, Elisa Baniassad: Aspect-Oriented Analysis and Design: The Theme Approach.
Addison-Wesley Professional, March 2005, 400 pp. ISBN: 0321246748.
• Ivar Jacobson, Pan-Wei Ng: Aspect-Oriented Software Development with Use Cases. Addison-Wesley
Professional, December 2004, 464 pp. ISBN: 0321268881.
• Joseph D. Gradecki, Nicholas Lesiecki: Mastering AspectJ: Aspect-Oriented Programming in Java.
Wiley, April 2003, 456 pp. ISBN: 0471431044.
• Russell Miles: Aspectj Cookbook. O'Reilly Media, December 2004, 331 pp. ISBN: 0596006543.
![Page 33: AOP on Android](https://reader033.fdocuments.us/reader033/viewer/2022050614/58ef6a211a28ab8f248b4623/html5/thumbnails/33.jpg)
33CONFIDENTIAL
@After(“execution(* Presenter.present()”)void endPresentation() {
presenter.say(
” Thank You”);}