Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AspectJ (annotations) compile-time weave with Ant and NetBeans

I want to use compile-time AspectJ with Ant in NetBeans. I want to run it on Google App Engine, but it is not essential at the moment. AspectJ is annotation based.
I prefer compile-time weave (modification, instrumentation? of classes). I wouldn't like to use a custom classloader. How to achieve this?

What I already have:

I tried AspectJ Annotation Tutorial with NetBeans. I modified build.xml to process aspectj (using iajc Ant task) as described here. The problem is that it requires adding -javaagent:lib/aspectjweaver.jar (it is not possible on GAE).
Running my build generates this output:

info compiling C:\NetBeansProjects\TryAspectJ\src\net\andrewewhite\examples\HelloWorld.java
weaveinfo Join point 'method-call(void java.io.PrintStream.println(java.lang.String))' in Type 'net.andrewewhite.examples.HelloWorld' (HelloWorld.java:9) advised by before advice from 'net.andrewewhite.aspects.BasicAspect' (BasicAspect.class:17(from BasicAspect.java))
weaveinfo Join point 'method-call(void java.io.PrintStream.println(java.lang.String))' in Type 'net.andrewewhite.examples.HelloWorld' (HelloWorld.java:9) advised by after advice from 'net.andrewewhite.aspects.BasicAspect' (BasicAspect.class:23(from BasicAspect.java))
info woven class net.andrewewhite.examples.HelloWorld (from C:\NetBeansProjects\TryAspectJ\src\net\andrewewhite\examples\HelloWorld.java)
info Compiler took 2547ms

When I run my project with -javaagent parameter it works OK. (In NetBeans: Click on project>Properties>Run>VM Options: -javaagent:./dist/lib/aspectjweaver.jar). Otput for code from tutorial:

run:
About to make call to print Hello World
Hello World!
Just made call to print Hello World
BUILD SUCCESSFUL (total time: 0 seconds)

Without agent (VM Options cleared) code runs as if it was without AspectJ:

run:
Hello World!
BUILD SUCCESSFUL (total time: 0 seconds)

Sources:

\TryAspectJ\src\META-INF\aop.xml

<?xml version="1.0" encoding="UTF-8"?>
<aspectj>
    <aspects>
        <aspect name="net.andrewewhite.aspects.BasicAspect" />
    </aspects>
</aspectj>

\TryAspectJ\src\net\andrewewhite\aspects\BasicAspect.java

package net.andrewewhite.aspects;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class BasicAspect {

    @Before("   call(void java.io.PrintStream.println(java.lang.String)) " +
            "&& !within(net.andrewewhite.aspects..*)")
    public void beforePrintlnCall() {
        System.out.println("About to make call to print Hello World");
    }

    @After("    call(void java.io.PrintStream.println(java.lang.String)) " +
           "&&  !within(net.andrewewhite.aspects..*)")
    public void afterPrintlnCall() {
        System.out.println("Just made call to print Hello World");
    }
}

\TryAspectJ\src\net\andrewewhite\examples\HelloWorld.java

package net.andrewewhite.examples;

public class HelloWorld {
    public static void main(String[] argv) {
        System.out.println("Hello World!"); 
    }   
}

\TryAspectJ\build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="TryAspectJ" default="default" basedir=".">
    <description>Builds, tests, and runs the project TryAspectJ.</description>
    <import file="nbproject/build-impl.xml"/>        
<taskdef classpath="lib/aspectj/aspectjtools.jar" resource="org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties"/>         

    <target name="aspectj">
        <echo level="info">--- aspectj (start) ---</echo>
        <iajc destDir="${build.classes.dir}" source="1.6" target="1.6" showweaveinfo="true" verbose="true" >
            <inpath>
                <pathelement location="lib/aspectj/aspectjrt.jar"/>
                <pathelement location="${build.classes.dir}" /> 
            </inpath>
            <sourceroots>
                <pathelement location="${src.dir}"/>
            </sourceroots>
            <classpath>
                <pathelement location="${javac.classpath}"/>
                <pathelement location="${j2ee.platform.classpath}"/>
            </classpath>
        </iajc> 
         <echo level="info">--- aspectj finished ---</echo>
    </target>

<target name="-post-compile" depends="aspectj"></target>
</project>

What do I have to add or change?

like image 849
zacheusz Avatar asked Jul 13 '11 11:07

zacheusz


1 Answers

Found solution. This is the correct build.xml fragment:

<taskdef classpath="lib/aspectjtools.jar" resource="org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties"/>
<target name="aspectj">
    <echo level="info">--- aspectj (start) ---</echo>
    <!-- begin fix classpath for this bug https://issues.apache.org/bugzilla/show_bug.cgi?id=40291 -->
    <condition property="targetos" value="windows" else="unix">
        <os family="windows"/>
    </condition>
    <!-- converting classpath -->
    <pathconvert targetos="${targetos}" property="javac.convertedClasspath" >
        <path path="${javac.classpath}" />                   
    </pathconvert>
    <!-- end fix classpath -->
    <iajc source="1.6" target="1.6" showweaveinfo="true" verbose="true" destdir="${build.classes.dir}"  >
        <inpath>
            <pathelement location="${build.classes.dir}"/>    
        </inpath>
        <classpath>
            <pathelement location="${javac.convertedClasspath}" />
        </classpath>
    </iajc>
    <echo level="info">--- aspectj (finished) ---</echo>
</target>
<target name="-post-compile" depends="aspectj"></target>

Calling java -classpath ./lib/aspectjrt.jar -jar TryAspectJ.jar works OK. What is strange is that from NetBeans (Right clik on project->Run) results are as if they were without AspectJ. I think that NetBeans runs projects from build/classes dir not as jar. But it's not a problem.

like image 162
zacheusz Avatar answered Oct 22 '22 18:10

zacheusz