< prev index next >

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

Print this page


  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.module.IllegalAccessLogger;
  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


 229      *
 230      * @see java.lang.reflect.Proxy
 231      */
 232     public ModuleLayer getLayer() {
 233         if (isNamed()) {
 234             ModuleLayer layer = this.layer;
 235             if (layer != null)
 236                 return layer;
 237 
 238             // special-case java.base as it is created before the boot layer
 239             if (loader == null && name.equals("java.base")) {
 240                 return ModuleLayer.boot();
 241             }
 242         }
 243         return null;
 244     }
 245 
 246     // --
 247 
 248     // special Module to mean "all unnamed modules"
 249     private static final Module ALL_UNNAMED_MODULE = new Module(null);
 250     private static final Set<Module> ALL_UNNAMED_MODULE_SET = Set.of(ALL_UNNAMED_MODULE);
 251 
 252     // special Module to mean "everyone"
 253     private static final Module EVERYONE_MODULE = new Module(null);
 254     private static final Set<Module> EVERYONE_SET = Set.of(EVERYONE_MODULE);











































 255 
 256     /**
 257      * The holder of data structures to support readability, exports, and
 258      * service use added at runtime with the reflective APIs.
 259      */
 260     private static class ReflectionData {
 261         /**
 262          * A module (1st key) reads another module (2nd key)
 263          */
 264         static final WeakPairMap<Module, Module, Boolean> reads =
 265             new WeakPairMap<>();
 266 
 267         /**
 268          * A module (1st key) exports or opens a package to another module
 269          * (2nd key). The map value is a map of package name to a boolean
 270          * that indicates if the package is opened.
 271          */
 272         static final WeakPairMap<Module, Module, Map<String, Boolean>> exports =
 273             new WeakPairMap<>();
 274 




  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.VM;
  59 import jdk.internal.module.IllegalAccessLogger;
  60 import jdk.internal.module.ModuleLoaderMap;
  61 import jdk.internal.module.ServicesCatalog;
  62 import jdk.internal.module.Resources;
  63 import jdk.internal.org.objectweb.asm.AnnotationVisitor;
  64 import jdk.internal.org.objectweb.asm.Attribute;
  65 import jdk.internal.org.objectweb.asm.ClassReader;
  66 import jdk.internal.org.objectweb.asm.ClassVisitor;
  67 import jdk.internal.org.objectweb.asm.ClassWriter;
  68 import jdk.internal.org.objectweb.asm.ModuleVisitor;
  69 import jdk.internal.org.objectweb.asm.Opcodes;
  70 import jdk.internal.reflect.CallerSensitive;
  71 import jdk.internal.reflect.Reflection;
  72 import sun.security.util.SecurityConstants;
  73 
  74 /**
  75  * Represents a run-time module, either {@link #isNamed() named} or unnamed.
  76  *
  77  * <p> Named modules have a {@link #getName() name} and are constructed by the
  78  * Java Virtual Machine when a graph of modules is defined to the Java virtual


 230      *
 231      * @see java.lang.reflect.Proxy
 232      */
 233     public ModuleLayer getLayer() {
 234         if (isNamed()) {
 235             ModuleLayer layer = this.layer;
 236             if (layer != null)
 237                 return layer;
 238 
 239             // special-case java.base as it is created before the boot layer
 240             if (loader == null && name.equals("java.base")) {
 241                 return ModuleLayer.boot();
 242             }
 243         }
 244         return null;
 245     }
 246 
 247     // --
 248 
 249     // special Module to mean "all unnamed modules"
 250     private static final Module ALL_UNNAMED_MODULE;
 251     private static final Set<Module> ALL_UNNAMED_MODULE_SET;
 252 
 253     // special Module to mean "everyone"
 254     private static final Module EVERYONE_MODULE;
 255     private static final Set<Module> EVERYONE_SET;
 256 
 257     private static class ArchivedData {
 258         private static ArchivedData archivedData;
 259         private final Module allUnnamedModule;
 260         private final Set<Module> allUnnamedModules;
 261         private final Module everyoneModule;
 262         private final Set<Module> everyoneSet;
 263 
 264         private ArchivedData() {
 265             this.allUnnamedModule = ALL_UNNAMED_MODULE;
 266             this.allUnnamedModules = ALL_UNNAMED_MODULE_SET;
 267             this.everyoneModule = EVERYONE_MODULE;
 268             this.everyoneSet = EVERYONE_SET;
 269         }
 270 
 271         static void archive() {
 272             archivedData = new ArchivedData();
 273         }
 274 
 275         static ArchivedData get() {
 276             return archivedData;
 277         }
 278 
 279         static {
 280             VM.initializeFromArchive(ArchivedData.class);
 281         }
 282     }
 283 
 284     static {
 285         ArchivedData archivedData = ArchivedData.get();
 286         if (archivedData != null) {
 287             ALL_UNNAMED_MODULE = archivedData.allUnnamedModule;
 288             ALL_UNNAMED_MODULE_SET = archivedData.allUnnamedModules;
 289             EVERYONE_MODULE = archivedData.everyoneModule;
 290             EVERYONE_SET = archivedData.everyoneSet;
 291         } else {
 292             ALL_UNNAMED_MODULE = new Module(null);
 293             ALL_UNNAMED_MODULE_SET = Set.of(ALL_UNNAMED_MODULE);
 294             EVERYONE_MODULE = new Module(null);
 295             EVERYONE_SET = Set.of(EVERYONE_MODULE);
 296             ArchivedData.archive();
 297         }
 298     }
 299 
 300     /**
 301      * The holder of data structures to support readability, exports, and
 302      * service use added at runtime with the reflective APIs.
 303      */
 304     private static class ReflectionData {
 305         /**
 306          * A module (1st key) reads another module (2nd key)
 307          */
 308         static final WeakPairMap<Module, Module, Boolean> reads =
 309             new WeakPairMap<>();
 310 
 311         /**
 312          * A module (1st key) exports or opens a package to another module
 313          * (2nd key). The map value is a map of package name to a boolean
 314          * that indicates if the package is opened.
 315          */
 316         static final WeakPairMap<Module, Module, Map<String, Boolean>> exports =
 317             new WeakPairMap<>();
 318 


< prev index next >