配置
classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.8'
app.gradle
apply plugin: 'android-aspectjx'
TestEvent
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface TestEvent {
}
@Aspect
class MethodAspect {
// 此处可以是一个类的切点
@Pointcut("@within(com.ji.aopaspectj.TestEvent) || @annotation(com.ji.aopaspectj.TestEvent)")
public void pointcut1() {
}
@Before("pointcut1()")
public void execBeforeAnnotatedArithmeticMethod(JoinPoint joinPoint) throws Throwable {
try {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = joinPoint.getTarget().getClass().
getMethod(signature.getMethod().getName(), signature.getMethod().getParameterTypes());
if (method.isAnnotationPresent(TestEvent.class)){
Log.e("----------AOP", "methe111d-===="+signature.getName());
}else if (joinPoint.getTarget().getClass().isAnnotationPresent(TestEvent.class)){
Log.e("----------AOP", "methed-===="+joinPoint.getTarget().getClass()+"."+signature.getName());
}
}catch(Exception e) {
}
}
@Around("execution (* com.ji.aopaspectj.*.*(..))")
public void getTime(ProceedingJoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
String name = signature.toShortString();
long time = System.currentTimeMillis();
try {
joinPoint.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
Log.e("TAG------------", name + " cost--" + (System.currentTimeMillis() - time));
}
}