< prev index next >

src/java.base/share/classes/java/lang/module/ModuleReference.java

Print this page




  27 
  28 import java.io.IOException;
  29 import java.net.URI;
  30 import java.util.Objects;
  31 import java.util.Optional;
  32 
  33 
  34 /**
  35  * A reference to a module's content.
  36  *
  37  * <p> A module reference is a concrete implementation of this class that
  38  * implements the abstract methods defined by this class. It contains the
  39  * module's descriptor and its location, if known.  It also has the ability to
  40  * create a {@link ModuleReader} in order to access the module's content, which
  41  * may be inside the Java run-time system itself or in an artifact such as a
  42  * modular JAR file.
  43  *
  44  * @see ModuleFinder
  45  * @see ModuleReader
  46  * @since 9

  47  */
  48 
  49 public abstract class ModuleReference {
  50 
  51     private final ModuleDescriptor descriptor;
  52     private final URI location;
  53 
  54     /**
  55      * Constructs a new instance of this class.
  56      *
  57      * @param descriptor
  58      *        The module descriptor
  59      * @param location
  60      *        The module location or {@code null} if not known
  61      */
  62     protected ModuleReference(ModuleDescriptor descriptor, URI location) {
  63         this.descriptor = Objects.requireNonNull(descriptor);
  64         this.location = location;
  65     }
  66 
  67     /**
  68      * Returns the module descriptor.
  69      *
  70      * @return The module descriptor
  71      */
  72     public final ModuleDescriptor descriptor() {
  73         return descriptor;
  74     }
  75 
  76     /**
  77      * Returns the location of this module's content, if known.
  78      *
  79      * <p> This URI, when present, is used as the {@linkplain
  80      * java.security.CodeSource#getLocation location} value of a {@link
  81      * java.security.CodeSource CodeSource} so that a module's classes can be
  82      * granted specific permissions when loaded by a {@link
  83      * java.security.SecureClassLoader SecureClassLoader}.
  84      *
  85      * @return The location or an empty {@code Optional} if not known
  86      */
  87     public final Optional<URI> location() {
  88         return Optional.ofNullable(location);
  89     }
  90 
  91     /**
  92      * Opens the module content for reading.
  93      *
  94      * @return A {@code ModuleReader} to read the module
  95      *
  96      * @throws IOException
  97      *         If an I/O error occurs
  98      * @throws SecurityException
  99      *         If denied by the security manager


  27 
  28 import java.io.IOException;
  29 import java.net.URI;
  30 import java.util.Objects;
  31 import java.util.Optional;
  32 
  33 
  34 /**
  35  * A reference to a module's content.
  36  *
  37  * <p> A module reference is a concrete implementation of this class that
  38  * implements the abstract methods defined by this class. It contains the
  39  * module's descriptor and its location, if known.  It also has the ability to
  40  * create a {@link ModuleReader} in order to access the module's content, which
  41  * may be inside the Java run-time system itself or in an artifact such as a
  42  * modular JAR file.
  43  *
  44  * @see ModuleFinder
  45  * @see ModuleReader
  46  * @since 9
  47  * @spec JPMS
  48  */
  49 
  50 public abstract class ModuleReference {
  51 
  52     private final ModuleDescriptor descriptor;
  53     private final URI location;
  54 
  55     /**
  56      * Constructs a new instance of this class.
  57      *
  58      * @param descriptor
  59      *        The module descriptor
  60      * @param location
  61      *        The module location or {@code null} if not known
  62      */
  63     protected ModuleReference(ModuleDescriptor descriptor, URI location) {
  64         this.descriptor = Objects.requireNonNull(descriptor);
  65         this.location = location;
  66     }
  67 
  68     /**
  69      * Returns the module descriptor.
  70      *
  71      * @return The module descriptor
  72      */
  73     public final ModuleDescriptor descriptor() {
  74         return descriptor;
  75     }
  76 
  77     /**
  78      * Returns the location of this module's content, if known.
  79      *
  80      * <p> This URI, when present, can be used as the {@linkplain
  81      * java.security.CodeSource#getLocation location} value of a {@link
  82      * java.security.CodeSource CodeSource} so that a module's classes can be
  83      * granted specific permissions when loaded by a {@link
  84      * java.security.SecureClassLoader SecureClassLoader}.
  85      *
  86      * @return The location or an empty {@code Optional} if not known
  87      */
  88     public final Optional<URI> location() {
  89         return Optional.ofNullable(location);
  90     }
  91 
  92     /**
  93      * Opens the module content for reading.
  94      *
  95      * @return A {@code ModuleReader} to read the module
  96      *
  97      * @throws IOException
  98      *         If an I/O error occurs
  99      * @throws SecurityException
 100      *         If denied by the security manager
< prev index next >