Date post: | 21-Jan-2018 |
Category: |
Software |
Upload: | tibor-sramek |
View: | 1,131 times |
Download: | 0 times |
Aspect-oriented programming
The next level of abstraction on Android
Tibor Šrámek
AUGUST 26, 2015
2CONFIDENTIAL
Topics
Cross-cutting concerns1
Separation of cross-cutting concerns2
Basics of aspect-oriented programming3
AspectJ4
Examples5
3CONFIDENTIAL
CROSS-CUTTING CONCERNS
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
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
6CONFIDENTIAL
AOP
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
8CONFIDENTIAL
What is AOP?
Modules in one layer
CCCs in a separate layer extracted from the
original modules (AOP is the glue)
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…
10CONFIDENTIAL
ASPECTJ
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
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
13CONFIDENTIAL
Weaving
Weaving can be static (compile-time)
or dynamic (runtime)
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.");
}
}
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
}
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>
17CONFIDENTIAL
Advanced pointcuts
•Class initialization
•Object initialization
•Advice execution
•Exception handling
•Annotations
•Etc. (virtually any point in the execution)
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)
19CONFIDENTIAL
Aspects
Aspects also can:
•be inner classes
•be instantiated
•declare advice execution order
•implement inter-type declarations
20CONFIDENTIAL
USE CASES
21CONFIDENTIAL
Common applications of AOP
•Logging
•Tracking (event, error, performance)
•Persistence
•Validation
•Synchronization
•Security
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
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
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();
}
}
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);
}
}
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)
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)
28CONFIDENTIAL
OUTRO
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.
30CONFIDENTIAL
Active users of AOP
•Microsoft
•Oracle
•IBM
•SAP
•Siemens
•Red Hat
•Epam
There is a whole paradigm called Aspect-oriented software development…
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
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.
33CONFIDENTIAL
@After(“execution(* Presenter.present()”)void endPresentation() {
presenter.say(
” Thank You”);}