AspectJ (ajc) with Modules

502 views Asked by At

I am using AspectJ 1.9.7 and I am trying to compile a modular application with ajc. Let's suppose I have a single module called test and the following tree:

.
└── test
    ├── aspectj
    │   ├── Main.java
    │   └── TestAspect.aj
    └── module-info.java

With javac the command I would use to compile it is:

javac --module-source-path . -d ../bin -m test

And everything works fine, of course without the weaving process. Instead, if I try to run the same line with ajc (ajc --module-source-path . -d ../bin -m test), the output is:

[error] build config error: dir arg not permitted: test
    
[error] unrecognized single argument: "-m"
    
[error] no sources specified
    

3 errors

I guess at this point that the -m option is not supported. I have found this question, where a user says that it is possible to specify the option -usejavac to use javac in the compilation process but, at least for me, does not work and the option is not even recognized.

1) How to use --module-source-path with ajc?


Assuming --module-source-path is not supported, I have opted for the --module-path option. Again, with javac, everything ok:

javac -d bin --module-path test $(find . -name *.java)

But switching to ajc (ajc -d bin --module-path test $(find . -name *.java -o -name *.aj)), the output is this:

/home/maluz/Desktop/OneDrive/First Semester/TSP/labs/lab2/Test/test/module-info.java:1 [error] Syntax error on token "module", aspect expected
module test {
^^^^^

1 error

Same output when I remove --module-path.

2) Is there a way to compile a module with ajc?

The alternative would be something like:

# I compile every *.java and *.aj except module-info.java with ajc
$ ajc -d bin $(find . \( -name *.java -o -name *.aj \) -and \( -not -name *module-info.java \))

# I compile module-info.java separately with javac
$ javac --module-path test -d bin/ test/module-info.java

It works but there must be a way to do it with ajc, given that --module-path and --module-source-path are documented under the Module compilation options of ajc man. I am not sure this is relevant, but I am using openjdk 15.0.2 2021-01-19.

1

There are 1 answers

7
Marco Luzzara On

Thanks to this blog, I found a partial solution. The first question is still unanswered because the --module option is unrecognized. Nonetheless, it seems possible to compile a module with ajc:

ajc -d bin --release 15 --module-path path/to/aspectjrt.jar $$(find . -name *.java -o -name *.aj)

And a small change in the module-info.java:

module test {
    exports aspectj;
    requires org.aspectj.runtime;
}

As for what I have understood, you need to specify the java version your application is compatible with: this is done with --release option.

Then, in order to use AspectJ classes and runtime types, you need to import the aspectjrt.jar into the module path and require the automatic module name in the module-info.java. This jar is placed by default in $HOME/aspectj${version}/lib/aspectjrt.jar.


I am not accepting my answer because the --module-source-path with ajc is still a mistery to me.