Post by Alan Bateman Post by MichaÅ Zegan
I know that modules/module declarations can be annotated. Contrary to
other elements from packages downwards, java has a way to read module
descriptions before creating modules themselves (in fact it is a
required step). I am wondering, why doesn't ModuleDescriptor
contain/parse module annotations from class file? That way the
module-info.class has to be read twice, once for module descriptor, once
for annotations. In addition, if I would like to load annotations of a
module x before it is instantiated (like introspection), I would have to
still use a library like asm or similar.
This is to avoid bloating the API and also to avoid overlapping with
the javax.lang.model API. So yes, if you are scanning observable
modules, maybe to select which modules to resolve based on
annotations, then you will need ASM or something to help you parse the
class files. It's the same thing for tools that scan classes or
elements for annotations.
In addition to what Alan says, I may add that I have tried to prototype
an API that would allow reading of annotations at the ModuleDescriptor
time, before Module is instantiated. I had the following problem: the
API can't be the same as implemented in existing AnnotatedElement
classes (Class, Method, Field, Constructor, Module, ...). Why? Because
of chicken-egg problem. The annotation types used in a particular
module-info.java may be declared in the module itself. For constructing
annotation objects (which is a necessity for AnnotatedElement API), the
annotation types have to be loaded 1st and if they are declared in a
module, the Module has to be instantiated first.
So this would technically be possible only with an entirely different
API than what we have today in java.lang[.relfect]. Something like the
That said, I wonder what is the status of JEP 119 today. If it was
updated to support module(s) then I guess it uses the instantiated
Module(s) to get the job done.
OTOH there's java compiler API that implements javax.lang.model too. I
haven't investigated it but it might be possible to employ it to parse
class files and provide you with javax.lang.model objects including
annotations. Something to try...