, , . , , .
, Java + AspectJ Android, , - , , , , .
:
, pointcut.
package org.android10.gintonic.annotation;
import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@Retention(RUNTIME)
@Target({ METHOD, CONSTRUCTOR })
public @interface MyAnnotation {}
:
package de.scrum_master.app;
import org.android10.gintonic.annotation.MyAnnotation;
public class Application {
@MyAnnotation
public void myMethod() {
System.out.println("Hi there!");
}
public static void main(String[] args) {
new Application().myMethod();
}
}
:
package de.scrum_master.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class AnnotationAspect {
private static final String POINTCUT_METHOD =
"execution(@org.android10.gintonic.annotation.MyAnnotation * *(..))";
@Pointcut(POINTCUT_METHOD)
public void methodToAnnotate() {}
@Around("methodToAnnotate()")
public Object weaveJoinPoint(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println(joinPoint);
joinPoint.proceed();
return null;
}
}
:
execution(void de.scrum_master.app.Application.myMethod())
Hi there!
, , . . , , , path aspectjrt.jar.
: , Gradle AspectJ, , . .