Eliminate cyclic deps was: JDK support for VM to read classes from modules in a module library

Jaroslav Tulach jaroslav.tulach at oracle.com
Wed May 16 12:44:21 PDT 2012


Dne St 16. května 2012 15:07:57, Jesse Glick napsal(a):
> On 05/16/2012 01:42 PM, Alan Bateman wrote:
> > [...In Properties] the workaround is to use the loader for the base module
> > to load XMLUtils. Given that XMLUtil is currently in the jaxp module (and
> > I know this isn't quite right either as JAXP is a standalone technology
> > etc.) then I would think that the Properties code will need to do a
> > Class.forName on an exported javax.xml type and use that loader (or
> > alternative use jdk.jaxp).
> You have a module system now, why not clean this up and get rid of the
> reflection (and cyclic dependency)?

Right. Cyclic dependencies are only needed if a class from depending module 
sticks out from a public API. Otherwise use ServiceLoader and eliminate 
cycles. Btw. Jesse's code below is a modern variant of my 2009 suggestion:

http://source.apidesign.org/hg/jdk/file/modularize/src/share/classes/java/util/Properties.java#l933
http://source.apidesign.org/hg/jdk/file/modularize/src/share/classes/sun/util/xml/PropertiesXMLUtils.java
http://source.apidesign.org/hg/jdk/file/modularize/src/share/classes/sun/util/xml/DefaultPropertiesXMLUtils.java

-jt

> 
> module jdk.base {
>    ...
>    view sun.jaxp.bridge {
>      permits jdk.jaxp;
>      export sun.misc;
>    }
>    requires optional service sun.misc.XMLUtils;
> }
> package sun.misc;
> public interface XMLUtils {
>    void load(Properties props, InputStream in) throws IOException,
> InvalidPropertiesFormatException; void save(Properties props, OutputStream
> os, String comment, String encoding) throws IOException; }
> package java.util;
> public class Properties {
>    ...
>    @RequireOptionalModule("jdk.jaxp")
>    public synchronized void loadFromXML(InputStream in) throws IOException,
> InvalidPropertiesFormatException { try {
>       
> ServiceLoader.loadInstalled(XMLUtils.class).iterator().next().load(this,
> in); } catch (NoSuchElementException x) {
>        throw new ModuleNotPresentException("jdk.jaxp missing", x);
>      }
>    }
>    @RequireOptionalModule("jdk.jaxp")
>    public void storeToXML(OutputStream os, String comment, String encoding)
> throws IOException { try {
>       
> ServiceLoader.loadInstalled(XMLUtils.class).iterator().next().save(this,
> os, comment, encoding); } catch (NoSuchElementException x) {
>        throw new ModuleNotPresentException("jdk.jaxp missing", x);
>      }
>    }
> }
> module jdk.jaxp {
>    ...
>    requires sun.jaxp.bridge;
>    provides service sun.misc.XMLUtils with ...;
> }



More information about the jigsaw-dev mailing list