dependencies {
...
compile 'org.aspectj:aspectjrt:1.8.9'
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.aspectj:aspectjtools:1.8.9'
classpath 'org.aspectj:aspectjweaver:1.8.9'
}
}
dependencies {
...
}
// 贴上面那段没用的代码是为了说明:下面的任务代码与dependencies同级
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 ->
if (!variant.buildType.isDebuggable()) {
log.debug("Skipping non-debuggable build type '${variant.buildType.name}'.")
return;
}
JavaCompile javaCompile = variant.javaCompile
javaCompile.doLast {
String[] args = ["-showWeaveInfo",
"-1.8",
"-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;
}
}
}
}
@Before("execution(public * *(..))")
public void before(JoinPoint point) {
System.out.println("CSDN_LQR");
}
@Around("execution(* *to(..))")
public void around(ProceedingJoinPoint joinPoint) {
System.out.println("CSDN");
joinPoint.proceed();
System.out.println("LQR");
}
@After("execution(* com.lqr..*to(..))")
public void after(JoinPoint point) {
System.out.println("CSDN_LQR");
}
@AfterReturning("execution(int com.lqr.*(..))")
public void afterReturning(JoinPoint point, Object returnValue) {
System.out.println("CSDN_LQR");
}
@AfterThrowing(value = "execution(* com.lqr..*(..))", throwing = "ex")
public void afterThrowing(Throwable ex) {
System.out.println("ex = " + ex.getMessage());
}
@Before("execution(* com.lqr..*(..))")
public void before(JoinPoint point) {
System.out.println("CSDN_LQR");
}
@AfterThrowing(value = "execution(* com.lqr..*(..))", throwing = "ex")
public void afterThrowing(Throwable ex) {
System.out.println("记录日志");
}
@Pointcut("execution(* com.lqr..*(..))")
public void pointcut() {}
@Before("pointcut()")
public void before(JoinPoint point) {
System.out.println("CSDN_LQR");
}
@AfterThrowing(value = "pointcut()", throwing = "ex")
public void afterThrowing(Throwable ex) {
System.out.println("记录日志");
}
public void test(View view) {
System.out.println("Hello, I am CSDN_LQR");
}
@Aspect
public class TestAnnoAspect {
@Pointcut("execution(* com.lqr.androidaopdemo.MainActivity.test(..))")
public void pointcut() {
}
@Before("pointcut()")
public void before(JoinPoint point) {
System.out.println("@Before");
}
@Around("pointcut()")
public void around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("@Around");
}
@After("pointcut()")
public void after(JoinPoint point) {
System.out.println("@After");
}
@AfterReturning("pointcut()")
public void afterReturning(JoinPoint point, Object returnValue) {
System.out.println("@AfterReturning");
}
@AfterThrowing(value = "pointcut()", throwing = "ex")
public void afterThrowing(Throwable ex) {
System.out.println("@afterThrowing");
System.out.println("ex = " + ex.getMessage());
}
}
@Around("pointcut()")
public void around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("@Around");
joinPoint.proceed();// 目标方法执行完毕
}
public void test(View view) {
System.out.println("Hello, I am CSDN_LQR");
int a = 1 / 0;
}
@Around("pointcut()")
public void around(ProceedingJoinPoint joinPoint) throws Throwable {
long beginTime = SystemClock.currentThreadTimeMillis();
joinPoint.proceed();
long endTime = SystemClock.currentThreadTimeMillis();
long dx = endTime - beginTime;
System.out.println("耗时:" + dx + "ms");
}
MethodSignature signature = (MethodSignature) joinPoint.getSignature(); String name = signature.getName(); // 方法名:test Method method = signature.getMethod(); // 方法:public void com.lqr.androidaopdemo.MainActivity.test(android.view.View) Class returnType = signature.getReturnType(); // 返回值类型:void Class declaringType = signature.getDeclaringType(); // 方法所在类名:MainActivity String[] parameterNames = signature.getParameterNames(); // 参数名:view Class[] parameterTypes = signature.getParameterTypes(); // 参数类型:View
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnoTrace {
String value();
int type();
}
@TestAnnoTrace(value = "lqr_test", type = 1)
public void test(View view) {
System.out.println("Hello, I am CSDN_LQR");
}
@Pointcut("execution(@com.lqr.androidaopdemo.TestAnnoTrace * *(..))")
public void pointcut() {}
MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); // 通过Method对象得到切点上的注解 TestAnnoTrace annotation = method.getAnnotation(TestAnnoTrace.class); String value = annotation.value(); int type = annotation.type();
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有