AspectJ: How to get pointcuts to advise classes located in other projects

4.3k views Asked by At

This should be simple.

Question
How do you get a pointcut in one project to advise the code/classes within another project?


Context
I'm working in eclipse with two projects. For ease of explanation, let's call one science project and the other math project and say the science project relies on the math project and I'm developing in both projects, concurrently. The math project is a core product, in production, and life will be easier if I don't modify the code much.

Currently, I'm debugging the interaction between these two projects. To assist with that, I'm writing an Aspect (within the science project) to log key information as the math code (and science code) executes.


Example
I running a simple example aspect along the lines of:

package org.science.example;

public aspect ScientificLog {
    public pointcut testCut() : execution (public * *.*(..));
    before() : testCut() {
        //do stuff
    }
}


Problem
The problem is, no matter what pointcut I create, it only advises code from the science project. No classes from org.math.example are crosscut, AT ALL!

I tried adding the math project to the inpath of the science project by going to proect properties > AspectJ Build > Inpath and clicking add project and choosing the math project. That didn't work but it seems like I need to do something along those lines.

Thanks, in advance, for any suggestions...

-gMale


EDIT 1:
Since writing this, I've noticed the project is giving the following error:

Caused by: org.aspectj.weaver.BCException: Unable to continue, this version of AspectJ
supports classes built with weaver version 6.0 but the class
com.our.project.adapter.GenericMessagingAdapter is version 7.0
when batch building BuildConfig[null] #Files=52 AopXmls=#0

So maybe this is setup properly and the error is more subtle. BTW, the class mentioned is from the "science project," so to speak. This happens even after I clean the project. I'm currently googling this error...


EDIT 2:
I found the solution to the error above in comment #5 here

The problem is the maven-aspectj-plugin's pom file declares a dependency on aspectjtools version 1.6.7. So, when configuring the plugin, that transient dependency has to be modified. Here's the related code snippet for the pom file that fixes the problem by specifying version 1.6.9 instead of 1.6.7:

                    <plugin>
                            <groupId>org.codehaus.mojo</groupId>
                            <artifactId>aspectj-maven-plugin</artifactId>
                            <version>1.3</version>
                            <dependencies>
                                <dependency>
                                    <groupId>org.aspectj</groupId>
                                    <artifactId>aspectjtools</artifactId>
                                    <version>1.6.9</version>
                                </dependency>
                            </dependencies>
                            <configuration>
                                    <source>1.6</source>
                                    <target>1.6</target>
                            </configuration>
                            <executions>
                                    <execution>
                                            <goals>
                                                    <goal>compile</goal>
                                                    <goal>test-compile</goal>
                                            </goals>
                                    </execution>
                            </executions>
                    </plugin>
2

There are 2 answers

1
Andrew Eisenberg On BEST ANSWER

Your second problem is unrelated to the first. It is saying that com.our.project.adapter.GenericMessagingAdapter was originally compiled and woven against a new version of AspectJ but is being used to binary weave against an older version of AspectJ.

This is essentially the same problem as when you try to run Java classes compiled under 1.6 on a 1.5 VM.

The version number was revved up for the release of AspectJ 1.6.8 (I think, or maybe it was 1.6.7).

The solution is to make sure you are using the latest version of AspectJ for all of your projects (eg- 1.6.9, or dev builds of 1.6.10).

1
Andrew Eisenberg On

When you add Math project to the in path of science project, all of math project's code is sent through the aspectj weaver and properly woven. The results of that weave are written to science project's output folder (not Math project's). So, if you were to look in science project's bin folder, you should see the woven classes there.

If you wanted to keep the in path files separate from the regular files, you can specify an inpath out folder. This folder should also be added to the class path as a binary folder. Also, this folder should be placed above the project dependency to Math project in the "Export and Order" tab of the Java build page for Science project.

Finally, if you run the main class from Science project, rather than from Math project, you will be executing the woven code.