简体   繁体   中英

ajc via cmd line doesn't recognize .aj aspect syntax

when trying to compile an aspect file using ajc and the command line, im getting error when trying to compile *.aj file (aspect syntax)

It works fine when compiling *.java aspect (using annotations)

Aspect Annotation - TestAspect.java :

@Aspect
public class TestAspect {

    @Pointcut("execution(* TestTarget.test(..))")
    void test() {} 

    @Before("test()")
    public void advice(JoinPoint joinPoint) {
        System.out.printf("TestAspect.advice() called on '%s'%n", joinPoint);

    }
}

Aspect Syntax - TestAspect.aj :

    public aspect TestAspect {

        pointcut test() : execution(* TestTarget.test*(...));

        before() : test()
        public void advice(JoinPoint joinPoint) {
            System.out.printf("TestAspect.advice() called on '%s'%n", joinPoint);
        }
}

when compiling TestAspect.aj im using the following command:

ajc -1.8 -sourceroots ./ -cp aspectjrt.jar;

and getting the following errors:

C:\****\TestAspect.aj:3 [error] Syntax error on token "...", "name pattern" expected
pointcut test() : execution(* TestTarget.test*(...));
                                               ^
C:\****\TestAspect.aj:6 [error] Syntax error on token "public", "{" expected
public void advice(JoinPoint joinPoint) {
^
C:\****\TestAspect.aj:7 [error] joinPoint cannot be resolved to a variable
System.out.printf("TestAspect.advice() called on '%s'%n", joinPoint);


3 errors

I didn't manage to find any solution while googling for over an hour. even not in the documentation.

I might be missing something, will be glad for some help.

solution:

public aspect TestAspect {

    pointcut test() : execution(* TestTarget.test*(..));

    before() : test() {
        System.out.printf("TestAspect.advice() called on '%s'%n", thisJoinPoint);
    }
}

1) notice the test() argument wildcard is only 2 dots instead of 3!

2) advice body doesn't have a method signature and you can reference thisJoinPoint without passing it as a parameter

(note im talking about the *.aj file)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM