jdk example that uses one module's loader to load a resource file in another module
Mandy Chung
mandy.chung at oracle.com
Mon Jun 7 18:01:16 PDT 2010
I find an example in the jdk that one module (javac) attempts to load a
resource file in another module (apt) using its module loader. Since
apt is not required by javac, it fails to find the apt's resource bundle.
This indicates that there likely exists legacy applications that may do
the same thing. It works fine in the legacy app. When migrating legacy
apps to modules, the legacy app may require changes like this javac /
apt case.
Details:
com.sun.tools.javac.util.JavacMessages class is in the javac module
while it is also used by the apt tool (apt module) for apt diagnostic.
In com.sun.tools.apt.util.Bark constructor:
// register additional resource bundle for APT messages.
JavacMessages aptMessages = JavacMessages.instance(context);
aptMessages.add("com.sun.tools.apt.resources.apt");
aptDiags = new JCDiagnostic.Factory(aptMessages, "apt");
The com.sun.tools.javac.util.JavacMessages.getBundles() implementation
loads a resource bundle with its own class loader (by calling
java.util.ResourceBundle.getBundle(String). In the legacy world, it has
no problem finding "com.sun.tools.apt.resources.apt" resource bundle
since com.sun.tools.javac.util.JavacMessages class and apt's resource
files are on the class path. In the module world, javac and apt are two
separate modules loaded by two different module loader. So
com.sun.tools.javac.util.JavacMessages class fails to find the
"com.sun.tools.apt.resources.apt" resource bundle that is in the apt module.
As discussed with Jon, to fix this specific javac/apt resource file
issue, the loader of the apt module should be included as part of the
apt context when passing to JavacMessages.
Mandy
More information about the jigsaw-dev
mailing list