SlideShare a Scribd company logo
Android AOP
Mike
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate");
setContentView(R.layout.activity_main);
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume");
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d(TAG, "onRestart");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop");
}
@Override
protected void onDestroy() {
剖面導向程式設計
(AOP, Aspect-oriented programming)
• Log
• 儲存資料
• 性能監控
• 數據驗證
• 權限檢查
專有名詞
• Join point(連接點):可作為程式碼注入
目標的點。執行方法或存取變數。
• Pointcut(切入點):告訴程式碼注入工具,
要在何處注入。要收集分析的 join point。
• Advice(通知):注入到 class 檔案的程式
碼,有 before, after, around
• Aspect(剖面):Pointcut + Advice
AspectJ
• 可對 source(aj 檔或@Aspect註解的 java 檔)
直接編譯
• 對編譯過的 class 檔做處理
• 當 class 檔執行在 JVM 後做處理
Join point
• When a method is called
• When an exception is thrown
• When a variable is accessed
• When instantiating an object
• When referring an object
Pointcut
Advice
• Join points: these are the call, execution, get, set, preinitialization,
initialization, staticinitialization, handler, and adviceexecution designators.
Result
Point.class
萬用字元的用法
args, target, within
args, target, within
thisJoinPoint
• 取得程式碼的位置
buildscript {
repositories {
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
classpath 'org.aspectj:aspectjtools:1.8.1'
}
}
How to use
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.1.0'
testCompile 'junit:junit:4.12'
compile 'org.aspectj:aspectjrt:1.8.1'
}
app/build.gradle
build.gradle
import org.aspectj.bridge.IMessage
import org.aspectj.bridge.MessageHandler
import org.aspectj.tools.ajc.Main
final def log = project.logger
final def variants = project.android.applicationVariants
variants.all { variant ->
JavaCompile javaCompile = variant.javaCompile
javaCompile.doLast {
String[] args = ["-showWeaveInfo",
"-1.7",
"-inpath", javaCompile.destinationDir.toString(),
"-aspectpath", javaCompile.classpath.asPath,
"-d", javaCompile.destinationDir.toString(),
"-classpath", javaCompile.classpath.asPath,
"-bootclasspath", project.android.bootClasspath.join(File.pathSeparator)]
log.debug "ajc args: " + Arrays.toString(args)
MessageHandler handler = new MessageHandler(true);
new Main().run(args, handler);
for (IMessage message : handler.getMessages(null, true)) {
switch (message.getKind()) {
case IMessage.ABORT:
case IMessage.ERROR:
case IMessage.FAIL:
log.error message.message, message.thrown
break;
case IMessage.WARNING:
log.warn message.message, message.thrown
break;
case IMessage.INFO:
log.info message.message, message.thrown
break;
case IMessage.DEBUG:
log.debug message.message, message.thrown
break;
app/build.gradle
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate");
setContentView(R.layout.activity_main);
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume");
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d(TAG, "onRestart");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy");
}
}
@Aspect
public class BaseAspect {
@Pointcut("execution(* com.example.mc2j.myapplication.MainActivity.on*(..))")
public void logForActivity() {};
@Before("logForActivity()")
public void log(JoinPoint joinPoint) {
Log.e("Aspect", joinPoint.toShortString()
+ " [line " + joinPoint.getSourceLocation().getLine() + "]");
}
}
https://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html
Reference
• http://o7planning.org/en/10257/java-aspect-
oriented-programming-tutorial-with-aspectj
• http://blog.csdn.net/innost/article/details/49
387395
• https://docs.spring.io/spring/docs/current/spr
ing-framework-reference/html/aop.html
• https://www.mkyong.com/spring3/spring-
aop-aspectj-annotation-example/

More Related Content

What's hot

Automated%20testing%20with%20Espresso2.x
Automated%20testing%20with%20Espresso2.xAutomated%20testing%20with%20Espresso2.x
Automated%20testing%20with%20Espresso2.x
Tatsuya Maki
 
Unit & Automation Testing in Android - Stanislav Gatsev, Melon
Unit & Automation Testing in Android - Stanislav Gatsev, MelonUnit & Automation Testing in Android - Stanislav Gatsev, Melon
Unit & Automation Testing in Android - Stanislav Gatsev, Melon
beITconference
 

What's hot (19)

Android Wear Essentials
Android Wear EssentialsAndroid Wear Essentials
Android Wear Essentials
 
Automated%20testing%20with%20Espresso2.x
Automated%20testing%20with%20Espresso2.xAutomated%20testing%20with%20Espresso2.x
Automated%20testing%20with%20Espresso2.x
 
Code to DI For - Dependency Injection for Modern Applications
Code to DI For - Dependency Injection for Modern ApplicationsCode to DI For - Dependency Injection for Modern Applications
Code to DI For - Dependency Injection for Modern Applications
 
The Ring programming language version 1.8 book - Part 77 of 202
The Ring programming language version 1.8 book - Part 77 of 202The Ring programming language version 1.8 book - Part 77 of 202
The Ring programming language version 1.8 book - Part 77 of 202
 
Automated self-testing and health check of live Sling instances
Automated self-testing and health check of live Sling instancesAutomated self-testing and health check of live Sling instances
Automated self-testing and health check of live Sling instances
 
The Ring programming language version 1.7 book - Part 75 of 196
The Ring programming language version 1.7 book - Part 75 of 196The Ring programming language version 1.7 book - Part 75 of 196
The Ring programming language version 1.7 book - Part 75 of 196
 
Creating custom aggregation strategy
Creating custom aggregation strategyCreating custom aggregation strategy
Creating custom aggregation strategy
 
Android Loaders : Reloaded
Android Loaders : ReloadedAndroid Loaders : Reloaded
Android Loaders : Reloaded
 
Agile Swift
Agile SwiftAgile Swift
Agile Swift
 
Advanced Google Analytics for iOS - How to not kill your SEO
Advanced Google Analytics for iOS - How to not kill your SEOAdvanced Google Analytics for iOS - How to not kill your SEO
Advanced Google Analytics for iOS - How to not kill your SEO
 
Loaders (and why we should use them)
Loaders (and why we should use them)Loaders (and why we should use them)
Loaders (and why we should use them)
 
Unit & Automation Testing in Android - Stanislav Gatsev, Melon
Unit & Automation Testing in Android - Stanislav Gatsev, MelonUnit & Automation Testing in Android - Stanislav Gatsev, Melon
Unit & Automation Testing in Android - Stanislav Gatsev, Melon
 
Martin Anderson - threads v actors
Martin Anderson - threads v actorsMartin Anderson - threads v actors
Martin Anderson - threads v actors
 
Creating custom aggregator
Creating custom aggregatorCreating custom aggregator
Creating custom aggregator
 
More android code puzzles
More android code puzzlesMore android code puzzles
More android code puzzles
 
The Ring programming language version 1.5 book - Part 12 of 31
The Ring programming language version 1.5 book - Part 12 of 31The Ring programming language version 1.5 book - Part 12 of 31
The Ring programming language version 1.5 book - Part 12 of 31
 
The Ring programming language version 1.10 book - Part 82 of 212
The Ring programming language version 1.10 book - Part 82 of 212The Ring programming language version 1.10 book - Part 82 of 212
The Ring programming language version 1.10 book - Part 82 of 212
 
The Gradle in Ratpack: Dissected
The Gradle in Ratpack: DissectedThe Gradle in Ratpack: Dissected
The Gradle in Ratpack: Dissected
 
Signalsで Event処理を簡単に
Signalsで Event処理を簡単にSignalsで Event処理を簡単に
Signalsで Event処理を簡単に
 

Similar to Androidaop 170105090257

Android Best Practices
Android Best PracticesAndroid Best Practices
Android Best Practices
Yekmer Simsek
 
Quick Intro to Android Development
Quick Intro to Android DevelopmentQuick Intro to Android Development
Quick Intro to Android Development
Jussi Pohjolainen
 
Overview of Android Infrastructure
Overview of Android InfrastructureOverview of Android Infrastructure
Overview of Android Infrastructure
C.T.Co
 
Overview of Android Infrastructure
Overview of Android InfrastructureOverview of Android Infrastructure
Overview of Android Infrastructure
Alexey Buzdin
 
名古屋SGGAE/J勉強会 Grails、Gaelykでハンズオン
名古屋SGGAE/J勉強会 Grails、Gaelykでハンズオン名古屋SGGAE/J勉強会 Grails、Gaelykでハンズオン
名古屋SGGAE/J勉強会 Grails、Gaelykでハンズオン
Tsuyoshi Yamamoto
 

Similar to Androidaop 170105090257 (20)

Android Best Practices
Android Best PracticesAndroid Best Practices
Android Best Practices
 
Mini curso Android
Mini curso AndroidMini curso Android
Mini curso Android
 
Quick Intro to Android Development
Quick Intro to Android DevelopmentQuick Intro to Android Development
Quick Intro to Android Development
 
Sharper Better Faster Dagger ‡ - Droidcon SF
Sharper Better Faster Dagger ‡ - Droidcon SFSharper Better Faster Dagger ‡ - Droidcon SF
Sharper Better Faster Dagger ‡ - Droidcon SF
 
Overview of Android Infrastructure
Overview of Android InfrastructureOverview of Android Infrastructure
Overview of Android Infrastructure
 
Overview of Android Infrastructure
Overview of Android InfrastructureOverview of Android Infrastructure
Overview of Android Infrastructure
 
Android workshop
Android workshopAndroid workshop
Android workshop
 
Workshop 23: ReactJS, React & Redux testing
Workshop 23: ReactJS, React & Redux testingWorkshop 23: ReactJS, React & Redux testing
Workshop 23: ReactJS, React & Redux testing
 
Android - Anatomy of android elements & layouts
Android - Anatomy of android elements & layoutsAndroid - Anatomy of android elements & layouts
Android - Anatomy of android elements & layouts
 
名古屋SGGAE/J勉強会 Grails、Gaelykでハンズオン
名古屋SGGAE/J勉強会 Grails、Gaelykでハンズオン名古屋SGGAE/J勉強会 Grails、Gaelykでハンズオン
名古屋SGGAE/J勉強会 Grails、Gaelykでハンズオン
 
Android and the Seven Dwarfs from Devox'15
Android and the Seven Dwarfs from Devox'15Android and the Seven Dwarfs from Devox'15
Android and the Seven Dwarfs from Devox'15
 
Android training in mumbai
Android training in mumbaiAndroid training in mumbai
Android training in mumbai
 
yagdao-0.3.1 JPA guide
yagdao-0.3.1 JPA guideyagdao-0.3.1 JPA guide
yagdao-0.3.1 JPA guide
 
Android tutorial (2)
Android tutorial (2)Android tutorial (2)
Android tutorial (2)
 
Retrofit
RetrofitRetrofit
Retrofit
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Rohit android lab projects in suresh gyan vihar
Rohit android lab projects in suresh gyan viharRohit android lab projects in suresh gyan vihar
Rohit android lab projects in suresh gyan vihar
 
比XML更好用的Java Annotation
比XML更好用的Java Annotation比XML更好用的Java Annotation
比XML更好用的Java Annotation
 
Introducing Struts 2
Introducing Struts 2Introducing Struts 2
Introducing Struts 2
 
ActiveWeb: Chicago Java User Group Presentation
ActiveWeb: Chicago Java User Group PresentationActiveWeb: Chicago Java User Group Presentation
ActiveWeb: Chicago Java User Group Presentation
 

More from newegg (14)

互聯網產品運營
互聯網產品運營互聯網產品運營
互聯網產品運營
 
如何快速將Ui設計流程套入新專案
如何快速將Ui設計流程套入新專案如何快速將Ui設計流程套入新專案
如何快速將Ui設計流程套入新專案
 
Google play加入測試人員步驟
Google play加入測試人員步驟Google play加入測試人員步驟
Google play加入測試人員步驟
 
Test flight內部測試
Test flight內部測試Test flight內部測試
Test flight內部測試
 
Hybridapp 161209030125
Hybridapp 161209030125Hybridapp 161209030125
Hybridapp 161209030125
 
Boxtream tools-161106062349
Boxtream tools-161106062349Boxtream tools-161106062349
Boxtream tools-161106062349
 
Elasticsearch
ElasticsearchElasticsearch
Elasticsearch
 
使用者行為分析
使用者行為分析使用者行為分析
使用者行為分析
 
Software project management
Software project managementSoftware project management
Software project management
 
Ab Testing
Ab TestingAb Testing
Ab Testing
 
Growth Hacking
Growth HackingGrowth Hacking
Growth Hacking
 
App程式上架google play流程
App程式上架google play流程App程式上架google play流程
App程式上架google play流程
 
Design Pattern
Design PatternDesign Pattern
Design Pattern
 
How to Use Git?
How to Use Git?How to Use Git?
How to Use Git?
 

Recently uploaded

Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Peter Udo Diehl
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
Bhaskar Mitra
 

Recently uploaded (20)

Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
НАДІЯ ФЕДЮШКО БАЦ «Професійне зростання QA спеціаліста»
НАДІЯ ФЕДЮШКО БАЦ  «Професійне зростання QA спеціаліста»НАДІЯ ФЕДЮШКО БАЦ  «Професійне зростання QA спеціаліста»
НАДІЯ ФЕДЮШКО БАЦ «Професійне зростання QA спеціаліста»
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
 

Androidaop 170105090257

  • 2. public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG, "onCreate"); setContentView(R.layout.activity_main); } @Override protected void onResume() { super.onResume(); Log.d(TAG, "onResume"); } @Override protected void onStart() { super.onStart(); Log.d(TAG, "onStart"); } @Override protected void onRestart() { super.onRestart(); Log.d(TAG, "onRestart"); } @Override protected void onPause() { super.onPause(); Log.d(TAG, "onPause"); } @Override protected void onStop() { super.onStop(); Log.d(TAG, "onStop"); } @Override protected void onDestroy() { 剖面導向程式設計 (AOP, Aspect-oriented programming) • Log • 儲存資料 • 性能監控 • 數據驗證 • 權限檢查
  • 3. 專有名詞 • Join point(連接點):可作為程式碼注入 目標的點。執行方法或存取變數。 • Pointcut(切入點):告訴程式碼注入工具, 要在何處注入。要收集分析的 join point。 • Advice(通知):注入到 class 檔案的程式 碼,有 before, after, around • Aspect(剖面):Pointcut + Advice
  • 4. AspectJ • 可對 source(aj 檔或@Aspect註解的 java 檔) 直接編譯 • 對編譯過的 class 檔做處理 • 當 class 檔執行在 JVM 後做處理
  • 5. Join point • When a method is called • When an exception is thrown • When a variable is accessed • When instantiating an object • When referring an object
  • 7. Advice • Join points: these are the call, execution, get, set, preinitialization, initialization, staticinitialization, handler, and adviceexecution designators.
  • 10.
  • 15.
  • 16. buildscript { repositories { } dependencies { classpath 'com.android.tools.build:gradle:2.2.0' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files classpath 'org.aspectj:aspectjtools:1.8.1' } } How to use dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.1.0' testCompile 'junit:junit:4.12' compile 'org.aspectj:aspectjrt:1.8.1' } app/build.gradle build.gradle
  • 17. import org.aspectj.bridge.IMessage import org.aspectj.bridge.MessageHandler import org.aspectj.tools.ajc.Main final def log = project.logger final def variants = project.android.applicationVariants variants.all { variant -> JavaCompile javaCompile = variant.javaCompile javaCompile.doLast { String[] args = ["-showWeaveInfo", "-1.7", "-inpath", javaCompile.destinationDir.toString(), "-aspectpath", javaCompile.classpath.asPath, "-d", javaCompile.destinationDir.toString(), "-classpath", javaCompile.classpath.asPath, "-bootclasspath", project.android.bootClasspath.join(File.pathSeparator)] log.debug "ajc args: " + Arrays.toString(args) MessageHandler handler = new MessageHandler(true); new Main().run(args, handler); for (IMessage message : handler.getMessages(null, true)) { switch (message.getKind()) { case IMessage.ABORT: case IMessage.ERROR: case IMessage.FAIL: log.error message.message, message.thrown break; case IMessage.WARNING: log.warn message.message, message.thrown break; case IMessage.INFO: log.info message.message, message.thrown break; case IMessage.DEBUG: log.debug message.message, message.thrown break; app/build.gradle
  • 18. public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG, "onCreate"); setContentView(R.layout.activity_main); } @Override protected void onResume() { super.onResume(); Log.d(TAG, "onResume"); } @Override protected void onStart() { super.onStart(); Log.d(TAG, "onStart"); } @Override protected void onRestart() { super.onRestart(); Log.d(TAG, "onRestart"); } @Override protected void onPause() { super.onPause(); Log.d(TAG, "onPause"); } @Override protected void onStop() { super.onStop(); Log.d(TAG, "onStop"); } @Override protected void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy"); } }
  • 19. @Aspect public class BaseAspect { @Pointcut("execution(* com.example.mc2j.myapplication.MainActivity.on*(..))") public void logForActivity() {}; @Before("logForActivity()") public void log(JoinPoint joinPoint) { Log.e("Aspect", joinPoint.toShortString() + " [line " + joinPoint.getSourceLocation().getLine() + "]"); } }
  • 21. Reference • http://o7planning.org/en/10257/java-aspect- oriented-programming-tutorial-with-aspectj • http://blog.csdn.net/innost/article/details/49 387395 • https://docs.spring.io/spring/docs/current/spr ing-framework-reference/html/aop.html • https://www.mkyong.com/spring3/spring- aop-aspectj-annotation-example/

Editor's Notes

  1. 物件導向實現功能模組化,每個類別處理自己的事;如果希望在多個 UI 或邏輯的方法內加上重複的功能,也就是在任何地方都能呼叫,則適用剖面導向。 性能監控:一個方法要執行多久
  2. 不需要對原本的程式碼做修改,可直接注入
  3. 要收集分析的 joint point,其他不管
  4. https://eclipse.org/aspectj/doc/released/adk15notebook/jpsigs.html#join-point-matching
  5. 有參數的情況
  6. within的用法
  7. before advice 的參數型別和命名要和 pointcut 一致
  8. Main() 是 ajc 對 class 做進一步處理
  9. AOP 的真正作用,它只負責收集。一些簡單的功能可在裡面完成,而一些複雜的功能,則要交給其他類別處理。 和用正則一樣,需要時再做更深入的研究就好。