Skip to content

Commit 7d3e893

Browse files
authored
Merge pull request #1 from north2016/master
update
2 parents 243e3e7 + e9561aa commit 7d3e893

File tree

107 files changed

+2014
-503
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+2014
-503
lines changed

.idea/.name

-1
This file was deleted.

.idea/gradle.xml

+5-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

aop/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

aop/build.gradle

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import com.android.build.gradle.LibraryPlugin
2+
import org.aspectj.bridge.IMessage
3+
import org.aspectj.bridge.MessageHandler
4+
import org.aspectj.tools.ajc.Main
5+
6+
apply plugin: 'com.android.library'
7+
8+
android {
9+
compileSdkVersion 23
10+
buildToolsVersion "23.0.3"
11+
12+
defaultConfig {
13+
minSdkVersion 16
14+
targetSdkVersion 23
15+
versionCode 1
16+
versionName "1.0"
17+
}
18+
buildTypes {
19+
release {
20+
minifyEnabled false
21+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
22+
}
23+
}
24+
}
25+
26+
dependencies {
27+
compile project(':lib')
28+
compile 'org.aspectj:aspectjrt:1.8.9'
29+
}
30+
31+
32+
/**
33+
* @使用ajc编译java代码,同时织入切片代码
34+
* 使用 AspectJ 的编译器(ajc,一个java编译器的扩展)
35+
* 对所有受 aspect 影响的类进行织入。
36+
* 在 gradle 的编译 task 中增加额外配置,使之能正确编译运行。
37+
*/
38+
android.libraryVariants.all { variant ->
39+
LibraryPlugin plugin = project.plugins.getPlugin(LibraryPlugin)
40+
def javaCompile = variant.javaCompile
41+
javaCompile.doLast {
42+
String[] args = ["-showWeaveInfo",
43+
"-1.8",
44+
"-inpath", javaCompile.destinationDir.toString(),
45+
"-aspectpath", javaCompile.classpath.asPath,
46+
"-d", javaCompile.destinationDir.toString(),
47+
"-classpath", javaCompile.classpath.asPath,
48+
"-bootclasspath", plugin.project.android.bootClasspath.join(
49+
File.pathSeparator)]
50+
51+
MessageHandler handler = new MessageHandler(true);
52+
new Main().run(args, handler)
53+
54+
def log = project.logger
55+
for (IMessage message : handler.getMessages(null, true)) {
56+
switch (message.getKind()) {
57+
case IMessage.ABORT:
58+
case IMessage.ERROR:
59+
case IMessage.FAIL:
60+
log.error message.message, message.thrown
61+
break;
62+
case IMessage.WARNING:
63+
case IMessage.INFO:
64+
log.info message.message, message.thrown
65+
break;
66+
case IMessage.DEBUG:
67+
log.debug message.message, message.thrown
68+
break;
69+
}
70+
}
71+
}
72+
}
73+
74+
tasks.withType(JavaCompile) {
75+
options.encoding = "UTF-8"
76+
}
77+
78+

aop/proguard-rules.pro

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /Users/baixiaokang/Library/Android/sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}

aop/src/main/AndroidManifest.xml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.app.aop">
2+
3+
<application android:allowBackup="true" android:label="@string/app_name"
4+
android:supportsRtl="true">
5+
6+
</application>
7+
8+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.app.aop.aspect;
2+
3+
import android.text.TextUtils;
4+
5+
import com.app.aop.utils.LogUtils;
6+
import com.app.aop.utils.MemoryCacheManager;
7+
8+
import org.aspectj.lang.ProceedingJoinPoint;
9+
import org.aspectj.lang.annotation.Around;
10+
import org.aspectj.lang.annotation.Aspect;
11+
import org.aspectj.lang.annotation.Pointcut;
12+
import org.aspectj.lang.reflect.MethodSignature;
13+
14+
import java.util.List;
15+
16+
/**
17+
* Created by baixiaokang on 16/10/24.
18+
* 根据MemoryCache注解自动添加缓存代理代码,通过aop切片的方式在编译期间织入源代码中
19+
* 功能:缓存某方法的返回值,下次执行该方法时,直接从缓存里获取。
20+
*/
21+
@Aspect
22+
public class MemoryCacheAspect {
23+
24+
@Pointcut("execution(@com.app.annotation.aspect.MemoryCache * *(..))")//方法切入点
25+
public void methodAnnotated() {
26+
}
27+
28+
@Around("methodAnnotated()")//在连接点进行方法替换
29+
public Object aroundJoinPoint(ProceedingJoinPoint joinPoint) throws Throwable {
30+
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
31+
String methodName = methodSignature.getName();
32+
MemoryCacheManager mMemoryCacheManager = MemoryCacheManager.getInstance();
33+
StringBuilder keyBuilder = new StringBuilder();
34+
keyBuilder.append(methodName);
35+
for (Object obj : joinPoint.getArgs()) {
36+
if (obj instanceof String) keyBuilder.append((String) obj);
37+
else if (obj instanceof Class) keyBuilder.append(((Class) obj).getSimpleName());
38+
}
39+
String key = keyBuilder.toString();
40+
Object result = mMemoryCacheManager.get(key);//key规则 : 方法名+参数1+参数2+...
41+
LogUtils.showLog("MemoryCache", "key:" + key + "--->" + (result != null ? "not null" : "null"));
42+
if (result != null) return result;//缓存已有,直接返回
43+
result = joinPoint.proceed();//执行原方法
44+
if (result instanceof List && result != null && ((List) result).size() > 0 //列表不为空
45+
|| result instanceof String && !TextUtils.isEmpty((String) result)//字符不为空
46+
|| result instanceof Object && result != null)//对象不为空
47+
mMemoryCacheManager.add(key, result);//存入缓存
48+
LogUtils.showLog("MemoryCache", "key:" + key + "--->" + "save");
49+
return result;
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.app.aop.aspect;
2+
3+
4+
import com.app.aop.utils.LogUtils;
5+
6+
import org.aspectj.lang.ProceedingJoinPoint;
7+
import org.aspectj.lang.annotation.Around;
8+
import org.aspectj.lang.annotation.Aspect;
9+
import org.aspectj.lang.annotation.Pointcut;
10+
import org.aspectj.lang.reflect.MethodSignature;
11+
12+
import java.util.concurrent.TimeUnit;
13+
14+
/**
15+
* 根据注解TimeLog自动添加打印方法耗代码,通过aop切片的方式在编译期间织入源代码中
16+
* 功能:自动打印方法的耗时
17+
*/
18+
@Aspect
19+
public class TimeLogAspect {
20+
21+
@Pointcut("execution(@com.app.annotation.aspect.TimeLog * *(..))")//方法切入点
22+
public void methodAnnotated() {
23+
}
24+
25+
@Pointcut("execution(@com.app.annotation.aspect.TimeLog *.new(..))")//构造器切入点
26+
public void constructorAnnotated() {
27+
}
28+
29+
@Around("methodAnnotated() || constructorAnnotated()")//在连接点进行方法替换
30+
public Object aroundJoinPoint(ProceedingJoinPoint joinPoint) throws Throwable {
31+
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
32+
String className = methodSignature.getDeclaringType().getSimpleName();
33+
String methodName = methodSignature.getName();
34+
long startTime = System.nanoTime();
35+
Object result = joinPoint.proceed();//执行原方法
36+
StringBuilder keyBuilder = new StringBuilder();
37+
keyBuilder.append(methodName+":");
38+
for (Object obj : joinPoint.getArgs()) {
39+
if (obj instanceof String) keyBuilder.append((String) obj);
40+
else if (obj instanceof Class) keyBuilder.append(((Class) obj).getSimpleName());
41+
}
42+
String key = keyBuilder.toString();
43+
LogUtils.showLog("TimeLog", (className + "." + key + joinPoint.getArgs().toString() + " --->:" + "[" + (TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime)) + "ms]"));// 打印时间差
44+
return result;
45+
}
46+
}

0 commit comments

Comments
 (0)