< prev index next >

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

Print this page
rev 47453 : imported patch jdk-new-asm-update.patch


  40 import java.net.URL;
  41 import java.security.AccessController;
  42 import java.security.PrivilegedAction;
  43 import java.util.Collections;
  44 import java.util.HashMap;
  45 import java.util.HashSet;
  46 import java.util.Iterator;
  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.JavaLangAccess;
  61 import jdk.internal.misc.SharedSecrets;
  62 import jdk.internal.module.IllegalAccessLogger;
  63 import jdk.internal.module.ModuleLoaderMap;
  64 import jdk.internal.module.ServicesCatalog;
  65 import jdk.internal.module.Resources;
  66 import jdk.internal.org.objectweb.asm.AnnotationVisitor;
  67 import jdk.internal.org.objectweb.asm.Attribute;
  68 import jdk.internal.org.objectweb.asm.ClassReader;
  69 import jdk.internal.org.objectweb.asm.ClassVisitor;
  70 import jdk.internal.org.objectweb.asm.ClassWriter;

  71 import jdk.internal.org.objectweb.asm.Opcodes;
  72 import jdk.internal.reflect.CallerSensitive;
  73 import jdk.internal.reflect.Reflection;
  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


1415 
1416     private Class<?> loadModuleInfoClass() {
1417         Class<?> clazz = null;
1418         try (InputStream in = getResourceAsStream("module-info.class")) {
1419             if (in != null)
1420                 clazz = loadModuleInfoClass(in);
1421         } catch (Exception ignore) { }
1422         return clazz;
1423     }
1424 
1425     /**
1426      * Loads module-info.class as a package-private interface in a class loader
1427      * that is a child of this module's class loader.
1428      */
1429     private Class<?> loadModuleInfoClass(InputStream in) throws IOException {
1430         final String MODULE_INFO = "module-info";
1431 
1432         ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
1433                                          + ClassWriter.COMPUTE_FRAMES);
1434 
1435         ClassVisitor cv = new ClassVisitor(Opcodes.ASM5, cw) {
1436             @Override
1437             public void visit(int version,
1438                               int access,
1439                               String name,
1440                               String signature,
1441                               String superName,
1442                               String[] interfaces) {
1443                 cw.visit(version,
1444                         Opcodes.ACC_INTERFACE
1445                             + Opcodes.ACC_ABSTRACT
1446                             + Opcodes.ACC_SYNTHETIC,
1447                         MODULE_INFO,
1448                         null,
1449                         "java/lang/Object",
1450                         null);
1451             }
1452             @Override
1453             public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
1454                 // keep annotations
1455                 return super.visitAnnotation(desc, visible);
1456             }
1457             @Override
1458             public void visitAttribute(Attribute attr) {
1459                 // drop non-annotation attributes
1460             }





1461         };
1462 
1463         ClassReader cr = new ClassReader(in);
1464         cr.accept(cv, 0);
1465         byte[] bytes = cw.toByteArray();
1466 
1467         ClassLoader cl = new ClassLoader(loader) {
1468             @Override
1469             protected Class<?> findClass(String cn)throws ClassNotFoundException {
1470                 if (cn.equals(MODULE_INFO)) {
1471                     return super.defineClass(cn, bytes, 0, bytes.length);
1472                 } else {
1473                     throw new ClassNotFoundException(cn);
1474                 }
1475             }
1476         };
1477 
1478         try {
1479             return cl.loadClass(MODULE_INFO);
1480         } catch (ClassNotFoundException e) {




  40 import java.net.URL;
  41 import java.security.AccessController;
  42 import java.security.PrivilegedAction;
  43 import java.util.Collections;
  44 import java.util.HashMap;
  45 import java.util.HashSet;
  46 import java.util.Iterator;
  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.module.IllegalAccessLogger;
  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 sun.security.util.SecurityConstants;
  74 
  75 /**
  76  * Represents a run-time module, either {@link #isNamed() named} or unnamed.
  77  *
  78  * <p> Named modules have a {@link #getName() name} and are constructed by the
  79  * Java Virtual Machine when a graph of modules is defined to the Java virtual
  80  * machine to create a {@linkplain ModuleLayer module layer}. </p>
  81  *
  82  * <p> An unnamed module does not have a name. There is an unnamed module for
  83  * each {@link ClassLoader ClassLoader}, obtained by invoking its {@link
  84  * ClassLoader#getUnnamedModule() getUnnamedModule} method. All types that are
  85  * not in a named module are members of their defining class loader's unnamed
  86  * module. </p>
  87  *
  88  * <p> The package names that are parameters or returned by methods defined in
  89  * this class are the fully-qualified names of the packages as defined in


1414 
1415     private Class<?> loadModuleInfoClass() {
1416         Class<?> clazz = null;
1417         try (InputStream in = getResourceAsStream("module-info.class")) {
1418             if (in != null)
1419                 clazz = loadModuleInfoClass(in);
1420         } catch (Exception ignore) { }
1421         return clazz;
1422     }
1423 
1424     /**
1425      * Loads module-info.class as a package-private interface in a class loader
1426      * that is a child of this module's class loader.
1427      */
1428     private Class<?> loadModuleInfoClass(InputStream in) throws IOException {
1429         final String MODULE_INFO = "module-info";
1430 
1431         ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
1432                                          + ClassWriter.COMPUTE_FRAMES);
1433 
1434         ClassVisitor cv = new ClassVisitor(Opcodes.ASM6, cw) {
1435             @Override
1436             public void visit(int version,
1437                               int access,
1438                               String name,
1439                               String signature,
1440                               String superName,
1441                               String[] interfaces) {
1442                 cw.visit(version,
1443                         Opcodes.ACC_INTERFACE
1444                             + Opcodes.ACC_ABSTRACT
1445                             + Opcodes.ACC_SYNTHETIC,
1446                         MODULE_INFO,
1447                         null,
1448                         "java/lang/Object",
1449                         null);
1450             }
1451             @Override
1452             public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
1453                 // keep annotations
1454                 return super.visitAnnotation(desc, visible);
1455             }
1456             @Override
1457             public void visitAttribute(Attribute attr) {
1458                 // drop non-annotation attributes
1459             }
1460             @Override
1461             public ModuleVisitor visitModule(String name, int flags, String version) {
1462                 // drop Module attribute
1463                 return null;
1464             }
1465         };
1466 
1467         ClassReader cr = new ClassReader(in);
1468         cr.accept(cv, 0);
1469         byte[] bytes = cw.toByteArray();
1470 
1471         ClassLoader cl = new ClassLoader(loader) {
1472             @Override
1473             protected Class<?> findClass(String cn)throws ClassNotFoundException {
1474                 if (cn.equals(MODULE_INFO)) {
1475                     return super.defineClass(cn, bytes, 0, bytes.length);
1476                 } else {
1477                     throw new ClassNotFoundException(cn);
1478                 }
1479             }
1480         };
1481 
1482         try {
1483             return cl.loadClass(MODULE_INFO);
1484         } catch (ClassNotFoundException e) {


< prev index next >