Search code examples
javamavenaspectjaspectj-maven-plugin

Aspect in java not being weaved correctly


I have the following method:

public class MonitorInterface {

    // this is the method you have to call to trigger the monitor
    public static void event(String eventName, HashMap params) { 
        System.out.println("Entering event method");
    }

}

and the following aspect :

package aspects;

import com.path.for.MonitorInterface;
import java.util.HashMap;
public aspect _asp_connector0 {
    private pointcut eventP():
        execution(public static void event(String, HashMap));

    before(): eventP(){
        System.out.println("Test pointcut weave");
    }
}

which basically adds a Sys.out.print to the previous method

As for the pom.xml I am using the following plugins mainly:

 <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.1</version>
    <configuration>
        <source>1.7</source>
        <target>1.7</target>
        <useIncrementalCompilation>false</useIncrementalCompilation>
    </configuration>
</plugin>
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.7</version>
    <configuration>
        <source>1.7</source>
        <target>1.7</target>
        <complianceLevel>1.7</complianceLevel>
        <Xlint>ignore</Xlint>
        <encoding>UTF-8</encoding>
        <verbose>true</verbose>
        <showWeaveInfo>true</showWeaveInfo>
        <sources>
            <source>
            <basedir>src/main/resources</basedir>
            <includes>
                <include>**/_asp_connector0.aj</include>
            </includes>
            <excludes>
                <exclude>**/*.java</exclude>
                    <exclude>**/*.lrv</exclude>
                    <exclude>**/*.txt</exclude>
                </excludes>
            </source>
        </sources>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjtools</artifactId>
            <version>${aspectj.version}</version>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            <goals>
                <goal>compile</goal>
                <goal>test-compile</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.3.2</version>
    <configuration>
         <mainClass>path.to.main.Example</mainClass>
    </configuration>
</plugin>
<plugin>
    <groupId>org.dstovall</groupId>
    <artifactId>onejar-maven-plugin</artifactId>
    <version>1.4.4</version>
    <executions>
       <execution>
          <configuration>
            <onejarVersion>0.96</onejarVersion>
            <mainClass>path.to.main.Example</mainClass>
            <attachToBuild>true</attachToBuild>
          </configuration>
          <goals>
              <goal>one-jar</goal>
          </goals>
       </execution>
  </executions>
</plugin>

When i compile (using mvn clean install) and run the generated jar file however, I am never getting the weaved code in the desired method.

Alternatively I tried to run them using the ajc compiler manually as follows:

ajc -outjar testMain.jar -target 1.5 -source 1.5 src\main\java\path\to\Example.java src\main\java\path\to\MonitorInterface.java
set CLASSPATH=%CLASSPATH%;.\testMain.jar
ajc -outjar testAsp.jar -target 1.5 -source 1.5 src\main\resources\aspects\_asp_connector0.aj
set CLASSPATH=%CLASSPATH%;.\testAsp.jar
aj path.to.Example

This results in the warning

_asp_connector0.aj:12 [warning] advice defined in aspects._asp_connector0 has not been applied [Xlint:adviceDidNotMatch]

but the new println still doesn't appear

How can I solve this, or at least debug this more efficiently?

Note: With maven, the class file for the aspect is being generated, the code is just not being weaved into the actual method


Solution

  • Since your aspect works in a plain java AspectJ project in Eclipse - at least for me - the problem must be your weaving configuration. One thing sticks out immediately:

    <basedir>src/main/resources</basedir>
    

    Why are you trying to apply your aspect to your resources and not your code(src/main/java as default in maven projects)? Changing that line and removing the includes/excludes, I get the following from the aspectj-maven-plugin:

    [INFO] Join point 'method-execution(void program.MonitorInterface.event(java.lang.String, java.util.HashMap))' in Type 'program.MonitorInterface' (MonitorInterface.java:7) advised by before advice from 'aspects._asp_connector0' (_asp_connector0.aj:11)
    

    So the advice is used and applied.

    Is there a reason for you to specify these specific includes/excludes?

    <exclude>**/*.java</exclude>
    

    That alone will make it pretty much impossible to advise any Java-code, because you are excluding all Java-code from compile time weaving. I'd suggest removing all includes/excludes and only adding them piece by piece IFF you have a specific problem that can be solved by includes/excludes. For the usecase you describe here, they are entirely unnecessary and actually problematic in their current configuration.