Anas Lachheb
Anas Lachheb

Reputation: 441

advice defined has not been applied : Aspectj Pointcut

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

Upvotes: 3

Views: 7952

Answers (3)

Joe Ogun
Joe Ogun

Reputation: 1

before(): execution(* YourOwnPackage.*.*(..))
{
     //packages is com
    System.out.println(" TEST");    

}

Use this as a start to find your own advice.

Upvotes: 0

Mark D
Mark D

Reputation: 41

[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

Upvotes: 0

kriegaex
kriegaex

Reputation: 67457

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.

Upvotes: 3

Related Questions