简体   繁体   English

AspectJ在跟踪源文件中的注释时出错

[英]AspectJ is giving error for tracing annotations in source files

I have an aspect code like this: 我有一个像这样的方面代码:

package aspects;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.aspectj.lang.Signature;
import org.junit.Test;

public aspect TestCaseTrace{

    // pointcut traceTestCase() : (execution(public * test*(..)));

    pointcut traceTestCase() : (execution(@Test * *(..)));

    before(): traceTestCase(){
        String sourceName = thisJoinPointStaticPart.getSourceLocation().getWithinType().getCanonicalName();
        Signature sig = thisJoinPointStaticPart.getSignature();
        System.out.println("Test case name is " + sig.getDeclaringTypeName() + "." + sig.getName() + "\n\n");
    }

    after(): traceTestCase(){
    // Logger.getLogger("Tracing").log(Level.INFO,"Test case got over\n\n");
        System.out.println("Test case got over\n\n");
    }
}

I'm trying to trace the junit @Test in the above code. 我试图在上面的代码中跟踪junit @Test While I compile the aspect, with the below command: 我编译方面时,使用以下命令:

ajc -classpath aspectjrt.jar:junit-4.10.jar  -outxml -outjar aspects.jar  TestCaseTrace.java

but this command gives an error like this: 但是这个命令给出了这样的错误:

TestCaseTrace.java:13 [error] annotation type patterns are only supported at Java 5 compliance level or above
pointcut traceTestCase() : (execution(@Test * *(..)));


1 error

where I'm making the mistake? 我在哪里弄错了?

Java version used is 1.7 and aspect version is 1.8.6 使用的Java版本是1.7,方面版本是1.8.6

Edit: 编辑:

After following the answer I could able to compile with warnings. 在听完答案后,我可以编译并发出警告。 But after that if I use the resulting aspect.jar to inspect the junit test case: 但之后如果我使用生成的aspect.jar来检查junit测试用例:

import org.junit.Test;
class HelloWorldApp {

    @Test
    public void testSome()
    {
        A.methodA();
    }

}


class A{
    public static void methodA()
    {
        methodB();
    }

    public static void methodB()
    {

    }
}

and run with the command line: 并使用命令行运行:

java -javaagent:aspectjweaver.jar -cp aspects.jar:. HelloWorldApp

Nothing is getting printed. 什么都没有印刷。

The default compliance level of the AspectJ compiler is independent of the version of the Java runtime. AspectJ编译器的默认合规级别与Java运行时的版本无关。 The default (ajc) is Java 1.4, as is specified in the documentation : 默认(ajc)是Java 1.4,如文档中所指定:

Set compliance level to 1.4 (default) This implies -source 1.4 and -target 1.2. 将合规性级别设置为1.4(默认值)这意味着-source 1.4和-target 1.2。

Thus, if you aspects need a higher compliance version, you need to specify the version explicitly when calling ajc : 因此,如果您需要更高的合规性版本,则需要在调用ajc时明确指定版本:

ajc -classpath aspectjrt.jar:junit-4.10.jar -1.5 -outxml -outjar aspects.jar  TestCaseTrace.java
                                            ^^^^ you need this

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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