SingleClickAspect.java
2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.studymachine.www.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.CodeSignature;
import timber.log.Timber;
/**
* time : 2019/12/06
* desc : 防重复点击切面
*/
@Aspect
public class SingleClickAspect {
/** 最近一次点击的时间 */
private long mLastTime;
/** 最近一次点击的标记 */
private String mLastTag;
/**
* 方法切入点
*/
@Pointcut("execution(@com.studymachine.www.aop.SingleClick * *(..))")
public void method() {}
/**
* 在连接点进行方法替换
*/
@Around("method() && @annotation(singleClick)")
public void aroundJoinPoint(ProceedingJoinPoint joinPoint, SingleClick singleClick) throws Throwable {
CodeSignature codeSignature = (CodeSignature) joinPoint.getSignature();
// 方法所在类
String className = codeSignature.getDeclaringType().getName();
// 方法名
String methodName = codeSignature.getName();
// 构建方法 TAG
StringBuilder builder = new StringBuilder(className + "." + methodName);
builder.append("(");
Object[] parameterValues = joinPoint.getArgs();
for (int i = 0; i < parameterValues.length; i++) {
Object arg = parameterValues[i];
if (i == 0) {
builder.append(arg);
} else {
builder.append(", ")
.append(arg);
}
}
builder.append(")");
String tag = builder.toString();
long currentTimeMillis = System.currentTimeMillis();
if (currentTimeMillis - mLastTime < singleClick.value() && tag.equals(mLastTag)) {
Timber.tag("SingleClick");
Timber.i("%s 毫秒内发生快速点击:%s", singleClick.value(), tag);
return;
}
mLastTime = currentTimeMillis;
mLastTag = tag;
// 执行原方法
joinPoint.proceed();
}
}