Giter Site home page Giter Site logo

saf-aop's Introduction

SAF-AOP

@Tony沈哲 on weibo Download GitHub release License

下载安装

在根目录下的build.gradle中添加

buildscript {
     repositories {
         jcenter()
     }
     dependencies {
         classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:1.0.8'
     }
 }

在app 模块目录下的build.gradle中添加

apply plugin: 'com.hujiang.android-aspectjx'

...

dependencies {
    compile 'com.safframework:saf-aop:1.1.0'
    ...
}

基于aspectj的AOP,无需使用耗费性能的反射.不过,需要在build.gradle中配置一下aspectj

注解名称 作用 备注
@Async 借助rxjava,异步执行app中的方法
@Cacheable Spring Cache风格的Cache注解,将结果放于缓存中 只适用于android4.0以后
@LogMethod 将方法的入参和出参都打印出来,可以用于调试
@HookMethod 可以在调用某个方法之前、以及之后进行hook 比较适合埋点的场景,可以单独使用也可以跟任何自定义注解配合使用
@Prefs 将方法返回的结果放入AppPrefs中 只适用于android4.0以后
@Safe 可以安全地执行方法,而无需考虑是否会抛出运行时异常
@Trace 用于追踪某个方法花费的时间,可以用于性能调优的评判

@Async的使用方法:

	@Async
	private void useAsync() {
		Log.e(TAG, " thread=" + Thread.currentThread().getId());
		Log.e(TAG, "ui thread=" + Looper.getMainLooper().getThread().getId());
	}

@Cacheable的使用方法:

	@Cacheable(key = "user")
	private User initData() {
		User user = new User();
		user.userName = "tony";
		user.password = "123456";
		return user;
	}

这里的@Cacheable,实际上用到Cache,要获取Cache也很简单.

@Trace的使用方法:

	@Trace
	@Async
	private void loadUser() {
		Log.e(TAG, " thread=" + Thread.currentThread().getId());
		Log.e(TAG, "ui thread=" + Looper.getMainLooper().getThread().getId());
		Cache cache = Cache.get(this);
		User user = (User) cache.getObject("user");
		Toast.makeText(MainActivity.this, SAFUtils.printObject(user), Toast.LENGTH_SHORT).show();
	}

将@Trace和@Async两个注解结合使用,可以看到调用loadUser()方法花费的时间.

05-18 14:31:31.229 21190-21190/app.magicwindow.cn.testsaf I/MainActivity: MainActivity=loadUser() take [1ms]
05-18 14:31:31.231 21190-22033/app.magicwindow.cn.testsaf E/com.test.saf.activity.MainActivity:  thread=14876
05-18 14:31:31.231 21190-22033/app.magicwindow.cn.testsaf E/com.test.saf.activity.MainActivity: ui thread=1

@HookMethod的使用方法:

不写beforeMethod和afterMethod,则相当于没有使用@HookMethod
beforeMethod和afterMethod对应的都是方法名,分别表示在调用doSomething()之前执行和之后执行。目前还不支持在beforeMethod和afterMethod中传递参数。

   @HookMethod(beforeMethod="dosthbeforeMethod",afterMethod="dosthafterMethod")
   void doSomething() {

   }

Proguard

-keep class com.safframework.aop.** { *; }

ChangeLog

版本更新记录

License

Copyright (C) 2017 Tony Shen.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

saf-aop's People

Contributors

fengzhizi715 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.