< prev index next >

src/java.base/share/classes/java/lang/Module.java

Print this page

  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.io.IOException;
  29 import java.io.InputStream;
  30 import java.lang.annotation.Annotation;


  31 import java.lang.module.Configuration;
  32 import java.lang.module.ModuleReference;
  33 import java.lang.module.ModuleDescriptor;
  34 import java.lang.module.ModuleDescriptor.Exports;
  35 import java.lang.module.ModuleDescriptor.Opens;
  36 import java.lang.module.ModuleDescriptor.Version;
  37 import java.lang.module.ResolvedModule;
  38 import java.lang.reflect.AnnotatedElement;
  39 import java.net.URI;
  40 import java.net.URL;
  41 import java.security.AccessController;
  42 import java.security.PrivilegedAction;
  43 import java.util.HashMap;
  44 import java.util.HashSet;
  45 import java.util.List;
  46 import java.util.Map;
  47 import java.util.Objects;
  48 import java.util.Optional;
  49 import java.util.Set;
  50 import java.util.concurrent.ConcurrentHashMap;
  51 import java.util.function.Function;
  52 import java.util.stream.Collectors;
  53 import java.util.stream.Stream;
  54 
  55 import jdk.internal.loader.BuiltinClassLoader;
  56 import jdk.internal.loader.BootLoader;
  57 import jdk.internal.loader.ClassLoaders;
  58 import jdk.internal.misc.CDS;
  59 import jdk.internal.module.ModuleLoaderMap;
  60 import jdk.internal.module.ServicesCatalog;
  61 import jdk.internal.module.Resources;
  62 import jdk.internal.org.objectweb.asm.AnnotationVisitor;
  63 import jdk.internal.org.objectweb.asm.Attribute;
  64 import jdk.internal.org.objectweb.asm.ClassReader;
  65 import jdk.internal.org.objectweb.asm.ClassVisitor;
  66 import jdk.internal.org.objectweb.asm.ClassWriter;
  67 import jdk.internal.org.objectweb.asm.ModuleVisitor;
  68 import jdk.internal.org.objectweb.asm.Opcodes;
  69 import jdk.internal.reflect.CallerSensitive;
  70 import jdk.internal.reflect.Reflection;

  71 import sun.security.util.SecurityConstants;
  72 
  73 /**
  74  * Represents a run-time module, either {@link #isNamed() named} or unnamed.
  75  *
  76  * <p> Named modules have a {@link #getName() name} and are constructed by the
  77  * Java Virtual Machine when a graph of modules is defined to the Java virtual
  78  * machine to create a {@linkplain ModuleLayer module layer}. </p>
  79  *
  80  * <p> An unnamed module does not have a name. There is an unnamed module for
  81  * each {@link ClassLoader ClassLoader}, obtained by invoking its {@link
  82  * ClassLoader#getUnnamedModule() getUnnamedModule} method. All types that are
  83  * not in a named module are members of their defining class loader's unnamed
  84  * module. </p>
  85  *
  86  * <p> The package names that are parameters or returned by methods defined in
  87  * this class are the fully-qualified names of the packages as defined in
  88  * section {@jls 6.5.3} of <cite>The Java Language Specification</cite>, for
  89  * example, {@code "java.lang"}. </p>
  90  *

  92  * in this class causes a {@link NullPointerException NullPointerException} to
  93  * be thrown. </p>
  94  *
  95  * @since 9
  96  * @see Class#getModule()
  97  * @jls 7.7 Module Declarations
  98  */
  99 
 100 public final class Module implements AnnotatedElement {
 101 
 102     // the layer that contains this module, can be null
 103     private final ModuleLayer layer;
 104 
 105     // module name and loader, these fields are read by VM
 106     private final String name;
 107     private final ClassLoader loader;
 108 
 109     // the module descriptor
 110     private final ModuleDescriptor descriptor;
 111 
 112     // true, if this module allows restricted native access
 113     private volatile boolean enableNativeAccess;


 114 
 115     /**
 116      * Creates a new named Module. The resulting Module will be defined to the
 117      * VM but will not read any other modules, will not have any exports setup
 118      * and will not be registered in the service catalog.
 119      */
 120     Module(ModuleLayer layer,
 121            ClassLoader loader,
 122            ModuleDescriptor descriptor,
 123            URI uri)
 124     {
 125         this.layer = layer;
 126         this.name = descriptor.name();
 127         this.loader = loader;
 128         this.descriptor = descriptor;
 129 
 130         // define module to VM
 131 
 132         boolean isOpen = descriptor.isOpen() || descriptor.isAutomatic();
 133         Version version = descriptor.version().orElse(null);

  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.io.IOException;
  29 import java.io.InputStream;
  30 import java.lang.annotation.Annotation;
  31 import java.lang.invoke.MethodHandle;
  32 import java.lang.invoke.VarHandle;
  33 import java.lang.module.Configuration;
  34 import java.lang.module.ModuleReference;
  35 import java.lang.module.ModuleDescriptor;
  36 import java.lang.module.ModuleDescriptor.Exports;
  37 import java.lang.module.ModuleDescriptor.Opens;
  38 import java.lang.module.ModuleDescriptor.Version;
  39 import java.lang.module.ResolvedModule;
  40 import java.lang.reflect.AnnotatedElement;
  41 import java.net.URI;
  42 import java.net.URL;
  43 import java.security.AccessController;
  44 import java.security.PrivilegedAction;
  45 import java.util.HashMap;
  46 import java.util.HashSet;
  47 import java.util.List;
  48 import java.util.Map;
  49 import java.util.Objects;
  50 import java.util.Optional;
  51 import java.util.Set;
  52 import java.util.concurrent.ConcurrentHashMap;
  53 import java.util.function.Function;
  54 import java.util.stream.Collectors;
  55 import java.util.stream.Stream;
  56 
  57 import jdk.internal.loader.BuiltinClassLoader;
  58 import jdk.internal.loader.BootLoader;
  59 import jdk.internal.loader.ClassLoaders;
  60 import jdk.internal.misc.CDS;
  61 import jdk.internal.module.ModuleLoaderMap;
  62 import jdk.internal.module.ServicesCatalog;
  63 import jdk.internal.module.Resources;
  64 import jdk.internal.org.objectweb.asm.AnnotationVisitor;
  65 import jdk.internal.org.objectweb.asm.Attribute;
  66 import jdk.internal.org.objectweb.asm.ClassReader;
  67 import jdk.internal.org.objectweb.asm.ClassVisitor;
  68 import jdk.internal.org.objectweb.asm.ClassWriter;
  69 import jdk.internal.org.objectweb.asm.ModuleVisitor;
  70 import jdk.internal.org.objectweb.asm.Opcodes;
  71 import jdk.internal.reflect.CallerSensitive;
  72 import jdk.internal.reflect.Reflection;
  73 import jdk.internal.vm.annotation.Stable;
  74 import sun.security.util.SecurityConstants;
  75 
  76 /**
  77  * Represents a run-time module, either {@link #isNamed() named} or unnamed.
  78  *
  79  * <p> Named modules have a {@link #getName() name} and are constructed by the
  80  * Java Virtual Machine when a graph of modules is defined to the Java virtual
  81  * machine to create a {@linkplain ModuleLayer module layer}. </p>
  82  *
  83  * <p> An unnamed module does not have a name. There is an unnamed module for
  84  * each {@link ClassLoader ClassLoader}, obtained by invoking its {@link
  85  * ClassLoader#getUnnamedModule() getUnnamedModule} method. All types that are
  86  * not in a named module are members of their defining class loader's unnamed
  87  * module. </p>
  88  *
  89  * <p> The package names that are parameters or returned by methods defined in
  90  * this class are the fully-qualified names of the packages as defined in
  91  * section {@jls 6.5.3} of <cite>The Java Language Specification</cite>, for
  92  * example, {@code "java.lang"}. </p>
  93  *

  95  * in this class causes a {@link NullPointerException NullPointerException} to
  96  * be thrown. </p>
  97  *
  98  * @since 9
  99  * @see Class#getModule()
 100  * @jls 7.7 Module Declarations
 101  */
 102 
 103 public final class Module implements AnnotatedElement {
 104 
 105     // the layer that contains this module, can be null
 106     private final ModuleLayer layer;
 107 
 108     // module name and loader, these fields are read by VM
 109     private final String name;
 110     private final ClassLoader loader;
 111 
 112     // the module descriptor
 113     private final ModuleDescriptor descriptor;
 114 
 115     // true, if this module allows restricted native access; @Stable makes sure that modules that allow native
 116     // access capture this property as a constant.
 117     @Stable
 118     private boolean enableNativeAccess;
 119 
 120     /**
 121      * Creates a new named Module. The resulting Module will be defined to the
 122      * VM but will not read any other modules, will not have any exports setup
 123      * and will not be registered in the service catalog.
 124      */
 125     Module(ModuleLayer layer,
 126            ClassLoader loader,
 127            ModuleDescriptor descriptor,
 128            URI uri)
 129     {
 130         this.layer = layer;
 131         this.name = descriptor.name();
 132         this.loader = loader;
 133         this.descriptor = descriptor;
 134 
 135         // define module to VM
 136 
 137         boolean isOpen = descriptor.isOpen() || descriptor.isAutomatic();
 138         Version version = descriptor.version().orElse(null);
< prev index next >