I'm trying to create an annotation to log all methods in annotated class, but I have a problem with my pointcut, it's not applied (AspectJ version 1.7.4, aspectj-maven-plugin version 1.7).
(advice defined in com.test.util.log.Logger has not been applied
[Xlint:adviceDidNotMatch]).
Pointcut:
@Pointcut(value = "execution(* (@Loggable *).*(..))"))
Annotation:
@Retention(RetentionPolicy.RUNTIME)
@Target(value = { ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.TYPE })
public @interface Loggable {
public enum Level {
TRACE, DEBUG, INFO, WARN, ERROR, FATAL
};
boolean entry() default true;
boolean exit() default true;
String prefix() default "";
String suffix() default "";
Level level() default Level.DEBUG;
}
Thank you
I assume that the annotation is not in the unnamed top level package but in a package like com.company.application.subpackage
. If this is true you need to use the fully qualified package name in annotation-style @AspectJ. In native syntax that would not be necessary because you could use imports there. So the pointcut should be:
@Pointcut("execution(* (@com.company.application.subpackage.Loggable *).*(..))"))
The way you use the parentheses makes the pointcut only match methods of classes annotated by @Loggable
. The annotation's @Target
definition says that it can also be applied to methods and constructors. Those will not be matched by your pointcut, you would have to modify it for that purpose. I hope you know that, I am just mentioning it for safety.