3

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 @Testin the above code. 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

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:

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.

Community
  • 1
  • 1
batman
  • 3,565
  • 5
  • 20
  • 41

1 Answers1

0

The default compliance level of the AspectJ compiler is independent of the version of the Java runtime. The default (ajc) is Java 1.4, as is specified in the documentation:

Set compliance level to 1.4 (default) This implies -source 1.4 and -target 1.2.

Thus, if you aspects need a higher compliance version, you need to specify the version explicitly when calling ajc:

ajc -classpath aspectjrt.jar:junit-4.10.jar -1.5 -outxml -outjar aspects.jar  TestCaseTrace.java
                                            ^^^^ you need this
dhke
  • 15,008
  • 2
  • 39
  • 56
  • It gives some other warnings like :`TestCaseTrace.java:21 [warning] advice defined in aspects.TestCaseTrace has not been applied [Xlint:adviceDidNotMatch] TestCaseTrace.java:15 [warning] advice defined in aspects.TestCaseTrace has not been applied [Xlint:adviceDidNotMatch]` – batman Aug 06 '15 at 08:09
  • @batman That means that the pointcut never matched. Since you don't have any `-inpath` (or `-injar`) specification I would expect that (no files to process). – dhke Aug 06 '15 at 08:16
  • I have edited the question, can you help me to find where I'm making mistake? – batman Aug 06 '15 at 08:42