Annotation processors and the --processor-module-path

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Annotation processors and the --processor-module-path

Eirik Bjørsnøs
Following up on my example, I updated both the annotation processor and the
example-module to be proper Java 9 modules with module-info.java.

Maven then moved the annotation processor to the --module-path for
example-module's compilation.

That did not seem to work. The processor wasn't loaded at all.

Then I saw that javac also has a --processor-module-path which seemed
promising.

However, my processor was not loaded at all.

First, let us verify that out module is actually a module now:

$ java -p
~/.m2/repository/com/example/annotation-processor/1.0-SNAPSHOT/annotation-processor-1.0-SNAPSHOT.jar
 --list-modules com.example.annotation.processor
com.example.annotation.processor
(file:///$HOME/.m2/repository/com/example/annotation-processor/1.0-SNAPSHOT/annotation-processor-1.0-SNAPSHOT.jar)
  requires mandated java.base
  requires java.compiler
  requires java.xml.bind
  conceals com.example.processor

Then, compile the example module, now using --processor-module-path:

$ javac --processor-module-path
~/.m2/repository/com/example/annotation-processor/1.0-SNAPSHOT/annotation-processor-1.0-SNAPSHOT.jar
-sourcepath src/main/java src/main/java/module-info.java
src/main/java/com/example/module/SomeClass.java -g -nowarn -target 1.9
-source 9

No output, the annotation-processor was not loaded.

Is there some trick needed to load annotation processors as modules?

Eirik.
Reply | Threaded
Open this post in threaded view
|

Re: Annotation processors and the --processor-module-path

Eirik Bjørsnøs
Assuming Java 9 annotaton processors might need to provide
javax.annotation.processing.Processor as a service, I added:

provides javax.annotation.processing.Processor with
com.example.processor.ProcessorUsingJavaXmlBind

to module-info.java.

But still, no dice.

Eirik.

On Mon, Nov 21, 2016 at 1:22 AM, Eirik Bjørsnøs <[hidden email]> wrote:

>
> Following up on my example, I updated both the annotation processor and
> the example-module to be proper Java 9 modules with module-info.java.
>
> Maven then moved the annotation processor to the --module-path for
> example-module's compilation.
>
> That did not seem to work. The processor wasn't loaded at all.
>
> Then I saw that javac also has a --processor-module-path which seemed
> promising.
>
> However, my processor was not loaded at all.
>
> First, let us verify that out module is actually a module now:
>
> $ java -p ~/.m2/repository/com/example/annotation-processor/1.0-
> SNAPSHOT/annotation-processor-1.0-SNAPSHOT.jar  --list-modules
> com.example.annotation.processor
> com.example.annotation.processor (file:///$HOME/.m2/repository/
> com/example/annotation-processor/1.0-SNAPSHOT/annotation-processor-1.0-
> SNAPSHOT.jar)
>   requires mandated java.base
>   requires java.compiler
>   requires java.xml.bind
>   conceals com.example.processor
>
> Then, compile the example module, now using --processor-module-path:
>
> $ javac --processor-module-path ~/.m2/repository/com/example/
> annotation-processor/1.0-SNAPSHOT/annotation-processor-1.0-SNAPSHOT.jar
> -sourcepath src/main/java src/main/java/module-info.java
> src/main/java/com/example/module/SomeClass.java -g -nowarn -target 1.9
> -source 9
>
> No output, the annotation-processor was not loaded.
>
> Is there some trick needed to load annotation processors as modules?
>
> Eirik.
>
Reply | Threaded
Open this post in threaded view
|

Re: Annotation processors and the --processor-module-path

Alan Bateman
In reply to this post by Eirik Bjørsnøs
On 21/11/2016 00:22, Eirik Bjørsnøs wrote:

> :
>
> $ javac --processor-module-path
> ~/.m2/repository/com/example/annotation-processor/1.0-SNAPSHOT/annotation-processor-1.0-SNAPSHOT.jar
> -sourcepath src/main/java src/main/java/module-info.java
> src/main/java/com/example/module/SomeClass.java -g -nowarn -target 1.9
> -source 9
>
> No output, the annotation-processor was not loaded.
>
> Is there some trick needed to load annotation processors as modules?
>
I assume you also need to specify -processor so that javac knows which
annotation processors to run.

-Alan.
Reply | Threaded
Open this post in threaded view
|

Re: Annotation processors and the --processor-module-path

Eirik Bjørsnøs
Alan,

| $ javac --help
| -processor <class1>[,<class2>,<class3>...]
|        Names of the annotation processors to run; bypasses default
discovery process

Why does the "default discovery process" work  with --classpath, but not
with --processor-module-path?

Is it simply an omission, or is this by design?

With the current discovery process (ServiceLoader?), the number of
processors and their class names are implementation details that the
processor author is free to change.

Do we really want to require users to have to deal with and keep track
of the internal workings of their processors?

Seems like a regression to me..

Cheers,
Eirik.


On Mon, Nov 21, 2016 at 9:22 AM, Alan Bateman <[hidden email]>
wrote:

> On 21/11/2016 00:22, Eirik Bjørsnøs wrote:
>
> :
>>
>> $ javac --processor-module-path
>> ~/.m2/repository/com/example/annotation-processor/1.0-SNAPSH
>> OT/annotation-processor-1.0-SNAPSHOT.jar
>> -sourcepath src/main/java src/main/java/module-info.java
>> src/main/java/com/example/module/SomeClass.java -g -nowarn -target 1.9
>> -source 9
>>
>> No output, the annotation-processor was not loaded.
>>
>> Is there some trick needed to load annotation processors as modules?
>>
>> I assume you also need to specify -processor so that javac knows which
> annotation processors to run.
>
> -Alan.
>
Reply | Threaded
Open this post in threaded view
|

Re: Annotation processors and the --processor-module-path

Alan Bateman
On 21/11/2016 09:46, Eirik Bjørsnøs wrote:

> Alan,
>
> | $ javac --help
> | -processor <class1>[,<class2>,<class3>...]
> |        Names of the annotation processors to run; bypasses default
> discovery process
>
> Why does the "default discovery process" work  with --classpath, but not
> with --processor-module-path?
>
> Is it simply an omission, or is this by design?
>
If you have `provides javax.annotation.processing.Processor with ...` in
your module declaration then it should find it. I just checked this
locally and it works as expected. One of your mails said this wasn't
working for you, not immediately obvious why from the mails. So if you
aren't using `provides` then my understanding is that the processor
needs to be specified via -processor but I don't think there is support
there for allowing -processor to specify the processor module, hence not
clear how it could be resolved.

-Alan
Reply | Threaded
Open this post in threaded view
|

Re: Annotation processors and the --processor-module-path

Jan Lahoda
On 21.11.2016 11:03, Alan Bateman wrote:

> On 21/11/2016 09:46, Eirik Bjørsnøs wrote:
>
>> Alan,
>>
>> | $ javac --help
>> | -processor <class1>[,<class2>,<class3>...]
>> |        Names of the annotation processors to run; bypasses default
>> discovery process
>>
>> Why does the "default discovery process" work  with --classpath, but not
>> with --processor-module-path?
>>
>> Is it simply an omission, or is this by design?
>>
> If you have `provides javax.annotation.processing.Processor with ...` in
> your module declaration then it should find it. I just checked this
> locally and it works as expected. One of your mails said this wasn't
> working for you, not immediately obvious why from the mails. So if you
> aren't using `provides` then my understanding is that the processor
> needs to be specified via -processor but I don't think there is support
> there for allowing -processor to specify the processor module, hence not
> clear how it could be resolved.

Currently, AFAIK, the processors on module path need to be registered in
the ServiceLoader. For --processor-module-path, "-processor" can be used
to select processors out of those registered in the ServiceLoader. If
--processor-module-path is used and there is no "-processor", all
processors registered in ServiceLoader are considered, AFAIK. Please
note that (as before) the processors are only run if their
SupportedAnnotationTypes match, etc.

I tried a simple example as well, and with the proper "provides" the
annotation processor was run. Is there a testcase we could try?

Jan

>
> -Alan
Reply | Threaded
Open this post in threaded view
|

Re: Annotation processors and the --processor-module-path

Eirik Bjørsnøs
> Currently, AFAIK, the processors on module path need to be registered in
> the ServiceLoader. For --processor-module-path, "-processor" can be used to
> select processors out of those registered in the ServiceLoader. If
> --processor-module-path is used and there is no "-processor", all
> processors registered in ServiceLoader are considered, AFAIK. Please note
> that (as before) the processors are only run if their
> SupportedAnnotationTypes match, etc.
>
> I tried a simple example as well, and with the proper "provides" the
> annotation processor was run. Is there a testcase we could try?
>


Jan,

I've updated the testcase on Github:

https://github.com/eirbjo/java-xml-bind-processor

How to reproduce:

$ mvn clean install
$ cd example-module

Now (since Maven doesn't know about --processor-module-path):

$ javac -d target/classes --processor-module-path
 ~/.m2/repository/com/example/annotation-processor/1.0-SNAPSHOT/annotation-processor-1.0-SNAPSHOT.jar
-sourcepath src/main/java src/main/java/com/example/module/SomeClass.java

No output - the processor is not applied.

However:

$ javac -d target/classes -classpath
~/.m2/repository/com/example/annotation-processor/1.0-SNAPSHOT/annotation-processor-1.0-SNAPSHOT.jar
-sourcepath src/main/java src/main/java/com/example/module/SomeClass.java
Running ProcessorUsingJavaXmlBind.process
 java.xml? true
 java.xml.bind? false


The processor was now applied.

Note that the test case now has provides in module-info.java _and_  the
ServiceLoader file in META-INF/services. I tried removing the services
file, no change.

Eirik.
Reply | Threaded
Open this post in threaded view
|

Re: Annotation processors and the --processor-module-path

Eirik Bjørsnøs
Found it!

With --processor-module-path, JavacProcessingEnvironment.java:257 throws an
exception when
calling fileManager.getServiceLoader(ANNOTATION_PROCESSOR_MODULE_PATH,
Processor.class)

There's a catch for this exception at JavaCompiler, line 982:

catch (Abort ex) {
       if (devVerbose)
                ex.printStackTrace(System.err);
        }

Since devVerbose is false, this exception is effectively swallowed.

The exception is an ResolutionException: Module java.xml.bind not found,
required by com.example.annotation.processor

So again, I added -J-add-modules=java.xml.bind, and now my annotation
processor is loading.

Given that my annotation processor does require java.xml.bind and I'm using
--processor-module-path, my expectation would be that javac would resolve
that requires for me.

What gives, am I expecting too much?

Bonus question: What is devVerbose, and how can I enable it?

Eirik.


On Mon, Nov 21, 2016 at 11:39 AM, Eirik Bjørsnøs <[hidden email]> wrote:

>
> Currently, AFAIK, the processors on module path need to be registered in
>> the ServiceLoader. For --processor-module-path, "-processor" can be used to
>> select processors out of those registered in the ServiceLoader. If
>> --processor-module-path is used and there is no "-processor", all
>> processors registered in ServiceLoader are considered, AFAIK. Please note
>> that (as before) the processors are only run if their
>> SupportedAnnotationTypes match, etc.
>>
>> I tried a simple example as well, and with the proper "provides" the
>> annotation processor was run. Is there a testcase we could try?
>>
>
>
> Jan,
>
> I've updated the testcase on Github:
>
> https://github.com/eirbjo/java-xml-bind-processor
>
> How to reproduce:
>
> $ mvn clean install
> $ cd example-module
>
> Now (since Maven doesn't know about --processor-module-path):
>
> $ javac -d target/classes --processor-module-path
>  ~/.m2/repository/com/example/annotation-processor/1.0-
> SNAPSHOT/annotation-processor-1.0-SNAPSHOT.jar -sourcepath src/main/java
> src/main/java/com/example/module/SomeClass.java
>
> No output - the processor is not applied.
>
> However:
>
> $ javac -d target/classes -classpath ~/.m2/repository/com/example/
> annotation-processor/1.0-SNAPSHOT/annotation-processor-1.0-SNAPSHOT.jar
> -sourcepath src/main/java src/main/java/com/example/module/SomeClass.java
> Running ProcessorUsingJavaXmlBind.process
>  java.xml? true
>  java.xml.bind? false
>
>
> The processor was now applied.
>
> Note that the test case now has provides in module-info.java _and_  the
> ServiceLoader file in META-INF/services. I tried removing the services
> file, no change.
>
> Eirik.
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Annotation processors and the --processor-module-path

Alan Bateman
On 22/11/2016 10:33, Eirik Bjørsnøs wrote:

> :
>
> So again, I added -J-add-modules=java.xml.bind, and now my annotation
> processor is loading.
>
> Given that my annotation processor does require java.xml.bind and I'm using
> --processor-module-path, my expectation would be that javac would resolve
> that requires for me.
>
> What gives, am I expecting too much?
When you deploy annotation processors on the --processor-module-path
then javac needs to resolve the annotation processor modules as part of
creating the dynamic configuration. The issue with the dependency on
java.xml.bind is that it's not in the boot layer and it's also not
observable on the processor module path. There is no support at this
time for augmenting the boot layer once it is created, also javac does
not attempt to resolve these modules from the original module path and
system image that were used to start the VM. So for now at least,
running with -J--add-modules=java.xml.bind is the only way to get this
to work.

-Alan
Reply | Threaded
Open this post in threaded view
|

Re: Annotation processors and the --processor-module-path

cowwoc
Administrator
Alan Bateman wrote

> On 22/11/2016 10:33, Eirik Bjørsnøs wrote:
>
>> :
>>
>> So again, I added -J-add-modules=java.xml.bind, and now my annotation
>> processor is loading.
>>
>> Given that my annotation processor does require java.xml.bind and I'm
>> using
>> --processor-module-path, my expectation would be that javac would resolve
>> that requires for me.
>>
>> What gives, am I expecting too much?
> When you deploy annotation processors on the --processor-module-path
> then javac needs to resolve the annotation processor modules as part of
> creating the dynamic configuration. The issue with the dependency on
> java.xml.bind is that it's not in the boot layer and it's also not
> observable on the processor module path. There is no support at this
> time for augmenting the boot layer once it is created, also javac does
> not attempt to resolve these modules from the original module path and
> system image that were used to start the VM. So for now at least,
> running with -J--add-modules=java.xml.bind is the only way to get this
> to work.
>
> -Alan

Hi Alan,

Seeing as `--add-modules=java.xml.bind` no longer works in Java 11 what is
the updated recommendation?

Thanks,
Gili



--
Sent from: http://jigsaw-dev.1059479.n5.nabble.com/
Reply | Threaded
Open this post in threaded view
|

Re: Annotation processors and the --processor-module-path

Alex Buckley
On 1/30/2019 9:40 AM, cowwoc wrote:
> Seeing as `--add-modules=java.xml.bind` no longer works in Java 11 what is
> the updated recommendation?

The Java SE Platform no longer includes JAXB. JEP 320 discusses where to
find standalone versions of JAXB (http://openjdk.java.net/jeps/320).

StackOverflow also has a question on this topic, with many suggestions
and more than 250,000 views
(https://stackoverflow.com/questions/43574426/how-to-resolve-java-lang-noclassdeffounderror-javax-xml-bind-jaxbexception-in-j).

Alex
Reply | Threaded
Open this post in threaded view
|

Re: Annotation processors and the --processor-module-path

cowwoc
Administrator
Sorry, I asked the wrong question (similar to the above but different).

What I was trying to say is that if you stick jmh on the module path, the
annotation processor stops working:
https://stackoverflow.com/q/53927874/14731

If I explicitly add the JAR file to the annotation processor path then it
works. Why do I need to explicitly add an entry to the annotation processor
path when this was working file on the classpath? Is there another way to
make this work?

Thank you,
Gili



--
Sent from: http://jigsaw-dev.1059479.n5.nabble.com/