0

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.

kriegaex
  • 63,017
  • 15
  • 111
  • 202
Marco Luzzara
  • 5,540
  • 3
  • 16
  • 42

1 Answers1

0

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.

Marco Luzzara
  • 5,540
  • 3
  • 16
  • 42
  • 1
    Thanks for raising this question. Currently we have a [bug when parsing the `--module-source-path` option](https://github.com/eclipse/org.aspectj/issues/118) in AJC (which is derived from ECJ, the Eclipse Java Compiler), causing the compiler to core-dump. This can probably be fixed, but please note that even so, ECJ currently has a [problem handling module source paths with `*`](https://bugs.eclipse.org/bugs/show_bug.cgi?id=578306), i.e. if you want to use multi-module compilation mode, you need to use the default directory layout with modules directly below the specified base directory. – kriegaex Jan 21 '22 at 05:16
  • May I ask why you want to use multi-module compilation mode? In your described case, I see no benefit. If you are expecting `--module-source-path` to enable you to not having to specify all the files to be compiled anymore, I have to disappoint you. This is neither the case with Javac nor with ECJ. You still have to list all source files. – kriegaex Jan 21 '22 at 05:21
  • @kriegaex Thank you for explaining the current situation. I am just exploring AspectJ and wanted to test if it entirely supports module. *This is neither the case with Javac nor with ECJ*, with javac something like this `javac --module-source-path . -d output-folder -m mod1(,mod)*` should work as far as I know. Why do you say the opposite? – Marco Luzzara Jan 21 '22 at 08:56
  • Maybe because I am a total JMS noob and never used modules in any of my projects before. I had no idea that I have to combine those two CLI options. I am going to check later when I am near a computer again, being on the road just now. Sorry if I might have said something stupid. You definitely know more about modules than I do. – kriegaex Jan 21 '22 at 10:26
  • Actually, I have learned that recently, we are probably not on different levels (I am a noob too). I read the book *The Java Module System by Nicolai Parlog* and from the paragraph 4.3.4 he explains pros and cons of `--module-source-path` combined with `--module`. – Marco Luzzara Jan 21 '22 at 10:48
  • 1
    That works nicely with Javac, but there is no `--module (,)*` or `-m (,)*` option in ECJ. Therefore, it also does not exist in ACJ. I.e., presently you need to compile each module separately and really specify all source files literally, with `$(find ...)` or with a source list file. – kriegaex Jan 21 '22 at 11:45
  • Thank you again for looking into this. – Marco Luzzara Jan 21 '22 at 11:47