< prev index next >

test/tools/jlink/plugins/SystemModuleDescriptors/src/m4/p4/Main.java

Print this page




  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package p4;
  25 
  26 import java.io.IOException;
  27 import java.io.InputStream;
  28 import java.lang.module.ModuleDescriptor;
  29 import java.lang.module.ModuleFinder;
  30 import java.lang.reflect.Layer;
  31 import java.net.URI;
  32 import java.nio.file.FileSystem;
  33 import java.nio.file.FileSystems;
  34 import java.nio.file.Files;
  35 import java.nio.file.Path;
  36 import java.util.Collections;
  37 import java.util.Set;
  38 








  39 public class Main {
































  40     // the system module plugin by default drops ModuleTarget attribute
  41     private static boolean expectModuleTarget = false;
  42     public static void main(String... args) throws IOException {
  43         if (args.length > 0) {
  44             if (!args[0].equals("retainModuleTarget")) {
  45                 throw new IllegalArgumentException(args[0]);
  46             }
  47 
  48             expectModuleTarget = true;
  49         }
  50 
  51         // java.base is packaged with osName/osArch/osVersion
  52         ModuleDescriptor md = Layer.boot().findModule("java.base").get()
  53                                    .getDescriptor();
  54         if (!md.osName().isPresent() ||
  55                 !md.osArch().isPresent() ||
  56                 !md.osVersion().isPresent()) {
  57             throw new RuntimeException("osName/osArch/osVersion is missing: " +
  58                 md.osName() + " " + md.osArch() + " " + md.osVersion());
  59         }
  60 
  61         // verify module-info.class for m1 and m4
  62         checkModule("m1", "p1", "p2");
  63         checkModule("m4", "p4");
  64     }
  65 
  66     private static void checkModule(String mn, String... packages) throws IOException {
  67         // verify ModuleDescriptor from the runtime module
  68         ModuleDescriptor md = Layer.boot().findModule(mn).get()
  69                                    .getDescriptor();
  70         checkModuleDescriptor(md, packages);
  71 
  72         // verify ModuleDescriptor from module-info.class read from ModuleReader
  73         try (InputStream in = ModuleFinder.ofSystem().find(mn).get()
  74             .open().open("module-info.class").get()) {
  75             checkModuleDescriptor(ModuleDescriptor.read(in), packages);
  76         }
  77 
  78         // verify ModuleDescriptor from module-info.class read from jimage
  79         FileSystem fs = FileSystems.newFileSystem(URI.create("jrt:/"),
  80             Collections.emptyMap());
  81         Path path = fs.getPath("/", "modules", mn, "module-info.class");
  82         checkModuleDescriptor(ModuleDescriptor.read(Files.newInputStream(path)), packages);
  83     }
  84 
  85     static void checkModuleDescriptor(ModuleDescriptor md, String... packages) {
  86         String mainClass = md.name().replace('m', 'p') + ".Main";
  87         if (!md.mainClass().get().equals(mainClass)) {
  88             throw new RuntimeException(md.mainClass().toString());
  89         }
  90 
  91         if (expectModuleTarget) {
  92             // ModuleTarget attribute is retained
  93             if (!md.osName().isPresent() || !md.osArch().isPresent()) {
  94                 throw new RuntimeException("osName or osArch is missing: " +
  95                     md.osName() + " " + md.osArch());
  96             }
  97         } else {
  98             // by default ModuleTarget attribute is dropped
  99             if (md.osName().isPresent() || md.osArch().isPresent()) {
 100                 throw new RuntimeException("osName and osArch should not be set: " +
 101                     md.osName() + " " + md.osArch());
 102             }
 103         }
 104 
 105         if (md.osVersion().isPresent()) {
 106             throw new RuntimeException("Expected no osVersion set: " + md.osVersion());
 107         }
 108 
 109         Set<String> pkgs = md.packages();
 110         if (!pkgs.equals(Set.of(packages))) {
 111             throw new RuntimeException(pkgs + " expected: " + Set.of(packages));
 112         }
 113     }
 114 }


  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package p4;
  25 
  26 import java.io.IOException;
  27 import java.io.InputStream;
  28 import java.lang.module.ModuleDescriptor;
  29 import java.lang.module.ModuleFinder;
  30 import java.lang.reflect.Layer;
  31 import java.net.URI;
  32 import java.nio.file.FileSystem;
  33 import java.nio.file.FileSystems;
  34 import java.nio.file.Files;
  35 import java.nio.file.Path;
  36 import java.util.Collections;
  37 import java.util.Set;
  38 
  39 import jdk.internal.module.ClassFileAttributes;
  40 import jdk.internal.module.ClassFileAttributes.ModuleTargetAttribute;
  41 import jdk.internal.module.ClassFileConstants;
  42 import jdk.internal.org.objectweb.asm.Attribute;
  43 import jdk.internal.org.objectweb.asm.ClassReader;
  44 import jdk.internal.org.objectweb.asm.ClassVisitor;
  45 import jdk.internal.org.objectweb.asm.Opcodes;
  46 
  47 public class Main {
  48     private static boolean hasModuleTarget(InputStream in) throws IOException {
  49         ModuleTargetAttribute[] modTargets = new ModuleTargetAttribute[1];
  50         ClassVisitor cv = new ClassVisitor(Opcodes.ASM5) {
  51             @Override
  52             public void visitAttribute(Attribute attr) {
  53                 if (attr instanceof ModuleTargetAttribute) {
  54                     modTargets[0] = (ModuleTargetAttribute)attr;
  55                 }
  56             }
  57         };
  58 
  59         // prototype of attributes that should be parsed
  60         Attribute[] attrs = new Attribute[] {
  61             new ModuleTargetAttribute()
  62         };
  63 
  64         // parse module-info.class
  65         ClassReader cr = new ClassReader(in);
  66         cr.accept(cv, attrs, 0);
  67         return modTargets[0] != null &&
  68             (modTargets[0].osName() != null || modTargets[0].osArch() != null);
  69     }
  70 
  71     private static boolean hasModuleTarget(String modName) throws IOException {
  72         FileSystem fs = FileSystems.newFileSystem(URI.create("jrt:/"),
  73                                                   Collections.emptyMap());
  74         Path path = fs.getPath("/", "modules", modName, "module-info.class");
  75         try (InputStream in = Files.newInputStream(path)) {
  76             return hasModuleTarget(in);
  77         }
  78     }
  79 
  80     // the system module plugin by default drops ModuleTarget attribute
  81     private static boolean expectModuleTarget = false;
  82     public static void main(String... args) throws IOException {
  83         if (args.length > 0) {
  84             if (!args[0].equals("retainModuleTarget")) {
  85                 throw new IllegalArgumentException(args[0]);
  86             }
  87 
  88             expectModuleTarget = true;
  89         }
  90 
  91         // java.base is packaged with osName/osArch/osVersion
  92         if (! hasModuleTarget("java.base")) {
  93             throw new RuntimeException("ModuleTarget absent for java.base");





  94         }
  95 
  96         // verify module-info.class for m1 and m4
  97         checkModule("m1", "p1", "p2");
  98         checkModule("m4", "p4");
  99     }
 100 
 101     private static void checkModule(String mn, String... packages) throws IOException {
 102         // verify ModuleDescriptor from the runtime module
 103         ModuleDescriptor md = Layer.boot().findModule(mn).get()
 104                                    .getDescriptor();
 105         checkModuleDescriptor(md, packages);
 106 
 107         // verify ModuleDescriptor from module-info.class read from ModuleReader
 108         try (InputStream in = ModuleFinder.ofSystem().find(mn).get()
 109             .open().open("module-info.class").get()) {
 110             checkModuleDescriptor(ModuleDescriptor.read(in), packages);
 111         }
 112 
 113         // verify ModuleDescriptor from module-info.class read from jimage
 114         FileSystem fs = FileSystems.newFileSystem(URI.create("jrt:/"),
 115             Collections.emptyMap());
 116         Path path = fs.getPath("/", "modules", mn, "module-info.class");
 117         checkModuleDescriptor(ModuleDescriptor.read(Files.newInputStream(path)), packages);
 118     }
 119 
 120     static void checkModuleDescriptor(ModuleDescriptor md, String... packages) throws IOException {
 121         String mainClass = md.name().replace('m', 'p') + ".Main";
 122         if (!md.mainClass().get().equals(mainClass)) {
 123             throw new RuntimeException(md.mainClass().toString());
 124         }
 125 
 126         if (expectModuleTarget) {
 127             // ModuleTarget attribute is retained
 128             if (! hasModuleTarget(md.name())) {
 129                 throw new RuntimeException("ModuleTarget missing for " + md.name());

 130             }
 131         } else {
 132             // by default ModuleTarget attribute is dropped
 133             if (hasModuleTarget(md.name())) {
 134                 throw new RuntimeException("ModuleTarget present for " + md.name());

 135             }
 136         }
 137 




 138         Set<String> pkgs = md.packages();
 139         if (!pkgs.equals(Set.of(packages))) {
 140             throw new RuntimeException(pkgs + " expected: " + Set.of(packages));
 141         }
 142     }
 143 }
< prev index next >