Hi everyone,

I'm a novice to AspectJ and I've been struggling for the past few days
trying to weave an aspect at compile time with aspectj-maven-plugin. I
could do it with a simple project, but not in my main project. From what I
see in the maven build log, the pointcut is matched and advised by my
aspect, but when I run the program the aspect code is not executed. I've
decompiled my class and found that the closure class was created but never
called from anywhere.

This is my project structure;

MainProject
    |
    +--- pom.xml
    |
    +--- ProjectA
    |    |
    |    +--- InterfaceA.java
    |    +--- ClassA.java
    |    +--- pom.xml
    |
    +--- AspectProject
        |
        +--- MyAspect.java
        +--- pom.xml

-----------------------------------------
MainProject/pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"; xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance";
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd";>
<modelVersion>4.0.0</modelVersion>
<groupId>myGroup</groupId>
<artifactId>MainProject</artifactId>
<version>1.0.5</version>
<packaging>pom</packaging>
<name>Main Project</name>
<modules>
<module>ProjectA</module>
<module>AspectProject</module>
</modules>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.2</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>myGroup</groupId>
<artifactId>AspectProject</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<source>1.7</source>
<target>1.7</target>
<complianceLevel>1.7</complianceLevel>
<showWeaveInfo>true</showWeaveInfo>
<verbose>true</verbose>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>C:\Output\</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>


<pluginManagement>

<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<source>1.7</source>
<target>1.7</target>
<complianceLevel>1.7</complianceLevel>
<showWeaveInfo>true</showWeaveInfo>
<verbose>true</verbose>
</configuration>
</plugin>
<!--This plugin's configuration is used to store Eclipse m2e settings
only. It has no influence on the Maven build itself. -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>
org.codehaus.mojo
</groupId>
<artifactId>
aspectj-maven-plugin
</artifactId>
<versionRange>
[1.3,)
</versionRange>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

-----------------------------------------
ProjectA/pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"; xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance";
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd";>
<modelVersion>4.0.0</modelVersion>

<artifactId>ProjectA</artifactId>
<name>Project A</name>

<dependencies>
<dependency>
<groupId>myGroup</groupId>
<artifactId>AspectProject</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<configuration>
<aspectLibraries>
<aspectLibrary>
<groupId>myGroup</groupId>
<artifactId>AspectProject</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<outputDirectory>C:\Output\</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>

<parent>
<groupId>myGroup</groupId>
<artifactId>MainProject</artifactId>
<version>1.0.5</version>
<relativePath>../</relativePath>
</parent>

</project>

-----------------------------------------
ProjectA/InterfaceA.java

public interface InterfaceA {
    myOperation (String arg) throws Exception;
}

-----------------------------------------
ProjectA/ClassA.java

public class ClassA extends InterfaceA {
    @Override
    public Object myOperation (String arg) throws Exception {
        //...
    }
}

-----------------------------------------
AspectProject/pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"; xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance";
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd";>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>myGroup</groupId>
<artifactId>MainProject</artifactId>
<version>1.0.5</version>
<relativePath>../</relativePath>
</parent>

<artifactId>AspectProject</artifactId>

<name>Aspects Project</name>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<outputDirectory>C:\Output\</outputDirectory>
</configuration>
</plugin>
</plugins>

</build>

<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.2</version>
</dependency>
</dependencies>
</project>

-----------------------------------------
AspectProject/Aspect.java

@Aspect
public class Aspect {

    private void initializeObjects() {
        //...
    }

    private void finalizeObjects() {
        //...
    }

    @Around("execution(* myOperation(..))")
    public Object intercept(ProceedingJoinPoint pjp) throws Throwable {
        initializeObjects();
        try {
            Object ret = pjp.proceed();
            return ret;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            finalizeObjects();
        }
    }
}

This is the output when I run maven install in AspectProject:

[INFO] Scanning for projects...
[INFO]
[INFO]
------------------------------------------------------------------------
[INFO] Building AspectProject 1.0.5
[INFO]
------------------------------------------------------------------------
[INFO]
[INFO] --- aspectj-maven-plugin:1.7:compile (default) @ AspectProject ---
[INFO] Showing AJC message detail for messages of types: [error, warning,
fail]
[WARNING] advice defined in Aspect has not been applied
[Xlint:adviceDidNotMatch]
        C:\Workspace\MainProject\AspectProject\Aspect.java:##

[INFO]
[INFO] --- aspectj-maven-plugin:1.7:test-compile (default) @ AspectProject
---
[WARNING] No sources found skipping aspectJ compile

[INFO] MORE MAVEN STUFF FROM OTHER PLUGINS
[INFO]
------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO]
------------------------------------------------------------------------
[INFO] Total time: 4.050 s
[INFO] Finished at: 2015-09-16T08:37:17-03:00
[INFO] Final Memory: 24M/228M
[INFO]
------------------------------------------------------------------------

This is the output when I run maven install in ProjectA:

[INFO] Scanning for projects...
[INFO]
[INFO]
------------------------------------------------------------------------
[INFO] Building Project A 1.0.5
[INFO]
------------------------------------------------------------------------
[INFO]
[INFO] --- aspectj-maven-plugin:1.7:compile (default) @ ProjectA ---
[INFO] Showing AJC message detail for messages of types: [error, warning,
fail]
[INFO] Join point 'method-execution(Object ClassA.myOperation(String arg))'
in Type 'ClassA' (ClassA.java:###) advised by around advice from 'Aspect'
(AspectProject-1.0.5.jar!Aspect.class(from Aspect.java))
[INFO]
[INFO] --- aspectj-maven-plugin:1.7:test-compile (default) @ ItemNeuronSAD
---
[INFO] Showing AJC message detail for messages of types: [error, warning,
fail]
[WARNING] advice defined in Aspect has not been applied
[Xlint:adviceDidNotMatch]

C:\Path\to\Repo\myGroup\AspectProject\1.0.5\AspectProject-1.0.5.jar!\Aspect.class:0

[INFO] MORE MAVEN STUFF FROM OTHER PLUGINS
[INFO]
------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO]
------------------------------------------------------------------------
[INFO] Total time: 11.390 s
[INFO] Finished at: 2015-09-16T08:39:09-03:00
[INFO] Final Memory: 36M/512M
[INFO]
------------------------------------------------------------------------

When I inspect my ProjectA.jar file with JD (http://jd.benow.ca/), I found
that a new class was created for the closure (ClassA$AjcClosure1.class) and
it calls a "ClassA.myOperation_aroundBody0(...)". However, there's no
myOperation_aroundBody0 method woven into ClassA.

I would be realy grateful if anyone could point me on what is going on and
how I may fix this so the code is woven in the class.

Thanks,
Homero.
_______________________________________________
aspectj-users mailing list
aspectj-users@eclipse.org
To change your delivery options, retrieve your password, or unsubscribe from 
this list, visit
https://dev.eclipse.org/mailman/listinfo/aspectj-users

Reply via email to