3

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

kriegaex
  • 63,017
  • 15
  • 111
  • 202
Anas Lachheb
  • 441
  • 4
  • 17

3 Answers3

3

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.

kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • I have two scenarios : 1) Annotating class => the pointcut need to match all methods in this class 2) Annotating method=> the pointcut need to match only method annotated – Anas Lachheb Oct 15 '14 at 10:47
  • Ich can help you with both, no problem. But does the pointcut for case #1 work? Tell me that first in order to exclude other root causes, then we cann proceed with case #2. – kriegaex Oct 15 '14 at 13:14
0

[Xlint:adviceDidNotMatch]) means that your point cut was not applied in the compiled project. Most likely you didn't place your annotation on any method.

PS I also recommend not to reinvent the wheel and try aspect4log

Mark D
  • 41
  • 2
0
before(): execution(* YourOwnPackage.*.*(..))
{
     //packages is com
    System.out.println(" TEST");    

}

Use this as a start to find your own advice.